Hbase-scan

提供:Dev Guides
移動先:案内検索

HBase-スキャン

HBase Shellを使用したスキャン

*scan* コマンドは、HTableのデータを表示するために使用されます。 scanコマンドを使用して、テーブルデータを取得できます。 その構文は次のとおりです。
scan ‘<table name>’

次の例は、スキャンコマンドを使用してテーブルからデータを読み取る方法を示しています。 ここで emp テーブルを読んでいます。

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

ROW                           COLUMN &plus; 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を使用してテーブルデータ全体をスキャンする完全なプログラムは次のとおりです。

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}