`
lc_koven
  • 浏览: 352574 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

hbase上应用lucene创建索引及检索

阅读更多
    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,目前还没有用到


import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTablePool;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.util.Version;
import org.hbasene.index.HBaseIndexReader;
import org.hbasene.index.HBaseIndexStore;
import org.hbasene.index.HBaseIndexWriter;

public class test{
      static final String indexName = "myindex";
      static final String dataName = "t1";
      public static void main(String[] args) throws IOException {
        
        try{
        Configuration conf = HBaseConfiguration.create(); //hbase-site.xml in the classpath
        conf.set("hbase.rootdir", "hdfs://192.168.0.1:9000/hbase");
        conf.set("hbase.zookeeper.quorum", "192.168.0.1,192.168.0.2,192.168.0.3");
        HTablePool tablePool = new HTablePool(conf, 10);
        HBaseIndexStore.createLuceneIndexTable(indexName, conf, true);
        //Write
        HBaseIndexStore hbaseIndex = new HBaseIndexStore(tablePool, conf, indexName);
        HBaseIndexWriter writer = new HBaseIndexWriter(hbaseIndex, "content"); //Name of the primary key field.

        getDocument(writer);
        writer.close();
        
        //Read/Search
        IndexReader reader = new HBaseIndexReader(tablePool, indexName, "f");
        IndexSearcher searcher = new IndexSearcher(reader);
        Term term = new Term("content", "item.taobao.com");
        TermQuery termQuery = new TermQuery(term);
        TopDocs docs = searcher.search(termQuery, 3);
        searcher.close();
        }catch(IOException e){
            e.printStackTrace();
            throw e;
        }
      }


      private static void getDocument(HBaseIndexWriter writer) throws IOException{
        Document doc = new Document();
        doc.add(new Field("content", "some content some dog", Field.Store.YES,
            Field.Index.ANALYZED));
        writer.addDocument(doc, new StandardAnalyzer(Version.LUCENE_30));
        doc = new Document();
        doc.add(new Field("content", "some id", Field.Store.NO, Field.Index.ANALYZED));
        writer.addDocument(doc, new StandardAnalyzer(Version.LUCENE_30));
        doc = new Document();
        doc.add(new Field("content", "hot dog", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS));
        writer.addDocument(doc, new StandardAnalyzer(Version.LUCENE_30));
      }
      
      private static void getDocumentFromHTable(HTablePool tablePool, HBaseIndexWriter writer) throws IOException  {
          Document doc = new Document();
          Scan scan = new Scan();
          HTable htable = (HTable)tablePool.getTable(dataName);
          ResultScanner results = htable.getScanner(scan);
          Result row;
          while((row = results.next()) != null){
              doc = new Document();
              String value = new String(row.getValue("test".getBytes(), null));
              String url = value.split("\"")[2];
              doc.add(new Field("content", url, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_OFFSETS));
              writer.addDocument(doc, new StandardAnalyzer(Version.LUCENE_30));
          }
        }
}


    以下为运行后查看表的中情况:

  • 大小: 197.4 KB
分享到:
评论
5 楼 xchd 2013-12-29  
你好,可以发跟demo吗?谢谢。我修改源码的错误之后,发现数据不能插入hbase中。谢谢。xchd-3@163.com
4 楼 fncj 2013-02-27  
注意hbasene使用的是hbase-0.20.5,需要修改少量源代码才能运行在0.90.x以上的版本中。
您好,你有修改后的代码吗?能否发我一份?170466123#qq.com
3 楼 mls626 2012-11-07  
请问楼主,如何获取到 hbasene 的 jar 包,官网上给出的源代码有些问题,这是我的邮箱 spring_ssh@163.com 非常感谢
2 楼 cnrainbing 2012-03-31  
楼主你能否提供一下完整的例子,hbasene.jar 我们怎么获取啊,很急我的邮箱rainbing@live.com
1 楼 eryk 2011-08-24  
您好,请问一下如果升级hbase版本到0.90 需要修改什么地方?我的qq:47020113,很急,请联系我,谢谢

相关推荐

    CDH使用Solr实现HBase二级索引.docx

    通过创建 Solr 的分片,我们可以实现 HBase 的二级索引。我们可以使用 solrctl 命令来创建 Solr 的分片。例如,我们可以使用以下命令创建一个名为 ll 的 Solr 分片: solrctl instancedir --generate /opt/solr/ll ...

    HBase应用实践专场HBaseforSolr.pdf

    ### HBase for Solr的实现原理及应用场景 HBase for Solr是一种将HBase与Solr相结合的技术方案,旨在克服HBase在查询方面的限制。其核心思想是通过在Solr中建立索引来增强HBase的检索能力。具体来说,HBase中的数据...

    hbase+Coprocesser+solr实现二级索引

    Coprocessor是HBase提供的一个强大特性,允许我们在RegionServer上运行自定义的代码,比如实现特定的索引逻辑。Coprocessor可以在数据写入时同步更新二级索引,确保索引与数据的一致性。在“hbase-increment-index-...

    Elasticsearch-HBase-replication同步

    3. **定义数据映射**: 在Elasticsearch中创建索引,并定义映射(Mapping),这将决定如何将HBase的数据结构转换为Elasticsearch的文档格式。 4. **启动Replication**: 开启River,设定同步频率或触发条件,可以是...

    morphlines.confmorphline-hbase-mapper.xml

    当我们在HBase上构建二级索引时,通常会利用Solr来提升查询性能,特别是对于那些需要进行复杂查询和全文搜索的应用场景。 标题提到的"morphlines.conf"和"morphline-hbase-mapper.xml"是这两个关键步骤中的配置文件...

    \Lucene Nutch和安装说明文旦

    3. **使用示例**: 提供使用这些资源的实际操作例子,如创建索引、运行查询等。 4. **故障排查**: 针对常见问题和错误提供解决方案,帮助用户顺利进行安装和配置。 **总结** Lucene和Nutch是两个强大的开源搜索和...

    hbase-elasticsearch:从 hbase 创建弹性搜索索引的脚本

    5. **创建索引**:在Elasticsearch中创建一个新的索引,定义好映射(mapping),这会决定数据的存储方式和搜索行为。 6. **批量插入数据**:为了提高效率,通常会将从HBase读取的数据批量插入到Elasticsearch。这...

    基于cdh5.7.5集群的solr(4.10.3-cdh5.7.5)+hbase(1.2.0-cdh5.7.5)构建二级索引方案

    3. **创建Solr集合**:在Solr中创建一个与HBase表对应的集合,定义索引字段,包括要索引的HBase列族和列。 4. **集成HBase和Solr**:配置HBase的`hbase-site.xml`,启用HBase-Solr连接器(如`hbase.indexer.solr....

    lucene-inmemory-index:使用Lucene在内存中搜索索引

    要在Java环境中使用Lucene创建内存索引,首先需要引入Lucene的依赖库。在项目中添加对应的Maven或Gradle依赖,确保能够访问到Lucene的核心组件和分析器。 接着,我们创建一个`Directory`对象,这是Lucene存储索引的...

    第1章 HBase入门_v1.3.docx

    - **发展历程**:HBase 最初由Apache Lucene的创建者Doug Cutting提出,自2007年以来,HBase已经成为Apache顶级项目,并得到了广泛的应用和发展。 - **特点**: - **高可靠性**:通过多副本机制确保数据安全。 - *...

    EleasticSearch基本原理及架构.pdf

    我们需要考虑以下几个关键点:选择适合的数据库(如MySQL、Sybase、Oracle、MongoDB、HBase等)、解决单点故障(通过负载均衡器、Zookeeper等)、确保数据安全性(热备、冷备、异地多活)、处理检索难题(数据库代理...

    Nutch 安装ppt

    1. **创建索引**:使用 Lucene API 创建索引。 2. **查询索引**:使用 Lucene 提供的查询 API 进行文本搜索。 ### 总结 本文介绍了 Nutch 的基本概念、架构以及如何在 Eclipse 中导入和配置 Nutch。同时,还探讨了...

    类似google搜索功能实现

    4. **索引**:创建倒排索引是搜索引擎的关键步骤。倒排索引是一种数据结构,它将每个单词映射到包含该单词的文档列表。这样,当用户输入查询时,搜索引擎可以通过索引快速找到相关文档。Apache Lucene和Elastic...

    elasticsearch环境部署测试

    - 示例命令: 创建索引库 ```bash curl -X PUT http://192.168.100.11:9200/bjsxt/ ``` - 修改配置 ```bash curl -X PUT 'http://192.168.239.3:9200/test2/' -d '{"settings":{"number_of_replicas":2}}' ``...

    Google.zip_google

    Java 中的 Apache Lucene 和 Solr 提供了这些功能,它们是强大的全文检索库。 4. **索引构建(Indexing)**:将预处理后的文本构建为倒排索引,以便快速查找关键词。Lucene 和 Solr 内置了高效的索引机制。 5. **...

    分享一个Nutch入门学习的资料

    5. **分词和索引**:Nutch 使用Apache Lucene或Solr进行文本分析和索引,以便于后续的搜索查询。 6. **存储数据**:所有抓取的页面和索引信息会被存储在Hadoop的HDFS中。 7. **搜索**:最后,可以使用Nutch的搜索...

    搜索引擎的设计与实现

    搜索引擎是互联网上不可或缺的信息检索工具,它通过爬取、索引和检索大量网页来帮助用户找到所需的信息。本文将深入探讨基于JAVA实现的新闻搜索引擎的设计与实现,以及网络蜘蛛(Web Crawler)的关键技术。 首先,...

Global site tag (gtag.js) - Google Analytics