Changes between Initial Version and Version 1 of waue/2011/0426_4_3


Ignore:
Timestamp:
Apr 25, 2011, 3:54:50 PM (13 years ago)
Author:
waue
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • waue/2011/0426_4_3

    v1 v1  
     1{{{
     2#!html
     3<div style="text-align: center; color:#151B8D"><big style="font-weight: bold;"><big><big>
     4ITRI HBase 進階課程
     5</big></big></big></div> <div style="text-align: center; color:#7E2217"><big style="font-weight: bold;"><big>
     6HBase 範例
     7</big></big></div>
     8}}}
     9
     10[wiki:waue/2011/0426_4_2 上一關 < ] 第三關 [wiki:waue/2011/0426_4_4 > 下一關]
     11
     12 = 範例三: 取得 Column 的值 =
     13
     14{{{
     15$ bin/hadoop jar ItriMenu.jar GetColumn "ex1Table" "row221" "Detail" "c1"
     16}}}
     17
     18{{{
     19#!java
     20
     21package itri;
     22
     23import java.io.IOException;
     24
     25import org.apache.hadoop.conf.Configuration;
     26import org.apache.hadoop.hbase.HBaseConfiguration;
     27import org.apache.hadoop.hbase.client.Get;
     28import org.apache.hadoop.hbase.client.HTable;
     29import org.apache.hadoop.hbase.client.Result;
     30import org.apache.hadoop.hbase.util.Bytes;
     31import org.apache.hadoop.util.GenericOptionsParser;
     32
     33public class GetColumn {
     34
     35        static String getColumn(String tablename, String row, String family,
     36                        String column) {
     37                HBaseConfiguration conf = new HBaseConfiguration();
     38                String ret = "";
     39
     40                HTable table;
     41                try {
     42                        table = new HTable(conf, Bytes.toBytes(tablename));
     43                        Get g = new Get(Bytes.toBytes(row));
     44                        Result rowResult = table.get(g);
     45                        ret = Bytes.toString(rowResult.getValue(Bytes.toBytes(family + ":"
     46                                        + column)));
     47
     48                        table.close();
     49                } catch (IOException e) {
     50
     51                        e.printStackTrace();
     52                }
     53
     54                return ret;
     55        }
     56
     57        public static void main(String[] argv) {
     58//              String[] argc = {"t1","r1","f1","c1"};argv = argc;
     59                String[] args = new GenericOptionsParser(new Configuration(), argv)
     60                                .getRemainingArgs();
     61                if (args.length < 4) {
     62                        System.out.println("GetColumn <TableName> <Row> <Family> <Qualifier> ");
     63                        return;
     64                }
     65                System.out.println(getColumn(args[0], args[1], args[2], args[3]));
     66        }
     67}
     68
     69}}}
     70
     71 * 執行結果
     72
     73{{{
     74my value good
     75}}}