wiki:NCHCCloudCourse100929_4_HBEX3

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

--

HBase 進階課程
程式範例練習

上一關 < 第三關 > 下一關

範例三: 取得 Column 的值

$ bin/hadoop jar TCRCExample.jar GetColumn "ex1Table" "row221" "Detail" "c1"
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.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) {
      // eclipse only
//      String[] arg = {"ex1Table", "row221", "Detail", "c1"};
//      argv = arg;

    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