package com.sea.index; import java.io.File; import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Date; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.LockObtainFailedException; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.Version; public class DataBaseIndex { /** * @param args * @throws SQLException * @throws IOException * @throws LockObtainFailedException * @throws CorruptIndexException * @throws ParseException */ public static void main(String[] args) throws CorruptIndexException, LockObtainFailedException, IOException, SQLException, ParseException { // TODO Auto-generated method stub String url = "jdbc:oracle:thin:@10.1.80.170:1521:NETCOOL"; String user = "uum"; String pwd = "uum"; DataBaseIndex test = new DataBaseIndex(); test.makeIndexForDatabase(test.getConnection(url, user, pwd)); test.findSM("P55A"); } public void findSM(String str) throws IOException, ParseException { File file = new File("d:\\data"); FSDirectory directory = FSDirectory.open(file); IndexReader reader = IndexReader.open(directory); IndexSearcher is = new IndexSearcher(reader); QueryParser query = new QueryParser(Version.LUCENE_36, "NAME", new StandardAnalyzer(Version.LUCENE_36)); Query q1 = query.parse(str); ScoreDoc[] docs = is.search(q1, is.maxDoc()).scoreDocs; for (int i = 0; i < docs.length; i++) { Date start = new Date(); String ip = is.doc(docs[i].doc).get("IP"); String name = is.doc(docs[i].doc).get("NAME"); String id = is.doc(docs[i].doc).get("ID"); Date end = new Date(); System.out.println("===========" + id + " : " +name+" : "+ip+" "+(end.getTime() - start.getTime()) + "ms"); } } public Connection getConnection(String url, String user, String password) { try { Class.forName("oracle.jdbc.OracleDriver"); return DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return null; } public ResultSet executeSQL(Connection conn, String sql) { try { Statement smt = conn.createStatement(); return smt.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } return null; } public void makeIndexForDatabase(Connection conn) throws CorruptIndexException, LockObtainFailedException, IOException, SQLException { convert(executeSQL(conn, "select serial,name,new_netip from ams_asset_tab where new_netip is not null")); } public void convert(ResultSet rs) throws SQLException, CorruptIndexException, LockObtainFailedException, IOException { while (rs.next()) { File file = new File("d:\\data"); FSDirectory directory = FSDirectory.open(file); IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_36, new StandardAnalyzer(Version.LUCENE_36)); IndexWriter writer = new IndexWriter(directory, conf); Document doc = new Document(); String serial1 = rs.getString("serial"); String name1 = rs.getString("name"); String ip1 = rs.getString("new_netip"); System.out.println("serial:" + serial1 + "\t" + "name:" + name1 + "\t" + "ip:" + ip1); Field serial = new Field("ID", serial1, Field.Store.YES, Field.Index.ANALYZED); doc.add(serial); Field name = new Field("NAME", name1, Field.Store.YES, Field.Index.ANALYZED); doc.add(name); Field ip = new Field("IP", ip1, Field.Store.YES, Field.Index.ANALYZED); doc.add(ip); writer.addDocument(doc); writer.close(); } } }
Luence 数据库 Demo
猜你喜欢
转载自sants.iteye.com/blog/1867827
今日推荐
周排行