wiki:NCHCCloudCourse100929_4_HBEX4
HBase 進階課程
程式範例練習

上一關 < 第四關 > 下一關

範例四: 掃描並印出指定的column-Qualifier 的所有列值

package org.nchc.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
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;
import org.apache.hadoop.util.GenericOptionsParser;

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) {
    
      // eclipse only
//    String[] argt = {"ex1Table", "Detail", "c1"};
//    argv = argt;  
    
    String[] args = new GenericOptionsParser(new Configuration(), argv)
        .getRemainingArgs();
    if (args.length < 3) {
      System.out
          .println("ScanColumn <TableName> <Family> <Qualifier> ");
      return;
    }
    ScanColumn(args[0], args[1], args[2]);
  }
}

  • 執行結果
Scan the Table [ex1Table]'s Column => Detail:c1
row 1 is "my value good"
$ bin/hadoop jar TCRCExample.jar PutData "ex1Table" "row222" "Detail" "c1" "my value better"
$ bin/hadoop jar TCRCExample.jar ScanTable "ex1Table" "Detail" "c1"
row 1 is "my value good"
row 2 is "my value better"


Last modified 13 years ago Last modified on Apr 24, 2011, 6:21:54 PM