更新时间:2023-08-18 GMT+08:00
分享

使用Scan读取数据

功能介绍

要从表中读取数据,首先需要实例化该表对应的Table实例,然后创建一个Scan对象,并针对查询条件设置Scan对象的参数值,为了提高查询效率,最好指定StartRow和StopRow。查询结果的多行数据保存在ResultScanner对象中,每行数据以Result对象形式存储,Result中存储了多个Cell。

代码样例

  • 不指定HOT_ONLY参数来查询数据。在这种情况下,将会查询冷存储中的数据。
    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.");
    } 
  • 通过指定HOT_ONLY参数来查询数据。在这种情况下,只会查询热存储中的数据。
    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.");
    } 
分享:

    相关文档

    相关产品