测试环境:Mac+JDK1.6
测试工具:eclipse4.3,mysql
lucene版本:lucene4.0
主要Jar包:ant- 1.8.2.jar、junit-4.10.jar、junit4-ant-2.0.1.jar、lucene-analyzers-common- 4.0.0.jar、lucene-codecs-4.0.0.jar、lucene-core-4.0.0.jar
lucene- join-4.0.0.jar、lucene-memory-4.0.0.jar、lucene-queries-4.0.0.jar、lucene- queryparser-4.0.0.jar、lucene-test-framework-4.0.0.jar
mysql-connector-java-5.1.19-bin.jar、randomizedtesting-runner-2.0.1.jar
1)新建Java工程:创建对象实体类——User
/**
* 实体对象
* @author zzg
*
*/
public class User {
private Integer userId;
private String rpsId;
private String userType;
private Integer personId;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getRpsId() {
return rpsId;
}
public void setRpsId(String rpsId) {
this.rpsId = rpsId;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public Integer getPersonId() {
return personId;
}
public void setPersonId(Integer personId) {
this.personId = personId;
}
}
/**
* 链接数据源
* @author zzg
*
*/
public class DBSource {
private String password="数据库秘密";
private String url="jdbc:mysql://数据库ip:端口/数据库名称?autoReconnect=true&characterEncoding=utf8";
private String userName="登录名";
private static final String JDBC_DRIVER="com.mysql.jdbc.Driver";
private Statement st;
private Connection conn;
private ResultSet rs;
/**
* 内部类获得数据源对象
*/
private static class DBInstance{
public final static DBSource instance= new DBSource();
}
public static DBSource getInstance(){
return DBInstance.instance;
}
/**
*连接数据库
*/
public Connection getConn(){
try {
Class.forName(JDBC_DRIVER);
conn=DriverManager.getConnection(url,userName,password);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
/**
* 创建执行SQL
*/
public Statement getStm(){
conn = getConn();
try {
st=conn.createStatement();
return st;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 返回结果集
*/
public ResultSet getResult(String sql){
st=getStm();
try {
rs=st.executeQuery(sql);
return rs;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 处理关闭按钮
*/
public void close(){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(st!=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/**
* lucene核心测试处理方法
* @author zzg
*
*/
public class LuceneUtil {
public static final String INDEX_DIRECTORY = "/Users/zzg/Documents/workspace/luceneDir";
public static final String WRITE_LOCK_NAME="write.lock";
//private static Analyzer analyzer = new IK_CAnalyzer();
private static StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
/**
* 创建索引库
*/
@SuppressWarnings("deprecation")
public void createIndex(){
try {
//true 表示每此都创建一个新的文件夹,false表示每次都覆盖
FSDirectory dir = FSDirectory.open(new File(INDEX_DIRECTORY));
IndexWriterConfig config =new IndexWriterConfig(Version.LUCENE_40, analyzer);
IndexWriter indexWriter=new IndexWriter(dir, config);
DBSource dbSource = DBSource.getInstance();
//DBSource dbSource = new DBSource();
Connection conn=dbSource.getConn();
Statement stm = null;
ResultSet rs = null;
String sql="select user_id,rps_id,user_type,person_id from 表名";
stm=dbSource.getStm();
rs=dbSource.getResult(sql);
List<User> list = new ArrayList<User>();
try {
while(rs.next()){
User user = new User();
user.setUserId(rs.getInt(1));
user.setRpsId(rs.getString(2));
user.setUserType(rs.getString(3));
user.setPersonId(rs.getInt(4));
list.add(user);
}
System.out.println("总记录条数:"+list.size());
for (int i = 0; i < list.size(); i++) {
//创建一个lucene索引
Document document = new Document();
String userId=list.get(i).getUserId().toString();
String rpsId=list.get(i).getRpsId();
String userType=list.get(i).getUserType();
String personId=list.get(i).getPersonId().toString();
//将用户ID添加到索引文件中并高亮显示
document.add(new Field("userId" , userId , Field.Store.YES , Field.Index.ANALYZED , Field.TermVector.WITH_POSITIONS_OFFSETS));
document.add(new Field("rpsId" , rpsId , Field.Store.YES , Field.Index.ANALYZED , Field.TermVector.WITH_POSITIONS_OFFSETS));
document.add(new Field("userType" , userType , Field.Store.YES , Field.Index.ANALYZED , Field.TermVector.WITH_POSITIONS_OFFSETS));
document.add(new Field("personId" , personId , Field.Store.YES , Field.Index.ANALYZED , Field.TermVector.WITH_POSITIONS_OFFSETS));
//document.add(new Field("rpsId" , rpsId , Field.Store.YES , Field.Index.ANALYZED , Field.TermVector.WITH_POSITIONS_OFFSETS));
indexWriter.addDocument(document);
}
//System.out.println(list.size());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
indexWriter.close();
dbSource.close();
if(IndexWriter.isLocked(dir)){
dir.makeLock(WRITE_LOCK_NAME);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 索引查询
*/
public List<PO> searcheFile(String keyword) throws IOException, ParseException{
Date startTime= new Date(System.currentTimeMillis());
System.out.println("开始时间:"+getDate(startTime));
Directory ireader = FSDirectory.open(new File(INDEX_DIRECTORY));
//System.out.println(iread);
System.out.println();
IndexReader reader = DirectoryReader.open(ireader);
IndexSearcher searcher=new IndexSearcher(reader);
QueryParser parser = new QueryParser(Version.LUCENE_40, "userType", analyzer);
Query query = parser.parse(keyword);
TopScoreDocCollector topScoreDocCollector = TopScoreDocCollector.create(100, true);
searcher.search(query, topScoreDocCollector);
//处理返回结果集
ScoreDoc[] docs = topScoreDocCollector.topDocs().scoreDocs;
System.out.println("总记录数:"+topScoreDocCollector.getTotalHits());
for (int i = 0; i < docs.length; i++) {
//ScoreDoc scoreDoc = hits[i];
Document result = searcher.doc(docs[i].doc);
}
Date endTime= new Date(System.currentTimeMillis());
System.out.println("结束时间:"+getDate(endTime));
System.out.println("通过lucene索引查询总计用时:"+(endTime.getTime()-startTime.getTime()));
System.out.println("================================================================");
reader.close();
return null;
}
/**
* 通过sql语句查询
*/
public void searchBySql(String columName,Object value){
DBSource dbSource = DBSource.getInstance();
//DBSource dbSource = new DBSource();
Connection conn=dbSource.getConn();
Statement stm = null;
ResultSet rs = null;
String sql="select user_id,rps_id,user_type,person_id from linksus_relation_weibouser where "+columName+"="+value;
stm=dbSource.getStm();
Date startTime=new Date();
System.out.println(getDate(startTime));
rs=dbSource.getResult(sql);
List<User> list = new ArrayList<User>();
try {
while(rs.next()){
User user = new User();
user.setUserId(rs.getInt(1));
user.setRpsId(rs.getString(2));
user.setUserType(rs.getString(3));
user.setPersonId(rs.getInt(4));
list.add(user);
}
System.out.println("总记录条数:"+list.size());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
dbSource.close();
}
Date endTime=new Date();
System.out.println(getDate(endTime));
System.out.println("通过sql语句直接查询总计用时:"+(endTime.getTime()-startTime.getTime()));
}
/**
* 时间格式化处理
*/
public String getDate(Date time){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-ss hh:mm:ss ssss");
String dateStr=dateFormat.format(time);
return dateStr;
};
}
4)测试类——Junit 单元测试
测试user_type=1或2
public class Test {
@org.junit.Test
public void testIndex() throws IOException, ParseException{
LuceneUtil lu = new LuceneUtil();
//创建索引
//lu.createIndex();
//通过索引查询user_type=1的数据
lu.searcheFile("1");
//通过直接查询数据库查询user_type=1的数据
lu.searchBySql("user_type",1);
}
}
测试结果:
user_type |
lucene索引查询 |
sql直接查询 |
---|---|---|
1 | 总记录数:174448 用时:637~545毫秒 |
总记录数:174448 用时:2966~2035毫秒 |
2 | 总记录数:27996 用时:601~565毫秒 |
总记录数:27996 用时:1228~745毫秒 |