3.7.1 全文检索概述
HBase-Elasticsearch的全文检索能力,是以HBase为基础存储用户源数据,在KV(key value)查询能力的基础上使用云搜索服务(简称CSS)中的Elasticsearch搜索引擎来 补充全文检索能力。用户可以根据自身业务需求来定义HBase中的哪些字段需要全文 检索,在创建HBase表时会自动连接用户指定的云搜索服务集群并在Elasticsearch中创 建索引,索引数据存放在Elasticsearch。同时,HBase的原生API(put和scan接口)支 持索引数据的写入和查询。
使用方法
●
使用HBase Shell进行全文索引
● Java应用程序开发
您可以通过开发HBase Elasticsearch全文检索的应用来实现全文检索的功能,详 情请参见《表格存储服务开发指南》的开发HBase Elasticsearch全文检索应用。
原理
CloudTable作为大数据存储服务,用户数据以Byte类型存储,同时提供高效的kv随机 查询能力。在此基础上,用户根据自身业务需求,自定义schema来指定部分字段的数 据类型(一般是文本类型)来扩展CloudTable的全文检索能力。CloudTable服务是计 算和存储分离,数据存储易扩容和低成本,适合作为海量源数据(任何数据类型)的 主存储系统,云搜索服务(Elasticsearch)保留轻量级的索引数据来支持关键词检索。
如下图所示:
图3-28 原理图
用户创建HBase表时如果定义部分字段开启全文索引,HBase写入数据时会自动同步全
HBase Elasticsearch schema 定义说明
HBase通过表的METADATA来存储Elasticsearch schema的定义:
表3-16 schema 定义
字段名称 value说明 是否
必填 hbase.index.es.enab
led 该HBase表在Elasticsearch中是否创建全文索引,
true表示创建,默认为false。 是 hbase.index.es.endp
oint 云搜索服务集群(Elasticsearch引擎)的访问地
址,例如'ip1:port,ip2:port'。 是 hbase.index.es.index
name HBase表对应在Elasticsearch中的索引名称,必须 小写。
是
hbase.index.es.shar
ds Elasticsearch中索引的分片数量,默认5。取值为大
于等于1的整数。 否
hbase.index.es.replic
as Elasticsearch中的索引的副本数量,默认1。取值为
大于等于0的整数。 否
字段名称 value说明 是否 必填 hbase.index.es.sche
ma HBase和Elasticsearch的字段映射,json数组格式的 字符,每个元素包含以下字段:
● name:Elasticsearch中的字段名称。
● type:Elasticsearch中的字段类型。
● hbaseQualifier:数据源HBase qualifier。
● analyzer:text类型的字段通过“analyzer”可 以指定分词器。中文分词器一般使用
“ik_smart”。默认是“Standard”分词器,支 持英文。
例如:'[ {"name":"contentCh","type":"text","hbaseQualifier":"cf1:conten tCh","analyzer":"ik_smart"},
{"name":"contentEng","type":"text","hbaseQualifier":"cf2:content Eng"},{"name":"id","type":"long","hbaseQualifier":"cf1:id"} ]'
是
HBase-Elasticsearch全文检索当前支持的数据类型有{"text", "long", "integer",
"short", "byte", "double", "float","boolean"},也就是schema中type的取值类型。
text是Elasticsearch中的文本类型。全文检索一般是指对text类型数据的检索,同时也 支持基本数据类型的准确检索。
3.7.2 使用 HBase Shell 进行全文索引
本章节为您介绍如何使用HBase Shell给HBase表创建全文索引。
使用前提
已创建表格存储服务集群(HBase)、弹性云服务器(ECS)实例(作为HBase客户 端)、云搜索服务集群(Elasticsearch引擎),需要保持VPC、子网和安全组一致,来 确保网络通畅。
全文检索示例
步骤1 启动HBase Shell访问CloudTable集群。
安装和启动HBase Shell的具体操作,请参见使用HBase Shell访问集群。
步骤2 在HBase Shell中,执行以下语句创建HBase表:
create 'hbase-es-table', {NAME => 'f', VERSIONS => 5},SPLITS => ['10', '20'], METADATA =>
{'hbase.index.es.enabled' => 'true',
'hbase.index.es.endpoint'=>'10.5.131.1:9200,10.5.131.2:9200','hbase.index.es.indexname'=>'product','hbase.ind ex.es.schema' => '[{"name":"email","type":"text","hbaseQualifier":"f:email"}]' }
METADATA中的字段schema定义,请参考HBase Elasticsearch schema定义说明。
其中,“hbase.index.es.endpoint”请替换为云搜索服务集群的访问地址。
步骤3 在HBase Shell中,执行put命令在HBase表中写入3行数据,命令如下:
put 'hbase-es-table', '001rowkey','f:email','how many apples' put hbase-es-table', '101rowkey','f:email','how much people' put 'hbase-es-table', '201rowkey','f:email','many time people'
步骤4 退出HBase Shell,使用curl命令调用Elasticsearch的全文搜索接口,搜索how关键词,
命令如下:
curl -X GET "${ES_ClusterIP:Port}/product/search" -H 'Content-Type: application/json' -d' { "storedfields" : ["rowkey"], "query" : { "term" : { "email" : "how" } } } '
以上命令中的${ES_Cluster_IP:Port},请替换为云搜索服务集群的访问地址,例如
“10.5.131.1:9200”。
搜索结果命中2个文档(文档是可以编制索引的基本信息单元,以JSON表示),返回 文档的rowkey字段,该rowkey是HBase源数据和Elasticsearch索引数据映射的桥梁,
结果如下:
{ "took":4, "timedout":false, "shards":{ "total":5, "successful":5, "skipped":0, "failed":0 }, "hits":
{ "total":2, "maxscore":0.2876821, "hits":[ { "index":"product", "type":"doc", "id":"GB087WYB7F1t0X-xu3ZX", "score":0.2876821, "fields":{ "rowkey":[ "MDAxcm93a2V5" ] } }, { "index":"product",
"type":"doc", "id":"GR087WYB7F1t0X-xvHZ5", "_score":0.2876821, "fields":{ "rowkey":
[ "MTAxcm93a2V5" ] } } ] } }
步骤5 使用如下网站反解析得到元数据在HBase中的rowkey:
https://www.base64decode.org/
在步骤4中返回的rowkey是经过Base64.Encoder编码的,用Base64.Decoder反编码就 得到HBase中的rowkey。
步骤6 重新启动HBase Shell,在HBase Shell中,执行get命令获取数据源,命令如下:
get 'hbase-es-table','rowkey'
在Java应用程序开发中,用户通过一步函数调用就可以实现步骤步骤3、步骤4、步骤5 的功能,详情请参见《表格存储服务开发指南》的查询数据。
----结束