package com.chedong.weblucene.index;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import org.apache.lucene.queryParser.ParseException;
import com.chedong.weblucene.beans.UserInfo;
public class TestIndex {
public final static String indexDir = "C:\\user\\index";
private static Connection getConnection() {
Connection conn = null;
String url = "jdbc:mysql://127.0.0.1:3306/bookmarks";
String userName = "root";
String password = "xiduo";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = java.sql.DriverManager
.getConnection(url, userName, password);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Error Trace in getConnection() : "
+ e.getMessage());
}
return conn;
}
public static void main(String[] args) throws
IOException, ParseException, SQLException {
index();
}
public static void index() throws SQLException {
Connection conn = null;
try {
conn = getConnection();
UserInfo[] list = UserInfo.loadPhotos(conn);
IndexerFile.indexFile(indexDir, list);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.close();
}
}
}
}
####################################################
package com.chedong.weblucene.beans;
import java.sql.PreparedStatement;
import java.util.Vector;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
public class UserInfo {
private long idUserInfo;
private String nickName;
public UserInfo() {
}
public long getIdUserInfo() {
return idUserInfo;
}
public void setIdUserInfo(long idUserInfo) {
this.idUserInfo = idUserInfo;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public static UserInfo[] loadPhotos(Connection con) throws Exception {
Vector list = new Vector();
PreparedStatement pstm = null;
ResultSet rs = null;
String sql = "select IdUserInfo, NickName from user_info";
try {
pstm = con.prepareStatement(sql);
rs = pstm.executeQuery();
while (rs.next()) {
UserInfo userinfo = new UserInfo();
userinfo.setIdUserInfo(rs.getLong(1));
userinfo.setNickName(rs.getString(2));
list.add(userinfo);
}
} catch (SQLException e) {
e.printStackTrace();
throw new Exception(" loadUserInfoByDate has error");
} finally {
if (rs != null) {
rs.close();
}
if (pstm != null) {
pstm.close();
}
}
return (UserInfo[]) list.toArray(new UserInfo[list.size()]);
}
}
#############################################
package com.chedong.weblucene.index;
import org.apache.lucene.index.IndexWriter;
import java.io.IOException;
import org.apache.lucene.analysis.cjk.CJKAnalyzer;
import java.io.File;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import com.chedong.weblucene.beans.UserInfo;
import java.util.Date;
public class IndexerFile {
//使用方法:IndexFiles[索引输出目录][索引的文件列表]
public static int indexFile(String indexDir, UserInfo[] list) throws
IOException {
long startTime = new Date().getTime();
File segments = new File(indexDir + File.separator + "segments");
boolean bCreate = !segments.exists();
bCreate = true;
//用指定的语言分析器构造一个新的写索引器(第三个参数表示是否为追加索引);
CJKAnalyzer analyzer = new CJKAnalyzer();
IndexWriter writer = new IndexWriter(indexDir, analyzer, bCreate);
String strNickName="";
for (int i = 0; i < list.length; i++) {
Document doc = new Document();
doc.add(Field.UnIndexed("IdUserInfo",
String.valueOf(list[i].getIdUserInfo())));
System.out.println("getIdUserInfo=" + i + "=" +
list[i].getIdUserInfo());
strNickName=list[i].getNickName();
if(strNickName==null){
strNickName="游客" + list[i].getIdUserInfo();
}
doc.add(Field.Text("NickName", strNickName));
System.out.println("getAddress=" + i + "=" + strNickName);
writer.addDocument(doc); //经文档写入索引
}
long endTime = new Date().getTime();
System.out .println("建立索引时间" + (endTime - startTime));
int numIndexed = writer.docCount();
writer.optimize();
//关闭写索引器
writer.close();
return numIndexed;
}
}