查询数据
功能简介
HBase通过原生的scan API来支持全文索引能力,同使用Scan读取数据类似。在此基础上,需要在configuration中配置HBASE_CLIENT_CONNECTION_IMPL为"org.apache.hadoop.hbase.client.LemonConnectionImplementation",同时将全文检索条件传入CloudTable定制好的过滤器ESColumnValueFilter。
样例代码
- 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);
- 全文检索条件通过ESColumnValueFilter的httpParameters或reqBodyJson传入,参数格式和开源Elasticsearch的官方文档一致,请分别参见URI Search和Request 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."); }
