开发手册 欢迎您!
软件开发者资料库

HBase - 扫描

HBase Scan - 使用此初学者教程,简单易学地学习HBase,其中包含从HBase概述,体系结构,安装,Shell,常规命令,Admin API,创建表,列表,禁用表,启用表,基本到高级知识,描述和更改,存在,删除表,关闭,客户端API,创建数据,更新数据,读取数据,删除数据,扫描,HBase和截断,安全性。

使用HBase Shell扫描

扫描命令用于查看HTable中的数据.使用scan命令,您可以获取表数据.其语法如下:

scan'< table name>'


示例

以下示例说明如何使用scan命令从表中读取数据.这里我们正在阅读 emp 表.

hbase(main):010:0> scan 'emp'ROW                           COLUMN + CELL1 column = personal data:city, timestamp = 1417521848375, value = hyderabad 1 column = personal data:name, timestamp = 1417521785385, value = ramu1 column = professional data:designation, timestamp = 1417585277,value = manager1 column = professional data:salary, timestamp = 1417521903862, value = 500001 row(s) in 0.0370 seconds


使用Java API扫描

扫描整个表的完整程序使用java API的数据如下.

import java.io.IOException;import org.apache.hadoop.conf.Configuration;import org.apache.hadoop.hbase.HBaseConfiguration;import org.apache.hadoop.hbase.util.Bytes;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;public class ScanTable{   public static void main(String args[]) throws IOException{      // Instantiating Configuration class      Configuration config = HBaseConfiguration.create();      // Instantiating HTable class      HTable table = new HTable(config, "emp");      // Instantiating the Scan class      Scan scan = new Scan();      // Scanning the required columns      scan.addColumn(Bytes.toBytes("personal"), Bytes.toBytes("name"));      scan.addColumn(Bytes.toBytes("personal"), Bytes.toBytes("city"));      // Getting the scan result      ResultScanner scanner = table.getScanner(scan);      // Reading values from scan result      for (Result result = scanner.next(); result != null; result = scanner.next())      System.out.println("Found row : " + result);      //closing the scanner      scanner.close();   }}


编译并执行上述程序,如下所示.

$ javac ScanTable.java $ java ScanTable


以下应该是输出:

Found row :keyvalues={row1/personal:city/1418275612888/Put/vlen=5/mvcc=0,row1/personal:name/1418035791555/Put/vlen=4/mvcc=0}