Updated on 2025-07-25 GMT+08:00

Reading HBase Cold and Hot Separation Data Using the Scan Command

Function Description

Before reading data from a table, instantiate the Table instance of the table, and then create a Scan object and set parameters for the Scan object based on search criteria. To improve query efficiency, you are advised to specify StartRow and StopRow. Query results are stored in the ResultScanner object, where each row of data is stored as a Result object that stores multiple Cells.

Sample Code

  • The query that does not contain the HOT_ONLY hint may hit cold data.
    public void testScanData() {
      LOG.info("Entering testScanData.");
      Table table = null; 
      // Instantiate a ResultScanner object.
      ResultScanner rScanner = null;
      try {
        // Create the Configuration instance.
        table = conn.getTable(tableName);
        // Instantiate a Get object.
        Scan scan = new Scan();
        byte[] startRow = Bytes.toBytes(Shenzhen#Longgang#2017/7/1 00:00:00);  
        byte[] stopRow = Bytes.toBytes(Shenzhen#Longgang#2017/7/3 00:00:00);
        scan.setStartRow(startRow);  
        scan.setStopRow(stopRow);
        scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("temp"));
        // Set the cache size.
        scan.setCaching(1000);
        // 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(Bytes.toString(CellUtil.cloneRow(cell)) + ":"
                + Bytes.toString(CellUtil.cloneFamily(cell)) + ","
                + Bytes.toString(CellUtil.cloneQualifier(cell)) + ","
                + Bytes.toString(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 testScanData.");
    } 
  • The query that contains the HOT_ONLY hint hits only hot data.
    public void testScanData() {
      LOG.info("Entering testScanData.");
      Table table = null; 
      // Instantiate a ResultScanner object.
      ResultScanner rScanner = null;
      try {
        // Create the Configuration instance.
        table = conn.getTable(tableName);
        // Instantiate a Get object.
        Scan scan = new Scan();
        byte[] startRow = Bytes.toBytes(Shenzhen#Longgang#2017/7/1 00:00:00);  
        byte[] stopRow = Bytes.toBytes(Shenzhen#Longgang#2017/7/3 00:00:00);
        scan.setStartRow(startRow);  
        scan.setStopRow(stopRow);
        scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("temp"));
    
        // Set HOT_ONLY.
        scan.setAttribute(HBaseConstants.HOT_ONLY, Bytes.toBytes(true));
        // Set the cache size.
        scan.setCaching(1000);
        // 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(Bytes.toString(CellUtil.cloneRow(cell)) + ":"
                + Bytes.toString(CellUtil.cloneFamily(cell)) + ","
                + Bytes.toString(CellUtil.cloneQualifier(cell)) + ","
                + Bytes.toString(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 testScanData.");
    }