| 1 | {{{ |
| 2 | #!html |
| 3 | <div style="text-align: center; color:#151B8D"><big style="font-weight: bold;"><big><big> |
| 4 | HBase 進階課程 |
| 5 | </big></big></big></div> <div style="text-align: center; color:#7E2217"><big style="font-weight: bold;"><big> |
| 6 | 程式範例練習 |
| 7 | </big></big></div> |
| 8 | }}} |
| 9 | |
| 10 | [wiki:NCHCCloudCourse100929_4_HBEX2 上一關 < ] 第三關 [wiki:NCHCCloudCourse100929_4_HBEX4 > 下一關] |
| 11 | |
| 12 | = 範例三: Get Column Value = |
| 13 | |
| 14 | {{{ |
| 15 | #!java |
| 16 | package org.nchc.hbase; |
| 17 | |
| 18 | import java.io.IOException; |
| 19 | |
| 20 | import org.apache.hadoop.conf.Configuration; |
| 21 | import org.apache.hadoop.hbase.HBaseConfiguration; |
| 22 | import org.apache.hadoop.hbase.client.Get; |
| 23 | import org.apache.hadoop.hbase.client.HTable; |
| 24 | import org.apache.hadoop.hbase.client.Result; |
| 25 | import org.apache.hadoop.hbase.util.Bytes; |
| 26 | import org.apache.hadoop.util.GenericOptionsParser; |
| 27 | |
| 28 | public class GetColumn { |
| 29 | |
| 30 | static String getColumn(String tablename, String row, String family, |
| 31 | String column) { |
| 32 | HBaseConfiguration conf = new HBaseConfiguration(); |
| 33 | String ret = ""; |
| 34 | |
| 35 | HTable table; |
| 36 | try { |
| 37 | table = new HTable(conf, Bytes.toBytes(tablename)); |
| 38 | Get g = new Get(Bytes.toBytes(row)); |
| 39 | Result rowResult = table.get(g); |
| 40 | ret = Bytes.toString(rowResult.getValue(Bytes.toBytes(family + ":" |
| 41 | + column))); |
| 42 | |
| 43 | table.close(); |
| 44 | } catch (IOException e) { |
| 45 | |
| 46 | e.printStackTrace(); |
| 47 | } |
| 48 | |
| 49 | return ret; |
| 50 | } |
| 51 | |
| 52 | public static void main(String[] argv) { |
| 53 | |
| 54 | String[] args = new GenericOptionsParser(new Configuration(), argv) |
| 55 | .getRemainingArgs(); |
| 56 | if (args.length < 4) { |
| 57 | System.out.println("GetColumn <TableName> <Row> <Family> <Qualifier> "); |
| 58 | return; |
| 59 | } |
| 60 | System.out.println(getColumn(args[0], args[1], args[2], args[3])); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | }}} |
| 65 | |
| 66 | |