鍍金池/ 教程/ 大數(shù)據(jù)/ HBase掃描
HBase禁用表
HBase創(chuàng)建表
HBase客戶端API
HBase安裝
HBase表描述和修改
HBase Admin API
HBase掃描
HBase創(chuàng)建數(shù)據(jù)
HBase列出表
HBase刪除數(shù)據(jù)
HBase讀取數(shù)據(jù)
HBase常用命令
HBase更新數(shù)據(jù)
HBase關(guān)閉
HBase架構(gòu)
HBase Shell
HBase Exists
HBase安全
HBase教程
HBase啟用表
HBase計數(shù)和截斷
HBase刪除表

HBase掃描

scan 命令用于查看HTable數(shù)據(jù)。使用 scan 命令可以得到表中的數(shù)據(jù)。它的語法如下:

scan ‘<table name>’ 

下面的示例演示了如何使用scan命令從表中讀取數(shù)據(jù)。在這里讀取的是emp表。

hbase(main):010:0> scan 'emp'

ROW                           COLUMN+CELL

1 column=personal data:city, timestamp=1417521848375, value=hyderabad

1 column=personal data:name, timestamp=1417521785385, value=ramu

1 column=professional data:designation, timestamp=1417585277,value=manager

1 column=professional data:salary, timestamp=1417521903862, value=50000

1 row(s) in 0.0370 seconds

使用Java API掃描

使用Java API掃描整個表的數(shù)據(jù)的完整程序如下:

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();
   }
}

編譯和執(zhí)行上述程序如下所示。

$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}