importjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.io.PrintWriter;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.Statement;importorg.apache.lucene.analysis.Analyzer;importorg.apache.lucene.analysis.standard.StandardAnalyzer;importorg.apache.lucene.document.Document;importorg.apache.lucene.document.Field;importorg.apache.lucene.index.IndexWriter;publicclassIndex {publicstaticvoidmain(String[] args) {try{
Index index=newIndex();
String path="d:\\index";//索引文件的存放路径String storeIdPath="d:\\storeId.txt";//存储ID的路径String storeId="";
storeId=index.getStoreId(storeIdPath);
ResultSet rs=index.getResult(storeId);
index.indexBuilding(path, storeIdPath, rs);
storeId=index.getStoreId(storeIdPath);
System.out.println(storeId);//打印出这次存储起来的ID}catch(Exception e) {
e.printStackTrace();
}
}publicResultSet getResult(String storeId)throwsException{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://localhost:3306/ding";
String userName="root";
String password="ding";
Connection conn=DriverManager.getConnection(url,userName,password);
Statement stmt=conn
.createStatement();
ResultSet rs=stmt
.executeQuery("select * from newitem where id > '"+storeId+"'order by id");returnrs;
}publicbooleanindexBuilding(String path,String storeIdPath, ResultSet rs) {//把RS换成LIST原理一样try{
Analyzer luceneAnalyzer=newStandardAnalyzer();//取得存储起来的ID,以判定是增量索引还是重新索引booleanisEmpty=true;try{
File file=newFile(storeIdPath);if(!file.exists()) {
file.createNewFile();
}
FileReader fr=newFileReader(storeIdPath);
BufferedReader br=newBufferedReader(fr);if(br.readLine()!=null) {
isEmpty=false;
}
br.close();
fr.close();
}catch(IOException e) {
e.printStackTrace();
}
IndexWriter writer=newIndexWriter(path, luceneAnalyzer, isEmpty);//参数isEmpty是false表示增量索引String storeId="";booleanindexFlag=false;
String id;
String title;while(rs.next()) {//for(Iterator it = list.iterator();it.hasNext();){id=rs.getString("id");
title=rs.getString("title");
writer.addDocument(Document(id, title));
storeId=id;//将拿到的id给storeId,这种拿法不合理,这里为了方便indexFlag=true;
}
writer.optimize();
writer.close();if(indexFlag){//将最后一个的ID存到磁盘文件中this.writeStoreId(storeIdPath, storeId);
}returntrue;
}catch(Exception e) {
e.printStackTrace();
System.out.println("出错了"+e.getClass()+"\n 错误信息为:"+e.getMessage());returnfalse;
}
}publicstaticDocument Document(String id, String title) {
Document doc=newDocument();
doc.add(newField("ID", id, Field.Store.YES, Field.Index.TOKENIZED));
doc.add(newField("TITLE", title, Field.Store.YES,
Field.Index.TOKENIZED));returndoc;
}//取得存储在磁盘中的IDpublicstaticString getStoreId(String path) {
String storeId="";try{
File file=newFile(path);if(!file.exists()) {
file.createNewFile();
}
FileReader fr=newFileReader(path);
BufferedReader br=newBufferedReader(fr);
storeId=br.readLine();if(storeId==null||storeId=="")
storeId="0";
br.close();
fr.close();
}catch(Exception e) {
e.printStackTrace();
}returnstoreId;
}//将ID写入到磁盘文件中publicstaticbooleanwriteStoreId(String path,String storeId) {booleanb=false;try{
File file=newFile(path);if(!file.exists()) {
file.createNewFile();
}
FileWriter fw=newFileWriter(path);
PrintWriter out=newPrintWriter(fw);
out.write(storeId);
out.close();
fw.close();
b=true;
}catch(IOException e) {
e.printStackTrace();
}returnb;
}
}