| 1 | {{{ |
| 2 | #!html |
| 3 | <div style="text-align: center; color:#151B8D"><big style="font-weight: bold;"><big><big> |
| 4 | ITRI HBase 進階課程 |
| 5 | </big></big></big></div> <div style="text-align: center; color:#7E2217"><big style="font-weight: bold;"><big> |
| 6 | HBase 範例 |
| 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 | |
| 21 | package itri; |
| 22 | |
| 23 | import java.io.IOException; |
| 24 | |
| 25 | import org.apache.hadoop.conf.Configuration; |
| 26 | import org.apache.hadoop.hbase.HBaseConfiguration; |
| 27 | import org.apache.hadoop.hbase.client.Get; |
| 28 | import org.apache.hadoop.hbase.client.HTable; |
| 29 | import org.apache.hadoop.hbase.client.Result; |
| 30 | import org.apache.hadoop.hbase.util.Bytes; |
| 31 | import org.apache.hadoop.util.GenericOptionsParser; |
| 32 | |
| 33 | public 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 | {{{ |
| 74 | my value good |
| 75 | }}} |