首页 / 爬虫 / 爬虫系统Lucene分词
爬虫系统Lucene分词
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了爬虫系统Lucene分词,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2206字,纯文字阅读大概需要4分钟。
内容图文
思路:查询数据库中信息,查询出id和name把那么进行分词存入文件
package com.open1111.index;
import java.io.IOException;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.apache.log4j.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import com.open1111.util.DbUtil;
import com.open1111.util.PropertiesUtil;
public class JarsIndex {
private static Logger logger=Logger.getLogger(JarsIndex.class);
private static Connection con=null;
public static void main(String[] args) throws IOException{
logger.info("创建索引开始");
DbUtil dbUtil=new DbUtil();
try {
con=dbUtil.getCon();
logger.info("创建数据库连接成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.info("创建数据库连接失败");
}
Directory dir=FSDirectory.open(Paths.get(PropertiesUtil.getValue("indexFile")));
Analyzer analyzer=new StandardAnalyzer();
IndexWriterConfig conf=new IndexWriterConfig(analyzer);
IndexWriter writer=new IndexWriter(dir, conf);
String sql="select * from t_jar where indexState=0";
try{
PreparedStatement pstmt=con.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
while(rs.next()){
String id=rs.getString("uuid");
String name=rs.getString("name");
Document doc=new Document();
doc.add(new StringField("id",id,Field.Store.YES));
doc.add(new TextField("name",name.replaceAll("-", " "),Field.Store.YES));
writer.addDocument(doc);
// 更新数据库indexState状态字段 改成1
String sql2="update t_jar set indexState=1 where uuid=‘"+id+"‘";
PreparedStatement pstmt2=con.prepareStatement(sql2);
pstmt2.executeUpdate();
}
}catch(Exception e){
logger.error("执行数据库报错", e);
}
try {
dbUtil.closeCon(con);
} catch (Exception e) {
logger.error("Exception", e);
}
writer.close(); // 关闭写入
logger.info("创建索引完成");
}
}
原文:http://www.cnblogs.com/csy666/p/6602424.html
内容总结
以上是互联网集市为您收集整理的爬虫系统Lucene分词全部内容,希望文章能够帮你解决爬虫系统Lucene分词所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。