HBase中创建索引

   hbasene(https://github.com/akkumar/hbasene)是开源项目,在hbase存储上封装使用Lucene来创建索引,代码API非常简单,熟悉lucene的朋友可以很方便地创建。

    以下为测试代码,完成读取一张hbase上记录url和用户id的表,对其创建索引并进行简单的基于url的索引的代码。当取到search的结果后,就可以拿到想要的数据了。由于分词后将原始内容进行了反向索引,所以匹配就转化为了查询,速度相当快。

    其中getDocumentFromHTable为读取一张hbase上己有的表,将url字段提取出来创建content索引。

    创建索引的实质是用了HBaseIndexWriter和HBaseIndexReader两个分别继承自IndexWriter和IndexReader的类来做索引的读取和写入。同时使用了HBaseIndexStore来做存储。

    而创建索引使用的分词等仍然是使用标准的lucene API。

    注意hbasene使用的是hbase-0.20.5,需要修改少量源代码才能运行在0.90.x以上的版本中。

    这里对创建索引表使用到的结构做下简单的说明,因为是lucene入门级水平,所以各位请尽管拍砖讨论。

    索引表由以下几个CF构成:
  • fm.sequence: 记录sequenceId,在执行createLuceneIndexTable时需要写死该CF的row为sequenceId,qulifier为qual.sequence,值为-1。可以不用理会
  • fm.doc2int: DocumentId,每个document都会有一个这样的id,如果Field.Store设置为YES,则能在索引表中查询到该id并得到完整的内容。
  • fm.termVector: 向量偏移,用于模糊查找,记录偏移量等信息
  • fm.termFrequency:分词后的关键词在每个document中出现的频率,qulifier为documentId,value为出现次数
  • fm.fields:记录了content内容,row为documentId,value为document的全文内容,它和fm.docint是相反的,后者是反向索引。
  • fm.payloads:扩展CF,目前还没有用到


Java代码  收藏代码
  1. import java.io.IOException;  
  2.   
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.hbase.HBaseConfiguration;  
  5. import org.apache.hadoop.hbase.client.HTable;  
  6. import org.apache.hadoop.hbase.client.HTablePool;  
  7. import org.apache.hadoop.hbase.client.Result;  
  8. import org.apache.hadoop.hbase.client.ResultScanner;  
  9. import org.apache.hadoop.hbase.client.Scan;  
  10. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  11. import org.apache.lucene.document.Document;  
  12. import org.apache.lucene.document.Field;  
  13. import org.apache.lucene.document.Fieldable;  
  14. import org.apache.lucene.index.IndexReader;  
  15. import org.apache.lucene.index.Term;  
  16. import org.apache.lucene.search.IndexSearcher;  
  17. import org.apache.lucene.search.ScoreDoc;  
  18. import org.apache.lucene.search.TermQuery;  
  19. import org.apache.lucene.search.TopDocs;  
  20. import org.apache.lucene.util.Version;  
  21. import org.hbasene.index.HBaseIndexReader;  
  22. import org.hbasene.index.HBaseIndexStore;  
  23. import org.hbasene.index.HBaseIndexWriter;  
  24.   
  25. public class test{  
  26.       static final String indexName = "myindex";  
  27.       static final String dataName = "t1";  
  28.       public static void main(String[] args) throws IOException {  
  29.           
  30.         try{  
  31.         Configuration conf = HBaseConfiguration.create(); //hbase-site.xml in the classpath  
  32.         conf.set("hbase.rootdir""hdfs://192.168.0.1:9000/hbase");  
  33.         conf.set("hbase.zookeeper.quorum""192.168.0.1,192.168.0.2,192.168.0.3");  
  34.         HTablePool tablePool = new HTablePool(conf, 10);  
  35.         HBaseIndexStore.createLuceneIndexTable(indexName, conf, true);  
  36.         //Write  
  37.         HBaseIndexStore hbaseIndex = new HBaseIndexStore(tablePool, conf, indexName);  
  38.         HBaseIndexWriter writer = new HBaseIndexWriter(hbaseIndex, "content"); //Name of the primary key field.  
  39.   
  40.         getDocument(writer);  
  41.         writer.close();  
  42.           
  43.         //Read/Search  
  44.         IndexReader reader = new HBaseIndexReader(tablePool, indexName, "f");  
  45.         IndexSearcher searcher = new IndexSearcher(reader);  
  46.         Term term = new Term("content""item.taobao.com");  
  47.         TermQuery termQuery = new TermQuery(term);  
  48.         TopDocs docs = searcher.search(termQuery, 3);  
  49.         searcher.close();  
  50.         }catch(IOException e){  
  51.             e.printStackTrace();  
  52.             throw e;  
  53.         }  
  54.       }  
  55.   
  56.   
  57.       private static void getDocument(HBaseIndexWriter writer) throws IOException{  
  58.         Document doc = new Document();  
  59.         doc.add(new Field("content""some content some dog", Field.Store.YES,  
  60.             Field.Index.ANALYZED));  
  61.         writer.addDocument(doc, new StandardAnalyzer(Version.LUCENE_30));  
  62.         doc = new Document();  
  63.         doc.add(new Field("content""some id", Field.Store.NO, Field.Index.ANALYZED));  
  64.         writer.addDocument(doc, new StandardAnalyzer(Version.LUCENE_30));  
  65.         doc = new Document();  
  66.         doc.add(new Field("content""hot dog", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS));  
  67.         writer.addDocument(doc, new StandardAnalyzer(Version.LUCENE_30));  
  68.       }  
  69.         
  70.       private static void getDocumentFromHTable(HTablePool tablePool, HBaseIndexWriter writer) throws IOException  {  
  71.           Document doc = new Document();  
  72.           Scan scan = new Scan();  
  73.           HTable htable = (HTable)tablePool.getTable(dataName);  
  74.           ResultScanner results = htable.getScanner(scan);  
  75.           Result row;  
  76.           while((row = results.next()) != null){  
  77.               doc = new Document();  
  78.               String value = new String(row.getValue("test".getBytes(), null));  
  79.               String url = value.split(""")[2];  
  80.               doc.add(new Field("content", url, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_OFFSETS));  
  81.               writer.addDocument(doc, new StandardAnalyzer(Version.LUCENE_30));  
  82.           }  
  83.         }  
  84. }  


    以下为运行后查看表的中情况:
原文地址:https://www.cnblogs.com/cl1024cl/p/6205186.html