文档首页 > > 开发指南> 开发HBase Elasticsearch全文检索应用> 样例代码说明> 查询数据

查询数据

分享
更新时间:2020/06/17 GMT+08:00

功能简介

HBase通过原生的scan API来支持全文索引能力,同使用Scan读取数据类似。在此基础上,需要在configuration中配置HBASE_CLIENT_CONNECTION_IMPL为"org.apache.hadoop.hbase.client.LemonConnectionImplementation",同时将全文检索条件传入CloudTable定制好的过滤器ESColumnValueFilter。

样例代码

  1. configuration定义HBASECLIENTCONNECTION_IMPL为LemonConnectionImplementation实现类。

    样例代码如下:

    conf = HBaseConfiguration.create();
    conf.set(HConnection.HBASE_CLIENT_CONNECTION_IMPL, "org.apache.hadoop.hbase.client.LemonConnectionImplementation");
    Connection conn = ConnectionFactory.createConnection(conf);
  2. 全文检索条件通过ESColumnValueFilter的httpParameters或reqBodyJson传入,参数格式和开源Elasticsearch的官方文档一致,请分别参见URI SearchRequest Body Search

    如下样例代码通过httpParameters来查询(请将代码中的字符串“***replace_with_Chinese_keywords_01***”替换为中文检索词):

      public void testScanDataWithES1() {
        LOG.info("Entering testScanDataWithES1.");
    
        Table table = null;
        // Instantiate a ResultScanner object.
        ResultScanner rScanner = null;
        try {
          // Create the Configuration instance.
          table = conn.getTable(tableName);
          assert table instanceof LemonWrapperHTable;
    
          // set specified qualifier.
          Scan scan = new Scan();
          scan.addColumn(CF1, QUA_ARTICLE_CONTENT_CHINESE);
          scan.addColumn(CF2, QUA_ARTICLE_CONTENT_English);
          scan.addColumn(CF1, QUA_ARTICLE_ID);
    
          // Set the cache size.
          scan.setCaching(1000);
    
          // with special filter
          Map<String, String> httpParameters = new HashMap<>();
          httpParameters.put("q", "contentCh:***replace_with_Chinese_keywords_01***");
          List<String> indexNames = new ArrayList();
          indexNames.add(ES_INDEX_NAME);
          ESColumnValueFilter filter = new ESColumnValueFilter(indexNames, httpParameters, "");
          scan.setFilter(filter);
    
          // Submit a scan request.
          rScanner = table.getScanner(scan);
    
          // Print query results.
          for (Result r = rScanner.next(); r != null; r = rScanner.next()) {
            for (Cell cell : r.rawCells()) {
              LOG.info("testScanDataWithES1 by text type condition, scan result:" + Bytes.toString(CellUtil.cloneRow(cell)) + ":"
                + Bytes.toString(CellUtil.cloneFamily(cell)) + ","
                + Bytes.toString(CellUtil.cloneQualifier(cell)) + ","
                + convertCellValue(Bytes.toString(CellUtil.cloneQualifier(cell)), CellUtil.cloneValue(cell)));
            }
          }
    
          LOG.info("Scan data successfully.");
        } catch (IOException e) {
          LOG.error("Scan data failed ", e);
        } finally {
          if (rScanner != null) {
            // Close the scanner object.
            rScanner.close();
          }
          if (table != null) {
            try {
              // Close the HTable object.
              table.close();
            } catch (IOException e) {
              LOG.error("Close table failed ", e);
            }
          }
        }
        LOG.info("Exiting testScanDataWithES1.");
      }

    如下样例代码通过reqBodyJson来查询(请将代码中的字符串“***replace_with_Chinese_keywords_02***”替换为中文检索词):

      public void testScanDataWithES2() {
        LOG.info("Entering testScanDataWithES2.");
    
        Table table = null;
        // Instantiate a ResultScanner object.
        ResultScanner rScanner = null;
        try {
          // Create the Configuration instance.
          table = conn.getTable(tableName);
          assert table instanceof LemonWrapperHTable;
    
          // set specified qualifier.
          Scan scan = new Scan();
          scan.addColumn(CF1, QUA_ARTICLE_CONTENT_CHINESE);
          scan.addColumn(CF2, QUA_ARTICLE_CONTENT_English);
          scan.addColumn(CF1, QUA_ARTICLE_ID);
    
          // Set the cache size.
          scan.setCaching(1000);
    
          // with special filter
          Map<String, String> httpParameters = Collections.emptyMap();
          List<String> indexNames = new ArrayList();
          indexNames.add(ES_INDEX_NAME);
          String reqBodyJson =
            "{" +
              "    \"query\" : {" +
              "        \"term\" : { \"contentCh\" : \"***replace_with_Chinese_keywords_02***\" }" +
              "    }" +
              "}";
          ESColumnValueFilter filter = new ESColumnValueFilter(indexNames, httpParameters, reqBodyJson);
          scan.setFilter(filter);
    
          // Submit a scan request.
          rScanner = table.getScanner(scan);
    
          // Print query results.
          for (Result r = rScanner.next(); r != null; r = rScanner.next()) {
            for (Cell cell : r.rawCells()) {
              LOG.info("testScanDataWithES2 by text type condition, scan result:" + Bytes.toString(CellUtil.cloneRow(cell)) + ":"
                + Bytes.toString(CellUtil.cloneFamily(cell)) + ","
                + Bytes.toString(CellUtil.cloneQualifier(cell)) + ","
                + convertCellValue(Bytes.toString(CellUtil.cloneQualifier(cell)), CellUtil.cloneValue(cell)));
            }
          }
    
          LOG.info("Scan data successfully.");
        } catch (IOException e) {
          LOG.error("Scan data failed ", e);
        } finally {
          if (rScanner != null) {
            // Close the scanner object.
            rScanner.close();
          }
          if (table != null) {
            try {
              // Close the HTable object.
              table.close();
            } catch (IOException e) {
              LOG.error("Close table failed ", e);
            }
          }
        }
    
        LOG.info("Exiting testScanDataWithES2.");
      }
分享:

    相关文档

    相关产品

文档是否有解决您的问题?

提交成功!非常感谢您的反馈,我们会继续努力做到更好!
反馈提交失败,请稍后再试!

*必选

请至少选择或填写一项反馈信息

字符长度不能超过200

提交反馈 取消

如您有其它疑问,您也可以通过华为云社区问答频道来与我们联系探讨

智能客服提问云社区提问