| 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_HBEX3 上一關 < ] 第四關 [wiki:NCHCCloudCourse100929_4_HBEX5 > 下一關] |
| 11 | |
| 12 | = 範例四: Scan all Column = |
| 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.HTable; |
| 23 | import org.apache.hadoop.hbase.client.Result; |
| 24 | import org.apache.hadoop.hbase.client.ResultScanner; |
| 25 | import org.apache.hadoop.hbase.util.Bytes; |
| 26 | import org.apache.hadoop.util.GenericOptionsParser; |
| 27 | |
| 28 | public class ScanTable { |
| 29 | |
| 30 | static void ScanColumn(String tablename, String family, String column) { |
| 31 | HBaseConfiguration conf = new HBaseConfiguration(); |
| 32 | |
| 33 | HTable table; |
| 34 | try { |
| 35 | table = new HTable(conf, Bytes.toBytes(tablename)); |
| 36 | |
| 37 | ResultScanner scanner = table.getScanner(Bytes.toBytes(family)); |
| 38 | |
| 39 | System.out.println("Scan the Table [" + tablename |
| 40 | + "]'s Column => " + family + ":" + column); |
| 41 | int i = 1; |
| 42 | for (Result rowResult : scanner) { |
| 43 | byte[] by = rowResult.getValue(Bytes.toBytes(family), Bytes |
| 44 | .toBytes(column)); |
| 45 | String str = Bytes.toString(by); |
| 46 | System.out.println("row " + i + " is \"" + str + "\""); |
| 47 | i++; |
| 48 | } |
| 49 | } catch (IOException e) { |
| 50 | e.printStackTrace(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public static void main(String[] argv) { |
| 55 | String[] args = new GenericOptionsParser(new Configuration(), argv) |
| 56 | .getRemainingArgs(); |
| 57 | if (args.length < 3) { |
| 58 | System.out |
| 59 | .println("ScanColumn <TableName> <Family> <Qualifier> "); |
| 60 | return; |
| 61 | } |
| 62 | ScanColumn(args[0], args[1], args[2]); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | }}} |
| 67 | |
| 68 | |