首页
技术博客
PHP教程
数据库技术
前端开发
HTML5
Nginx
php论坛
新用户注册
|
会员登录
PHP教程
技术博客
编程问答
PNG素材
编程语言
前端技术
Android
PHP教程
HTML5教程
数据库
Linux技术
Nginx技术
PHP安全
WebSerer
职场攻略
JavaScript
开放平台
业界资讯
大话程序猿
登录
极速注册
取消
热门标签 | HotTags
request
controller
regex
php
range
express
hashset
format
join
default
web
hashtable
vba
callback
search
char
hashcode
import
frameworks
buffer
usb
jar
typescript
spring
httpclient
config
ascii
tree
utf-8
filter
subset
python
function
install
emoji
expression
python3
httprequest
io
email
actionscrip
yaml
bitmap
runtime
substring
rsa
iostream
command
solr
flutter
java
web3
md5
get
uri
metadata
cookie
include
go
testing
sum
input
datetime
grid
byte
bash
const
stream
scala
js
shell
erlang
cSharp
jsp
blob
triggers
hash
chat
select
当前位置:
开发笔记
>
编程语言
> 正文
HBase中创建索引
作者:手机用户2502877255 | 来源:互联网 | 2023-05-26 08:29
hbasene(https://github.com/akkumar/hbasene)是开源项目,在hbase存储上封装使用Lucene来创建索引,代码API非常简单
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代码
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
https
git
lucene
api
search
get
int
ci
写下你的评论吧 !
吐个槽吧,看都看了
会员登录
|
用户注册
推荐阅读
import
Java学习笔记之面向对象编程(OOP)
本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ...
[详细]
蜡笔小新 2023-12-13 08:44:30
web
Tomcat/Jetty为何选择扩展线程池而不是使用JDK原生线程池?
本文探讨了Tomcat和Jetty选择扩展线程池而不是使用JDK原生线程池的原因。通过比较IO密集型任务和CPU密集型任务的特点,解释了为何Tomcat和Jetty需要扩展线程池来提高并发度和任务处理速度。同时,介绍了JDK原生线程池的工作流程。 ...
[详细]
蜡笔小新 2023-12-13 16:18:09
import
Postgresql备份和恢复的方法及命令行操作步骤
本文介绍了使用Postgresql进行备份和恢复的方法及命令行操作步骤。通过使用pg_dump命令进行备份,pg_restore命令进行恢复,并设置-h localhost选项,可以完成数据的备份和恢复操作。此外,本文还提供了参考链接以获取更多详细信息。 ...
[详细]
蜡笔小新 2023-12-11 10:17:12
import
多线程补充(一)JVM内存结构 VS Java内存模型 VS Java对象模型
一:Java内存结构参考:https:www.zhihu.comquestion64586462answer576543433内存结构 ...
[详细]
蜡笔小新 2023-10-17 08:58:44
jar
最全的JAVA知识汇总(附讲解和思维导图)
微信公众号:内核小王子关注可了解更多关于数据库,JVM内核相关的知识;如果你有任何疑问也可以加我pigpdong[^1]jvm一行代码是怎么运行的首先,java代码会被编译成字 ...
[详细]
蜡笔小新 2023-10-16 19:56:17
import
消息中间件RabbitMQ 高级特性之消费端ACK与重回队列
什么是消费端的ACK和重回队列?消费端的手工ACK和NACK消费端进行消费的时候,如果由于业务异常我们可以进行日志的记录,然后进行补偿如果由于服务器宕机等严重问题 ...
[详细]
蜡笔小新 2023-10-16 19:29:57
buffer
Java太阳系小游戏分析和源码详解
本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ...
[详细]
蜡笔小新 2023-12-14 19:53:34
web
CSS3选择器的使用方法详解,提高Web开发效率和精准度
本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ...
[详细]
蜡笔小新 2023-12-14 14:37:52
import
求解hdu 1003 java题目的动态规划优化方法
本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ...
[详细]
蜡笔小新 2023-12-14 13:11:00
web
知识图谱——机器大脑中的知识库
本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ...
[详细]
蜡笔小新 2023-12-14 10:06:19
import
关于cuowu类的错误提示和使用AdjustmentListener的问题
本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ...
[详细]
蜡笔小新 2023-12-13 22:09:56
import
scrapy存入excel时,excel文件被反复擦除重写。文件大小始终不超过100k,请问这种情况改如何解决
怀疑是每次都在新建文件,具体代码如下 ...
[详细]
蜡笔小新 2023-12-13 17:53:49
import
sklearn数据集库中的常用数据集类型介绍
本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ...
[详细]
蜡笔小新 2023-12-13 17:45:15
import
java 数据同步 分页_JAVA的数据分页显示
importjava.util.ArrayList;publicclassPageIndex{privateintpageSize;每页要显示的行privateintpageNum ...
[详细]
蜡笔小新 2023-12-13 16:40:13
import
python创建一个窗口_等一个大佬啊 要求用python创建一个窗口,窗口按钮功能是创建一个球体或立方体。明天上课之前交给我...
展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ...
[详细]
蜡笔小新 2023-12-13 16:26:09
手机用户2502877255
这个家伙很懒,什么也没留下!
Tags | 热门标签
request
controller
regex
php
range
express
hashset
format
join
default
web
hashtable
vba
callback
search
char
hashcode
import
frameworks
buffer
usb
jar
typescript
spring
httpclient
config
ascii
tree
utf-8
filter
RankList | 热门文章
1
2018年如何进入区块链行业求职
2
VS2012换肤
3
Docker Toolbox 使用小技巧-复制粘贴-批量处理-循环语句-dm别名
4
关于java并发的面试题
5
2019 MultiUniversity Training Contest 3 1007 Find the answer —— 线段树
6
教你在Centos8中更改时区
7
如何设置excel所有工作表都横向打印?:打印工作表怎么设置
8
实验二逻辑回归算法
9
ajax php上传图片_显示图像上传AJAX / PHP的缩略图
10
《寄李相公》翻译 原文赏析诗人唐白居易
11
JS与OC交互
12
面经系列 | Python,数据结构,神经网络
13
java – 想要从特定日期获取一个月的开始和结束日期
14
js在线更新时间
15
解决DataGridView 冻结列后出现的异常
PHP1.CN | 中国最专业的PHP中文社区 |
DevBox开发工具箱
|
json解析格式化
|
PHP资讯
|
PHP教程
|
数据库技术
|
服务器技术
|
前端开发技术
|
PHP框架
|
开发工具
|
在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved |
京公网安备 11010802041100号
|
京ICP备19059560号-4
| PHP1.CN 第一PHP社区 版权所有