介绍

scan 命令用于查看HTable数据。使用 scan 命令可以得到表中的数据。它的语法如下:

  1. scan ‘<table name>’


下面的示例演示了如何使用scan命令从表中读取数据。在这里读取的是emp表。

  1. hbase(main):010:0> scan 'emp'
  2. ROW COLUMN+CELL
  3. 1 column=personal data:city, timestamp=1417521848375, value=hyderabad
  4. 1 column=personal data:name, timestamp=1417521785385, value=ramu
  5. 1 column=professional data:designation, timestamp=1417585277,value=manager
  6. 1 column=professional data:salary, timestamp=1417521903862, value=50000
  7. 1 row(s) in 0.0370 seconds


使用Java API扫描

  1. 使用Java API扫描整个表的数据的完整程序如下:
  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;
  3. import org.apache.hadoop.hbase.HBaseConfiguration;
  4. import org.apache.hadoop.hbase.util.Bytes;
  5. import org.apache.hadoop.hbase.client.HTable;
  6. import org.apache.hadoop.hbase.client.Result;
  7. import org.apache.hadoop.hbase.client.ResultScanner;
  8. import org.apache.hadoop.hbase.client.Scan;
  9. public class ScanTable{
  10. public static void main(String args[]) throws IOException{
  11. // Instantiating Configuration class
  12. Configuration config = HBaseConfiguration.create();
  13. // Instantiating HTable class
  14. HTable table = new HTable(config, "emp");
  15. // Instantiating the Scan class
  16. Scan scan = new Scan();
  17. // Scanning the required columns
  18. scan.addColumn(Bytes.toBytes("personal"), Bytes.toBytes("name"));
  19. scan.addColumn(Bytes.toBytes("personal"), Bytes.toBytes("city"));
  20. // Getting the scan result
  21. ResultScanner scanner = table.getScanner(scan);
  22. // Reading values from scan result
  23. for (Result result = scanner.next(); result != null; result = Scanner.next())
  24. System.out.println("Found row : " + result);
  25. //closing the scanner
  26. scanner.close();
  27. }
  28. }
  1. 编译和执行上述程序如下所示。
  1. $javac ScanTable.java
  2. $java ScanTable
  1. 下面列出的是输出:
  1. Found row :
  2. keyvalues={row1/personal:city/1418275612888/Put/vlen=5/mvcc=0,
  3. row1/personal:name/1418035791555/Put/vlen=4/mvcc=0}