wiki:waue/2010/0204-04

Version 4 (modified by waue, 15 years ago) (diff)

--

範例四: Scan all Column

執行方法

測試用打包檔 tsmcHBase_100204.jar

$ /opt/hadoop/bin/hadoop jar tsmcHBase_100204.jar ScanColumn

程式碼

package tsmc;

import java.io.IOException;
import org.apache.hadoop.hbase.HBaseConfiguration;
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.util.Bytes;

public class ScanTable {

  static void ScanColumn(String tablename, String family, String column) {
    HBaseConfiguration conf = new HBaseConfiguration();

    HTable table;
    try {
      table = new HTable(conf, Bytes.toBytes(tablename));

      ResultScanner scanner = table.getScanner(Bytes.toBytes(family));

      System.out.println("Scan the Table [" + tablename
          + "]'s Column => " + family + ":" + column);
      int i = 1;
      for (Result rowResult : scanner) {
        byte[] by = rowResult.getValue(Bytes.toBytes(family), Bytes
            .toBytes(column));
        String str = Bytes.toString(by);
        System.out.println("row " + i + " is \"" + str + "\"");
        i++;
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] argv) {
    ScanColumn("tsmc", "Products", "P2");
  }
}