Querying Data
Function Description
HBase uses the native Scan API to support the full-text index, which is similar to that in Reading Data Using Scan. In addition, you need to set HBASE_CLIENT_CONNECTION_IMPL in configuration to org.apache.hadoop.hbase.client.LemonConnectionImplementation and transfer full-text search criteria to the filter ESColumnValueFilter customized by CloudTable.
Sample Code
- In configuration, HBASECLIENTCONNECTION_IMPL is defined as the LemonConnectionImplementation implementation class.
The sample code is as follows:
conf = HBaseConfiguration.create(); conf.set(HConnection.HBASE_CLIENT_CONNECTION_IMPL, "org.apache.hadoop.hbase.client.LemonConnectionImplementation"); Connection conn = ConnectionFactory.createConnection(conf);
- The full-text search criteria are transferred through httpParameters or reqBodyJson of ESColumnValueFilter. The parameter format is the same as the official document of the open source Elasticsearch. For details, see URI Search and Request Body Search.
The following code uses httpParameters for query. (Replace ***replace_with_Chinese_keywords_01*** in code with Chinese keywords.)
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."); }The following code uses reqBodyJson for query. (Replace ***replace_with_Chinese_keywords_02*** in code with Chinese keywords.)
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."); }
Last Article: Writing Data
Next Article: Commissioning Applications
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.