| | 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_3 上一關 < ] 第四關 [wiki:waue/2011/0426_4_5 > 下一關] |
| | 11 | |
| | 12 | = 範例四: 掃描並印出指定的column-Qualifier 的所有列值 = |
| | 13 | |
| | 14 | {{{ |
| | 15 | #!java |
| | 16 | |
| | 17 | package itri; |
| | 18 | |
| | 19 | import java.io.IOException; |
| | 20 | |
| | 21 | import org.apache.hadoop.conf.Configuration; |
| | 22 | import org.apache.hadoop.hbase.HBaseConfiguration; |
| | 23 | import org.apache.hadoop.hbase.client.HTable; |
| | 24 | import org.apache.hadoop.hbase.client.Result; |
| | 25 | import org.apache.hadoop.hbase.client.ResultScanner; |
| | 26 | import org.apache.hadoop.hbase.util.Bytes; |
| | 27 | import org.apache.hadoop.util.GenericOptionsParser; |
| | 28 | |
| | 29 | public class ScanTableV1 { |
| | 30 | |
| | 31 | static void ScanColumn(String tablename, String family, String column) { |
| | 32 | HBaseConfiguration conf = new HBaseConfiguration(); |
| | 33 | |
| | 34 | HTable table; |
| | 35 | try { |
| | 36 | table = new HTable(conf, Bytes.toBytes(tablename)); |
| | 37 | |
| | 38 | ResultScanner scanner = table.getScanner(Bytes.toBytes(family)); |
| | 39 | |
| | 40 | System.out.println("Scan the Table [" + tablename |
| | 41 | + "]'s Column => " + family + ":" + column); |
| | 42 | int i = 1; |
| | 43 | for (Result rowResult : scanner) { |
| | 44 | byte[] by = rowResult.getValue(Bytes.toBytes(family), Bytes |
| | 45 | .toBytes(column)); |
| | 46 | String str = Bytes.toString(by); |
| | 47 | System.out.println("row " + i + " is \"" + str + "\""); |
| | 48 | i++; |
| | 49 | } |
| | 50 | } catch (IOException e) { |
| | 51 | e.printStackTrace(); |
| | 52 | } |
| | 53 | } |
| | 54 | |
| | 55 | public static void main(String[] argv) { |
| | 56 | // String[] argc = {"t1", "f1", "c1"};argv = argc; |
| | 57 | |
| | 58 | String[] args = new GenericOptionsParser(new Configuration(), argv) |
| | 59 | .getRemainingArgs(); |
| | 60 | if (args.length < 3) { |
| | 61 | System.out |
| | 62 | .println("ScanTableV1 <TableName> <Family> <Qualifier> "); |
| | 63 | return; |
| | 64 | } |
| | 65 | ScanColumn(args[0], args[1], args[2]); |
| | 66 | } |
| | 67 | } |
| | 68 | }}} |
| | 69 | |
| | 70 | * 執行結果 |
| | 71 | |
| | 72 | {{{ |
| | 73 | Scan the Table [ex1Table]'s Column => Detail:c1 |
| | 74 | row 1 is "my value good" |
| | 75 | }}} |
| | 76 | |
| | 77 | {{{ |
| | 78 | $ bin/hadoop jar ItriMenu.jar PutData "ex1Table" "row222" "Detail" "c1" "my value better" |
| | 79 | $ bin/hadoop jar ItriMenu.jar ScanTable "ex1Table" "Detail" "c1" |
| | 80 | row 1 is "my value good" |
| | 81 | row 2 is "my value better" |
| | 82 | |
| | 83 | }}} |
| | 84 | |
| | 85 | |
| | 86 | = 進階: 給 table name 即可掃出全部值 = |
| | 87 | |
| | 88 | {{{ |
| | 89 | #!java |
| | 90 | package itri; |
| | 91 | |
| | 92 | import java.io.IOException; |
| | 93 | |
| | 94 | import org.apache.hadoop.conf.Configuration; |
| | 95 | import org.apache.hadoop.hbase.HBaseConfiguration; |
| | 96 | import org.apache.hadoop.hbase.KeyValue; |
| | 97 | import org.apache.hadoop.hbase.client.Get; |
| | 98 | import org.apache.hadoop.hbase.client.HTable; |
| | 99 | import org.apache.hadoop.hbase.client.Result; |
| | 100 | import org.apache.hadoop.hbase.client.ResultScanner; |
| | 101 | import org.apache.hadoop.hbase.client.Scan; |
| | 102 | import org.apache.hadoop.hbase.util.Bytes; |
| | 103 | import org.apache.hadoop.util.GenericOptionsParser; |
| | 104 | |
| | 105 | public class ScanTableV2 { |
| | 106 | |
| | 107 | static void ScanColumn(String tablename) throws IOException { |
| | 108 | HBaseConfiguration conf = new HBaseConfiguration(); |
| | 109 | |
| | 110 | HTable table = new HTable(conf, Bytes.toBytes(tablename)); |
| | 111 | Scan s = new Scan(); |
| | 112 | ResultScanner scanner = table.getScanner(s); |
| | 113 | try { |
| | 114 | |
| | 115 | for (Result rr : scanner) { |
| | 116 | |
| | 117 | byte[] row_b = rr.getRow(); |
| | 118 | String row_s = Bytes.toString(row_b); |
| | 119 | System.out.print("row= " +row_s ); |
| | 120 | for (KeyValue kv : rr.list()) { |
| | 121 | |
| | 122 | byte[] family_b = kv.getFamily(); |
| | 123 | String family_s = Bytes.toString(family_b); |
| | 124 | |
| | 125 | byte[] qfy_b = kv.getQualifier(); |
| | 126 | String qfy_s = Bytes.toString(qfy_b); |
| | 127 | |
| | 128 | Get g = new Get(row_b); |
| | 129 | Result rowResult = table.get(g); |
| | 130 | |
| | 131 | String val = Bytes.toString(rowResult.getValue(family_b, |
| | 132 | qfy_b)); |
| | 133 | System.out.print(" {"+family_s +":"+qfy_s +" = "+val +"}"); |
| | 134 | } |
| | 135 | System.out.println(" "); |
| | 136 | } |
| | 137 | |
| | 138 | } finally { |
| | 139 | scanner.close(); |
| | 140 | } |
| | 141 | } |
| | 142 | |
| | 143 | public static void main(String[] argv) throws IOException { |
| | 144 | // String[] argc = {"t1"};argv = argc; |
| | 145 | |
| | 146 | String[] args = new GenericOptionsParser(new Configuration(), argv) |
| | 147 | .getRemainingArgs(); |
| | 148 | if (args.length < 1) { |
| | 149 | System.out.println("ScanTableV2 <TableName> "); |
| | 150 | return; |
| | 151 | } |
| | 152 | ScanColumn(args[0]); |
| | 153 | } |
| | 154 | } |
| | 155 | }}} |