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

上一關 < 第三關 > 下一關

範例三: 取得 Column 的值

$ bin/hadoop jar ItriMenu.jar GetColumn "ex1Table" "row221" "Detail" "c1"
package itri;

import java.io.IOException;

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

public class GetColumn {

  static String getColumn(String tablename, String row, String family,
      String column) {
    HBaseConfiguration conf = new HBaseConfiguration();
    String ret = "";

    HTable table;
    try {
      table = new HTable(conf, Bytes.toBytes(tablename));
      Get g = new Get(Bytes.toBytes(row));
      Result rowResult = table.get(g);
      ret = Bytes.toString(rowResult.getValue(Bytes.toBytes(family + ":"
          + column)));

      table.close();
    } catch (IOException e) {

      e.printStackTrace();
    }

    return ret;
  }

  public static void main(String[] argv) {
//    String[] argc = {"t1","r1","f1","c1"};argv = argc;
    String[] args = new GenericOptionsParser(new Configuration(), argv)
        .getRemainingArgs();
    if (args.length < 4) {
      System.out.println("GetColumn <TableName> <Row> <Family> <Qualifier> ");
      return;
    }
    System.out.println(getColumn(args[0], args[1], args[2], args[3]));
  }
}

  • 執行結果
my value good
Last modified 14 years ago Last modified on Apr 25, 2011, 3:54:50 PM