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,目前还没有用到
- 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));
- }
- }
- }
以下为运行后查看表的中情况:
HBase中创建索引的更多相关文章
- elasticsearch kabana中创建索引
在kabana中创建索引和索引类型语法 PUT clockin{ "mappings": { "time": { } }} 查询索引下的所有数据 GET clo ...
- Mysql 中创建索引和索引的使用问题
在数据库中合理的使用索引是提升mysql数据库的一种高效和快捷的方式,但是在索引的使用上在我的使用中发现有很多坑,因为自己之前没有认识到,所以来总结一下 索引的介绍 索引是一种特殊的文件,其中包含着对 ...
- lucene中创建索引库
package com.hope.lucene;import org.apache.commons.io.FileUtils;import org.apache.lucene.document.Doc ...
- 在SqlServer和Oralce中创建索引
给表名A的字段A增加索引 SqlServer: if exists (select 1 from sysobjects where name='表名A' and type='u')and exists ...
- MySql SqlServer Sqlite中关于索引的创建
最近要更新Cocon90.Db库,令其ORM创建表时实现索引的添加.因此总结下列常用Sql,供大家学习与参考. 一.SqlServer中创建索引可以这样: ) Create Table Test ( ...
- hive中的索引创建
1.在hive中创建索引所在表 create table if not exists h_odse.hxy(id int,name string,hobby array<string>,a ...
- SQL语句-创建索引
语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名)WITH FILLFACTOR = 填充因子值0~100 GO USE 库名GO IF EXISTS (SELECT * FRO ...
- SQL Server创建索引(转)
什么是索引 拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQL Server中的数据记录也是按页存放的,每页容量一般为4K .为了加快查找的速度,汉语字(词)典一般都有按拼音. ...
- MySQL(五) MySQL中的索引详讲
序言 之前写到MySQL对表的增删改查(查询最为重要)后,就感觉MySQL就差不多学完了,没有想继续学下去的心态了,原因可能是由于别人的影响,觉得对于MySQL来说,知道了一些复杂的查询,就够了,但是 ...
随机推荐
- HDFS:NameNode、DataNode、SecondaryNameNode
可以一句话描述 HDFS:把客户端的大文件存放在很多节点的数据块中. HDFS设计原则: 1,文件以块(block)方式存储: 2,通过副本机制提高可靠度和读取吞吐量: 3,每个区块至少分到三台Dat ...
- [struts2学习笔记] 第六节 struts2依赖的jar包还有Could not find action or result 错误解决
本文地址:http://blog.csdn.net/sushengmiyan/article/details/43272061 本文作者:sushengmiyan ------------------ ...
- memcached实战系列(五)Memcached: List all keys 查询所有的key
memcached可能当时设计的时候就把它定位为内存性的kv结构的缓存系统.所以没有持久化到磁盘的命令,也没有查看所有key的值得命令.可能觉得没必要吧,你要是缓存1个G内存的数据,自己都头大,还敢看 ...
- Servlet之cookie处理
Cookies 通常设置在 HTTP 头信息中(虽然JavaScript 也可以直接在浏览器上设置一个 Cookie).设置 Cookie 的 Servlet 会发送如下的头信息: HTTP/1.1 ...
- 剑指Offer——网易笔试之解救小易——曼哈顿距离的典型应用
剑指Offer--网易笔试之解救小易--曼哈顿距离的典型应用 前言 首先介绍一下曼哈顿,曼哈顿是一个极为繁华的街区,高楼林立,街道纵横,从A地点到达B地点没有直线路径,必须绕道,而且至少要经C地点,走 ...
- ios7内购、Game Center 实现 in-App Purchases & Game Center
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=514 昨天使用ios7SDK b ...
- J2EE学习从菜鸟变大鸟之六 EJB(Enterprise JavaBean)企业级Java组件
EJB (EnterpriseJavaBean)是J2EE的一部分,定义了一个用于开发基于组件的企业多重应用程序的标准.其特点包括网络服务支持和核心开发工具(SDK).其称为Java 企业Bean,是 ...
- iOS开发之六:常用控件--UIImageView的使用
UIImageView是我们做iOS开发用的非常多的一个控件,IOS中的各种图片,包括头像,有的背景图片等基本都要用到这个控件. 1.常用的属性以及方法 <span style="fo ...
- 精通CSS+DIV网页样式与布局--制作实用菜单
在上篇博文中,小编中主要的简单总结了一下CSS中关于如何设置页面和浏览器元素,今天小编继续将来介绍CSS的相关基础知识,这篇博文,小编主要简单的总结一下在CSS中如何制作网页中的菜单,这部分的内容包括 ...
- SendMessageUpwards定义简单按钮(Unity3D开发之十)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=582 SendMessageUp ...