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

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

HBase中创建索引的更多相关文章

  1. elasticsearch kabana中创建索引

    在kabana中创建索引和索引类型语法 PUT clockin{ "mappings": { "time": { } }} 查询索引下的所有数据 GET clo ...

  2. Mysql 中创建索引和索引的使用问题

    在数据库中合理的使用索引是提升mysql数据库的一种高效和快捷的方式,但是在索引的使用上在我的使用中发现有很多坑,因为自己之前没有认识到,所以来总结一下 索引的介绍 索引是一种特殊的文件,其中包含着对 ...

  3. lucene中创建索引库

    package com.hope.lucene;import org.apache.commons.io.FileUtils;import org.apache.lucene.document.Doc ...

  4. 在SqlServer和Oralce中创建索引

    给表名A的字段A增加索引 SqlServer: if exists (select 1 from sysobjects where name='表名A' and type='u')and exists ...

  5. MySql SqlServer Sqlite中关于索引的创建

    最近要更新Cocon90.Db库,令其ORM创建表时实现索引的添加.因此总结下列常用Sql,供大家学习与参考. 一.SqlServer中创建索引可以这样: ) Create Table Test ( ...

  6. hive中的索引创建

    1.在hive中创建索引所在表 create table if not exists h_odse.hxy(id int,name string,hobby array<string>,a ...

  7. SQL语句-创建索引

    语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100 GO USE 库名GO IF EXISTS (SELECT * FRO ...

  8. SQL Server创建索引(转)

    什么是索引 拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQL Server中的数据记录也是按页存放的,每页容量一般为4K .为了加快查找的速度,汉语字(词)典一般都有按拼音. ...

  9. MySQL(五) MySQL中的索引详讲

    序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于MySQL来说,知道了一些复杂的查询,就够了,但是 ...

随机推荐

  1. 【Netty源码分析】客户端connect服务端过程

    上一篇博客[Netty源码分析]Netty服务端bind端口过程 我们介绍了服务端绑定端口的过程,这一篇博客我们介绍一下客户端连接服务端的过程. ChannelFuture future = boos ...

  2. 2.2、Android Studio通过注解提升代码检测

    使用像Lint这样的代码检测工具可以帮助你发现问题和提升代码,但是代码检测在有些地方很难应用.例如,Android的资源ID,使用一个int类型来表示字符.图像.颜色或者其他资源类型所以代码检测工具不 ...

  3. 剑指Offer——栈的java实现和栈的应用举例

    剑指Offer--栈的java实现和栈的应用举例 栈是一种先进后出的数据结构, 栈的实现如下: 首先定义了栈需要实现的接口: public interface MyStack<T> { / ...

  4. Axure实现淡入淡出效果

    小伙伴们有可能在各大网站看到淡入淡出效果的动画,比如淘宝.京东,淘宝每天会把各种打折促销.今日推荐.限时抢购等做成淡入淡入或者向右活动等类似翻页的效果放在首页,吸引顾客的眼球,那么如何使用Axure来 ...

  5. java 之容器

    在Java中,我们想要保存对象可以使用很多种手段.我们之前了解过的数组就是其中之一.但是数组具有固定的尺寸,而通常来说,程序总是在运行时根据条件来创建对象,我们无法预知将要创建对象的个数以及类型,所以 ...

  6. 03 SeekBar 音频播放拖拽进度条

    八,  SeekBar  音频播放拖拽进度条       >                 android:progress="40"   第一进度         and ...

  7. [GitHub]第八讲:GitHub Pages

    Github Pages 是 github 公司提供的免费的静态网站托管服务,用起来方便而且功能强大,不仅没有空间限制,还可以绑定自己的域名.在 https://pages.github.com/ 首 ...

  8. 纯CSS箭头,气泡

    原文地址: CSS Triangles 演示地址:CSS Triangles Demo 原文日期: 2013年8月5日 翻译日期: 2013年8月9日 本文两种实现方式: 使用或不使用 before ...

  9. (六十四)iOS的socket实现(C+OC混合实现)

    对于微博.微信朋友圈之类的网络通信,使用JSON完全可以满足需求,但是如果要制作网络游戏,就需要建立一个持久连接,这时候就要考虑使用socket. 在iOS上实现socket大体有两种方法,一是借助自 ...

  10. 小强的HTML5移动开发之路(17)——HTML5内联SVG

    来自:http://blog.csdn.net/dawanganban/article/details/18189181 一.什么是SVG 可缩放矢量图形是基于可扩展标记语言(标准通用标记语言的子集) ...