cloudtable
使用HBase Shell进行全文索引
更新时间:2019/08/19 GMT+08:00
本章节为您介绍如何使用HBase Shell给HBase表创建全文索引。
使用前提
已创建表格存储服务集群(HBase)、弹性云服务器(ECS)实例(作为HBase客户端)、云搜索服务集群(Elasticsearch引擎),需要保持VPC、子网和安全组一致,来确保网络通畅。
全文检索示例
- 启动HBase Shell访问CloudTable集群。
安装和启动HBase Shell的具体操作,请参见使用HBase Shell访问集群。
- 在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.index.es.schema' => '[{"name":"email","type":"text","hbaseQualifier":"f:email"}]' }
METADATA中的字段schema定义,请参考HBase Elasticsearch schema定义说明。其中,“hbase.index.es.endpoint”请替换为云搜索服务集群的访问地址。
- 在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'
- 退出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" ] } } ] } }
- 使用如下网站反解析得到元数据在HBase中的rowkey:
在4中返回的rowkey是经过Base64.Encoder编码的,用Base64.Decoder反编码就得到HBase中的rowkey。
- 重新启动HBase Shell,在HBase Shell中,执行get命令获取数据源,命令如下:
get 'hbase-es-table','rowkey'
在Java应用程序开发中,用户通过一步函数调用就可以实现步骤3、4、5的功能,详情请参见《表格存储服务开发指南》的查询数据。
