爬蟲系統Lucene分詞

來源:互聯網
上載者:User

標籤:uuid   store   val   close   lock   while   org   pac   ace   

思路:查詢資料庫中資訊,查詢出id和name把那麼進行分詞存入檔案

 

package com.open1111.index;

import java.io.IOException;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.open1111.util.DbUtil;
import com.open1111.util.PropertiesUtil;


public class JarsIndex {

private static Logger logger=Logger.getLogger(JarsIndex.class);

private static Connection con=null;

public static void main(String[] args) throws IOException{
logger.info("建立索引開始");
DbUtil dbUtil=new DbUtil();
try {
con=dbUtil.getCon();
logger.info("建立資料庫連接成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.info("建立資料庫連接失敗");
}
Directory dir=FSDirectory.open(Paths.get(PropertiesUtil.getValue("indexFile")));
Analyzer analyzer=new StandardAnalyzer();
IndexWriterConfig conf=new IndexWriterConfig(analyzer);
IndexWriter writer=new IndexWriter(dir, conf);

String sql="select * from t_jar where indexState=0";
try{
PreparedStatement pstmt=con.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
String id=rs.getString("uuid");
String name=rs.getString("name");
Document doc=new Document();
doc.add(new StringField("id",id,Field.Store.YES));
doc.add(new TextField("name",name.replaceAll("-", " "),Field.Store.YES));
writer.addDocument(doc);

// 更新資料庫indexState狀態欄位 改成1
String sql2="update t_jar set indexState=1 where uuid=‘"+id+"‘";
PreparedStatement pstmt2=con.prepareStatement(sql2);
pstmt2.executeUpdate();
}
}catch(Exception e){
logger.error("執行資料庫報錯", e);
}
try {
dbUtil.closeCon(con);
} catch (Exception e) {
logger.error("Exception", e);
}
writer.close(); // 關閉寫入
logger.info("建立索引完成");
}
}

爬蟲系統Lucene分詞

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.