Lucese循环遍历索引写入数据库

package www.sungoal.com;

importjava.io.File;

importjava.util.ArrayList;

importjava.util.List;

importjava.util.Map;

importorg.apache.lucene.document.Document;

importorg.apache.lucene.index.IndexReader;

importorg.apache.lucene.index.TermDocs;

importorg.apache.lucene.store.FSDirectory;

publicclassWriter{

publicstaticvoidmain(String[]args){

try{

StringindexPath=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]);

}

Stringtable="";

if(args.length>1){

table=args[1];

}else{

String[]t=indexPath.split("\\\\");

Stringname=t[t.length-2];

table=name.substring(name.indexOf(".")+1);

}

System.out.println("---------------开始抽取"+table+"表-----------------");

Map<String,String>map=newBaseDao().getTableInfo(table);

Stringsql="insertinto"+table+"(";

intindex=0;

for(Map.Entry<String,String>field:map.entrySet()){

index++;

Stringname=field.getValue();

if(index==map.size()){

sql+=name;

}else{

sql+=name+",";

}

}

sql+=")values(";

IndexReaderreader=IndexReader.open(FSDirectory.open(newFile(indexPath)));

TermDocstermDocs=reader.termDocs();

intmaxDoc=reader.maxDoc();

System.out.println(maxDoc);

BaseDaodao=newBaseDao();

List<String>sqls=newArrayList<String>();

intrecordNumber=0;

while(termDocs.next()){

recordNumber++;

//System.out.println(recordNumber);

intdocId=termDocs.doc();

//System.out.println("------------document:"+docId);

//根据当前documentId获取Document

Documentdoc=reader.document(docId);

StringBuffertempSQL=newStringBuffer();

tempSQL.append(sql);

intnField=0;

for(Stringfield_ch:map.keySet()){

nField++;

Stringvalue=null;

try{

value=doc.get(field_ch);

}catch(Exceptionex){

}

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时间:"+newjava.util.Date());

dao.executeBat(sqls,recordNumber,table);

sqls=newArrayList<String>();

System.out.println("数据库写入时间:"+newjava.util.Date());

}

}

if(sqls.size()>0){

dao.executeBat(sqls,recordNumber,table);

sqls=newArrayList<String>();

}

System.out.println("-------------"+table+"表抽取完毕,共"+maxDoc+"条,抽取了"+recordNumber+"条--------------");

reader.close();

}catch(Exceptionex){

ex.printStackTrace();

}

}

}

相关推荐