lunece建立索引遇到的问题

 

最近对lucene的检索进行了肤浅的学习  先是把论坛里大部分的lucene的帖子看了下  大致了解了下lucene   决定学习  在自己测试的时候  发现在对大表的创建索引时耗费的时间实在太长 想通过多线程来解决 对一个表的总记录数来决定创建几个线程来创建索引, 结果是报错:

 D:\lucene\index\_a.fnm (系统找不到指定的文件。)

Lock obtain timed out: SimpleFSLock@D:\lucene\index\write.lock

之类

 

难道lucene创建索引耗费是太长的问题真的没有解决  ,因为是初学没有去研究源代码  只是在自己的博客上发发感概  无意看到我篇文章的朋友不要笑我肤浅啊 鼓了勇气才敢来写第一篇博客的

我测试的代码:

主函数:

java 代码
  1. package luceneTest;   
  2.   
  3. import java.io.File;   
  4. import java.sql.Connection;   
  5. import java.sql.PreparedStatement;   
  6. import java.sql.ResultSet;   
  7. import java.sql.SQLException;   
  8. import java.sql.Statement;   
  9.   
  10. import dataConnectionPool.DataConnectionPool;   
  11.   
  12. public class TMain {   
  13.     private String find = " select id,name from author";   
  14.     private Connection con;   
  15.     private Statement stmt;   
  16.     private File indexFile;   
  17.     private int count ;   
  18.     public TMain(File indexFile)   
  19.     {   
  20.         this.indexFile = indexFile;   
  21.         this.count = countD();   
  22.     }   
  23.     public int countD()   
  24.     {      
  25.         int count=0;   
  26.         try {   
  27.             con = DataConnectionPool.getBasicDataSource().getConnection();   
  28.             stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);   
  29.             ResultSet rs  = stmt.executeQuery(find);   
  30.             while(rs.next())   
  31.            {   
  32.                   rs.last(); // 移动到最后一行    
  33.                   count = rs.getRow(); // 获得当前行号:此处即为最大记录数    
  34.            }    
  35.                
  36.             System.out.println(" 表的总记录数 :"+count);   
  37.         } catch (SQLException e) {   
  38.             // TODO Auto-generated catch block   
  39.             e.printStackTrace();   
  40.         }   
  41.         return count;   
  42.     }   
  43.     public void create() throws InterruptedException   
  44.     {   
  45.         int num = count/10;   
  46.            
  47.         for(int i = 1;i<=num;i++)   
  48.         {   
  49.             System.out.println("main start :"+((i-1)*10+1));   
  50.             CDataIndex cd = new CDataIndex(((i-1)*10+1),10,indexFile);   
  51.   
  52.                 cd.start();   
  53.                
  54.        
  55.         }      
  56.     }   
  57.     public void print(File indexFile, String markInfo)   
  58.     {   
  59.         FData fd = new FData(indexFile,markInfo);   
  60.         fd.findData();   
  61.     }   
  62.        
  63.     public static void main(String[] args) {   
  64.         // TODO Auto-generated method stub   
  65.      File indexFile = new File("D:/lucene/index");   
  66.      String markInfo = "you";   
  67.      TMain tm = new TMain(indexFile);   
  68.       try {   
  69.         tm.create();   
  70.           tm.print(indexFile, markInfo);   
  71.     } catch (InterruptedException e) {   
  72.         // TODO Auto-generated catch block   
  73.         e.printStackTrace();   
  74.     }       
  75.     }   
  76.   
  77. }   

 

调用一个建立索引的类:

java 代码
  1. package luceneTest;   
  2.   
  3. import java.io.File;   
  4. import java.io.IOException;   
  5. import java.sql.Connection;   
  6. import java.sql.PreparedStatement;   
  7. import java.sql.ResultSet;   
  8. import java.sql.SQLException;   
  9. import java.util.Date;   
  10.   
  11. import org.apache.lucene.analysis.cjk.CJKAnalyzer;   
  12. import org.apache.lucene.document.Document;   
  13. import org.apache.lucene.document.Field;   
  14. import org.apache.lucene.index.IndexReader;   
  15. import org.apache.lucene.index.IndexWriter;   
  16. import org.apache.lucene.store.FSDirectory;   
  17.   
  18. import dataConnectionPool.DataConnectionPool;   
  19.   
  20. public class CDataIndex extends Thread {   
  21.     private Connection con;   
  22.     private String find = " select id,name from author";   
  23.     private PreparedStatement ps;   
  24.     private int start = 0;   
  25.     private int num = 0;   
  26.     private File indexFile;   
  27.     private boolean mark = false;    //通过这个来控制是用增量索引还是全局索引
  28.     public CDataIndex(int start,int num,File indexFile)   
  29.     {   
  30.         this.start = start;   
  31.         this.num = num;   
  32.         this.indexFile = indexFile;   
  33.     }   
  34.        
  35.        
  36.        
  37.     public void run()   
  38.     {   
  39.            
  40.         System.out.println(" 开始索引!");   
  41.         Date beginDate = new Date();               
  42.                
  43.             try {   
  44.                 FSDirectory fsd =  FSDirectory.getDirectory(indexFile.getAbsolutePath(), mark);   
  45.                    if(IndexReader.isLocked(fsd)){      //这   
  46.                        IndexReader.unlock(fsd);;   
  47.                       }   
  48.             IndexWriter writerF = new IndexWriter(fsd,new CJKAnalyzer());   
  49. //          RAMDirectory ramD = new RAMDirectory();   
  50. //          IndexWriter writerR = new IndexWriter(ramD,new StandardAnalyzer());   
  51.                
  52.                
  53.             con = DataConnectionPool.getBasicDataSource().getConnection();   
  54.             ps = con.prepareStatement(find,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);   
  55.             ResultSet rs  = ps.executeQuery();   
  56.             System.out.println(" start :"+start);   
  57.             rs.absolute(start);   
  58.             rs.previous();   
  59.             while(rs.next()&&num!=0)   
  60.             {   
  61.                 Document doc = new Document();   
  62.                 doc.add(new Field("id",rs.getString(1),Field.Store.YES, Field.Index.UN_TOKENIZED));   
  63.                 doc.add(new Field("content",rs.getString(2), Field.Store.YES,Field.Index.TOKENIZED ));   
  64.                 //writerR.optimize();   
  65.                 //writerR.addDocument(doc); 
  66.                 writerF.optimize(); 
  67.                 writerF.addDocument(doc);   
  68.                 num--;   
  69.             }   
  70. //          writerR.optimize();   
  71. //          writerR.close();           
  72.             writerF.close();               
  73. //          writerF.addIndexes(new Directory[]{ramD});   
  74.             Date endDate = new Date();   
  75.   
  76.             System.out.println("索引耗去的时间(毫秒) :"  
  77.                     + (endDate.getTime() - beginDate.getTime()));   
  78.                
  79.         } catch (IOException e) {   
  80.             // TODO Auto-generated catch block   
  81.             e.printStackTrace();   
  82.         } catch (SQLException e) {   
  83.             // TODO Auto-generated catch block   
  84.             e.printStackTrace();   
  85.         }   
  86.   
  87.     }   
  88.   
  89. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值