wiki:waue/2011/0426_4_4
ITRI HBase 進階課程
HBase 範例

上一關 < 第四關 > 下一關

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

package itri;

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 ScanTableV1 {

  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) {
//    String[] argc = {"t1", "f1", "c1"};argv = argc;
    
    String[] args = new GenericOptionsParser(new Configuration(), argv)
        .getRemainingArgs();
    if (args.length < 3) {
      System.out
          .println("ScanTableV1 <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 ItriMenu.jar PutData "ex1Table" "row222" "Detail" "c1" "my value better"
$ bin/hadoop jar ItriMenu.jar ScanTable "ex1Table" "Detail" "c1"
row 1 is "my value good"
row 2 is "my value better"

進階: 給 table name 即可掃出全部值

package itri;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.Get;
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;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.util.GenericOptionsParser;

public class ScanTableV2 {

  static void ScanColumn(String tablename) throws IOException {
    HBaseConfiguration conf = new HBaseConfiguration();

    HTable table = new HTable(conf, Bytes.toBytes(tablename));
    Scan s = new Scan();
    ResultScanner scanner = table.getScanner(s);
    try {

      for (Result rr : scanner) {

        byte[] row_b = rr.getRow();
        String row_s = Bytes.toString(row_b);
        System.out.print("row= " +row_s );
        for (KeyValue kv : rr.list()) {

          byte[] family_b = kv.getFamily();
          String family_s = Bytes.toString(family_b);

          byte[] qfy_b = kv.getQualifier();
          String qfy_s = Bytes.toString(qfy_b);

          Get g = new Get(row_b);
          Result rowResult = table.get(g);
          
          String val = Bytes.toString(rowResult.getValue(family_b,
              qfy_b));
          System.out.print(" {"+family_s +":"+qfy_s +" = "+val +"}");
        }
        System.out.println(" ");
      }

    } finally {
      scanner.close();
    }
  }

  public static void main(String[] argv) throws IOException {
//    String[] argc = {"t1"};argv = argc;

    String[] args = new GenericOptionsParser(new Configuration(), argv)
        .getRemainingArgs();
    if (args.length < 1) {
      System.out.println("ScanTableV2 <TableName> ");
      return;
    }
    ScanColumn(args[0]);
  }
}
Last modified 14 years ago Last modified on Apr 25, 2011, 3:55:10 PM