从数据库里取数据并且用lucene添加索引

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;
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值