推特 阿里云技术文档正文

索引查询示例_HBase Solr 全文引擎_云数据库 HBase 版

admin 阿里云技术文档 2020-02-11 198 0
阿里云服务器优惠

索引查询示例

索引查询示例

本文介绍如何进行各种条件查询索引,返回匹配结果id后,转换为rowkey查询HBase取出最终完整原数据。流程如下:索引访问流程 索引查询示例_HBase Solr 全文引擎_云数据库 HBase 版 阿里云技术文档

客户端准备

本示例展示使用Java客户端SolrJ来操作solr,并使用Java的HBase API访问HBase。

Java项目工程添加依赖如下:

  1. <dependency>
  2. <groupId>org.apache.solr</groupId>
  3. <artifactId>solr-solrj</artifactId>
  4. <version>7.3.1</version>
  5. </dependency>

各种条件查询,获取doc ID

  1. String zkHost = "zk1:2181,zk2:2181,zk3:2181/solr"
  2. CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder(Collections.singletonList(zkHost), Optional.empty()).build();
  3. SolrQuery solrQuery = new SolrQuery("f1_s:val99");
  4. QueryResponse response = client.query(collection, solrQuery);
  5. SolrDocumentList documentList = response.getResults();
  6. for(SolrDocument doc : documentList){
  7. String id = (String)doc.getFieldValue("id");
  8. //do something
  9. }

更多精确、模糊、范围、facet、stats、and/or组合等查询,见 github demo地址

doc ID转换成rowkey

  1. string类型doc id如果默认 index_conf.xml配置中,不指定unique-key-formatter,或者显式指定使用string,如:
    1. <indexer table="solrdemo" unique-key-formatter="string">
    2. ...
    3. </indexer>
    那么,doc id转成rowkey过程如下:
    1. // String id = "xxxx";
    2. org.apache.hadoop.hbase.util.Bytes.toBytes(docId)
  2. hex类型doc id如果默认 index_conf.xml配置中,unique-key-formatter指定使用hex,如:
    1. <indexer table="solrdemo" unique-key-formatter="hex">
    2. ...
    3. </indexer>
    那么,doc id转成rowkey过程如下:
    1. // String id = "xxxx";
    2. org.apache.commons.codec.binary.Hex.decodeHex(id.toCharArray());
    此过程借助 commons-codec-1.9.jar的方法转换。此jar包依赖如下:
    1. <dependency>
    2. <groupId>commons-codec</groupId>
    3. <artifactId>commons-codec</artifactId>
    4. <version>1.9</version>
    5. </dependency>

获取最终数据

最终我们拿根据各种条件查询匹配到的rowkey,如需获取这个rowkey的完整数据, 只要进行HBase的 get操作即可。HBase的查询访问支持Java api原生方式 ,详见参考; 也可以通过thrift支持c#、python、go等多语言, 详见参考

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

评论

-----