Lucese循环遍历索引写入数据库
package www.sungoal.com;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.store.FSDirectory;
public class Writer {
???
???
??? public static void main(String[] args) {
??? ???
??? ??? try {
??? ??? ??? String indexPath =args.length<=0?"E:\\index\\SDI2.T_ZAZD_BJ_YW_BJAJXX\\SDI2.T_ZAZD_BJ_YW_BJAJXX_20120322225151334":args[0];
??? ??? ??? if(indexPath.equals("")){
??? ??? ??? ??? System.out.println("------------索引路径为空-------------");
??? ??? ??? ??? return;
??? ??? ??? }
??? ??? ??? if(args.length==2){
??? ??? ??? ??? System.out.println("索引路径:"+args[0]);
??? ??? ??? ??? System.out.println("表名:"+args[1]);
??? ??? ??? }
??? ??? ??? String table = "";
??? ??? ??? if(args.length>1){
??? ??? ??? ??? table=args[1];
??? ??? ??? }else{
??? ??? ??? ??? String[]? t = indexPath.split("\\\");
??? ??? ??? ??? String name=t[t.length-2];
??? ??? ??? ??? table=name.substring(name.indexOf(".")+1);
??? ??? ??? }
??? ??? ???
??? ??? ???
??? ??? ??? System.out.println("---------------开始抽取"+table+"表-----------------");
??? ??? ??? Map<String, String> map = new BaseDao().getTableInfo(table);
??? ??? ???
??? ??? ??? String sql = "insert into "+table+"(";
??? ??? ??? int index = 0;
??? ??? ??? for (Map.Entry<String, String> field : map.entrySet()) {
??? ??? ??? ??? index ++;
??? ??? ??? ??? String name = field.getValue();
??? ??? ??? ??? if(index == map.size()) {
??? ??? ??? ??? ??? sql += name;
??? ??? ??? ??? } else {
??? ??? ??? ??? ??? sql += name + ",";
??? ??? ??? ??? }
??? ??? ??? }
??? ??? ??? sql += ") values(";
??? ??? ???
??? ??? ???
??? ??? ??? IndexReader reader = IndexReader.open(FSDirectory.open(new File(indexPath)));
??? ??? ???
??? ??? ??? TermDocs termDocs = reader.termDocs();
??? ??? ??? int maxDoc = reader.maxDoc();
??? ??? ??? System.out.println(maxDoc);
??? ??? ???
??? ??? ??? BaseDao dao = new BaseDao();
??? ??? ??? List<String> sqls = new ArrayList<String>();
??? ??? ??? int? recordNumber = 0;
??? ??? ??? while (termDocs.next()) {
??? ??? ??? ???
??? ??? ??? ??? recordNumber++;
??? ??? ??? ??? //System.out.println(recordNumber);
??? ??? ??? ??? int docId = termDocs.doc();
??? ??? ??? ???
??? ??? ??? ??? //System.out.println("------------document:"+docId);
??? ??? ??? ???
??? ??? ??? ??? //根据当前documentId获取Document
??? ??? ??? ??? Document doc = reader.document(docId);
??? ??? ??? ???
??? ??? ??? ??? StringBuffer tempSQL = new StringBuffer();
??? ??? ??? ??? tempSQL.append(sql);
??? ??? ??? ??? int nField = 0;
??? ??? ??? ??? for(String field_ch : map.keySet()) {
??? ??? ??? ??? ??? nField ++;
??? ??? ??? ??? ??? String value = null;
??? ??? ??? ??? ??? try {
??? ??? ??? ??? ??? ??? value = doc.get(field_ch);
??? ??? ??? ??? ??? } catch (Exception ex) {
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? if(value == null) {
??? ??? ??? ??? ??? ??? value = "";
??? ??? ??? ??? ??? }
??? ??? ??? ??? ??? if(nField == map.size()) {
??? ??? ??? ??? ??? ??? tempSQL.append("'" + Molest.randString(value) + "'");
??? ??? ??? ??? ??? } else {
??? ??? ??? ??? ??? ??? tempSQL.append("'" + Molest.randString(value) + "'" + ",");
??? ??? ??? ??? ??? }
??? ??? ??? ??? }
??? ??? ??? ??? tempSQL.append(")");
??? ??? ??? ??? sqls.add(tempSQL.toString());
??? ??? ??? ??? //System.out.println(tempSQL.toString());
??? ??? ??? ??? if(recordNumber%500==0){
??? ??? ??? ??? ???
??? ??? ??? ??? ??? System.out.println(" ");
??? ??? ??? ??? ??? System.out.println("拼SQL时间???? :"+new java.util.Date());
??? ??? ??? ??? ??? dao.executeBat(sqls,recordNumber,table);
??? ??? ??? ??? ??? sqls=new ArrayList<String>();
??? ??? ??? ??? ??? System.out.println("数据库写入时间:"+new java.util.Date());
??? ??? ??? ??? }
??? ??? ??? }
??? ??? ??? if(sqls.size()>0){
??? ??? ??? ??? dao.executeBat(sqls,recordNumber,table);
??? ??? ??? ??? sqls=new ArrayList<String>();
??? ??? ??? }
??? ??? ??? System.out.println("-------------"+table+"表抽取完毕,共"+maxDoc+"条,抽取了"+recordNumber+"条--------------");
??? ??? ??? reader.close();
??? ??? } catch (Exception ex) {
??? ??? ??? ex.printStackTrace();
??? ??? }
??? }
???
???
}