24 | | String table_name = "waue"; |
25 | | String colomn_family = "family:"; |
26 | | String column_quolify= "qf"; |
27 | | String hbase_row = "w-row"; |
28 | | String value = "0911311311"; |
29 | | }}} |
30 | | |
31 | | * 首先,建立config 物件與admin 物件,前者用來設定資料表屬性,後者用來執行資料庫的建立刪除操作 |
32 | | |
33 | | {{{ |
34 | | #!java |
35 | | |
36 | | HBaseConfiguration config = new HBaseConfiguration(); |
37 | | HBaseAdmin admin = new HBaseAdmin(config); |
38 | | }}} |
39 | | |
40 | | * |
41 | | |
42 | | {{{ |
43 | | #!java |
44 | | |
45 | | if (!admin.tableExists(table_name)) { |
46 | | System.out.println("HTable : " + table_name |
47 | | + " creating ... please wait"); |
48 | | HTableDescriptor tableDesc = new HTableDescriptor(table_name); |
49 | | // add column family |
50 | | tableDesc.addFamily(new HColumnDescriptor(colomn_family)); |
51 | | admin.createTable(tableDesc); |
52 | | } |
53 | | }}} |
54 | | |
55 | | * |
56 | | |
57 | | {{{ |
58 | | #!java |
59 | | |
60 | | |
61 | | BatchUpdate batchUpdate = new BatchUpdate(hbase_row); |
62 | | batchUpdate.put(colomn_family+column_quolify, Bytes.toBytes(value)); |
63 | | batchUpdate.delete(colomn_family+"cellIWantDeleted"); |
64 | | }}} |
65 | | |
66 | | * |
67 | | |
68 | | {{{ |
69 | | #!java |
70 | | HTable table = new HTable(config, table_name); |
71 | | table.commit(batchUpdate); |
72 | | }}} |
73 | | |
74 | | * |
75 | | |
76 | | {{{ |
77 | | #!java |
78 | | |
79 | | Cell cell = table.get(hbase_row, colomn_family+column_quolify); |
80 | | System.out.print(cell.getValue()); |
81 | | |
82 | | }}} |
83 | | |
84 | | * |
85 | | |
86 | | {{{ |
87 | | #!java |
88 | | Scanner scanner = |
89 | | table.getScanner(new String[] { colomn_family+column_quolify }); |
90 | | |
91 | | RowResult rowResult = scanner.next(); |
92 | | |
93 | | while (rowResult != null) { |
94 | | |
95 | | System.out.println("Found row: " |
96 | | + Bytes.toString(rowResult.getRow()) |
97 | | + " with value: " |
98 | | + rowResult.get(Bytes |
99 | | .toBytes(colomn_family+column_quolify))); |
100 | | rowResult = scanner.next(); |
101 | | } |
102 | | |
103 | | for (RowResult result : scanner) { |
104 | | System.out.println("Found row: " |
105 | | + Bytes.toString(rowResult.getRow()) |
106 | | + " with value: " |
107 | | + rowResult.get(Bytes |
108 | | .toBytes(colomn_family+column_quolify))); |
109 | | } |
110 | | |
111 | | scanner.close(); |
112 | | |
113 | | |
114 | | |
| 27 | String table_name = "waue"; |
| 28 | String colomn_family = "family:"; |
| 29 | String column_quolify= "qf"; |
| 30 | String hbase_row = "w-row"; |
| 31 | String value = "yes ! good !"; |
| 32 | }}} |
| 33 | |
| 34 | === 必要條件 資料表屬性:HBaseConfiguration === |
| 35 | |
| 36 | * 首先,建立HTableDescriptor 物件,用來設定資料表屬性,為必要條件。 |
| 37 | |
| 38 | {{{ |
| 39 | #!java |
| 40 | |
| 41 | HBaseConfiguration config = new HBaseConfiguration(); |
| 42 | }}} |
| 43 | |
| 44 | === 資料表的特權操作:HBaseAdmin === |
| 45 | * HBaseAdmin 用來執行資料庫的建立刪除操作,下面這段是用來檢查是否已經存在這個資料表,沒有的話則新增, |
| 46 | 新增時需要用到HTableDescriptor這個類別來新增Column_Family這個成員。此非必要條件,因為不見得每次執行程式都要新建一個表單 |
| 47 | |
| 48 | * 注意: 在此colomn_family 的後面要以:結尾,如" family:" |
| 49 | |
| 50 | {{{ |
| 51 | #!java |
| 52 | HBaseAdmin admin = new HBaseAdmin(config); |
| 53 | |
| 54 | if (!admin.tableExists(table_name)) { |
| 55 | System.out.println("HTable : " + table_name |
| 56 | + " creating ... please wait"); |
| 57 | HTableDescriptor tableDesc = new HTableDescriptor(table_name); |
| 58 | // add column family |
| 59 | tableDesc.addFamily(new HColumnDescriptor(colomn_family)); |
| 60 | admin.createTable(tableDesc); |
| 61 | } |
| 62 | }}} |
| 63 | |
| 64 | * 接著,我們用BatchUpdate來加入表單內容,我們首先用建構值填入hbase_row,接著用put放入值,用delete刪除值。 |
| 65 | * 此非必要條件(也就是沒有用到這個類別程式也能跑),但若是希望能新增或刪除資料的話還是得用到它 |
| 66 | |
| 67 | 注意:這個類別不需要其他類別支援。 |
| 68 | |
| 69 | === 新增/刪除 資料表內容:BatchUpdate === |
| 70 | |
| 71 | {{{ |
| 72 | #!java |
| 73 | |
| 74 | BatchUpdate batchUpdate = new BatchUpdate(hbase_row); |
| 75 | batchUpdate.put(colomn_family+column_quolify, Bytes.toBytes(value)); |
| 76 | batchUpdate.delete(colomn_family+"cellIWantDeleted"); |
| 77 | }}} |
| 78 | |
| 79 | === 資料表內容:HTable === |
| 80 | |
| 81 | * HTable 類別在此是用來把之前的建構的內容寫入 ,之後還會用這個類別取得資料表內容,因此也算是必要條件 |
| 82 | * HTable 類別用來連接HBaseConfiguration與BatchUpdate,如此完成表單建立 |
| 83 | 這個類別填入HBaseConfiguration與table_name的建構值,並用commit這個函式輸入之前BatchUpdate建立的表單資料。 |
| 84 | |
| 85 | {{{ |
| 86 | #!java |
| 87 | HTable table = new HTable(config, table_name); |
| 88 | table.commit(batchUpdate); |
| 89 | }}} |
| 90 | |
| 91 | * 如果你已經知道資料表中的 "row" "colomn_family" "column_quolify" 的資訊,可以用下列方法把該cell的值印出來。 |
| 92 | |
| 93 | 注意: Hbase內的資料屬性雖然是byte,但要用 org.apache.hadoop.hbase.util.Bytes 的 toString(值) 才可以轉出正確格式 |
| 94 | |
| 95 | {{{ |
| 96 | #!java |
| 97 | |
| 98 | Cell cell = table.get(hbase_row, colomn_family+column_quolify); |
| 99 | // System.out.print(cell.getValue()); 會印出亂碼,因為資料格式不正確 |
| 100 | System.out.print(Bytes.toString(cell.getValue())); // cell value = yes ! good ! |
| 101 | |
| 102 | }}} |
| 103 | |
| 104 | === 掃描資料表:Scanner === |
| 105 | |
| 106 | * 接下來的程式碼用來印出資料表內容,因此需要用到HTable |
| 107 | * Scanner連接到HBase後,怎麼把內容萃取出來呢? 用到 RowResult承接 Scanner.next()方法,然而一個row裡面,會有 |
| 108 | 許多colomn_family,每個colomn_family又會有自己的column_quolify,形成colomn_family:column_quolify的二維結構。 |
| 109 | * getRow() : 把row的名稱秀出來 |
| 110 | * get(colomn_family:column_quolify) : 把該cell的值秀出來 |
| 111 | * for (RowResult result : scanner) 可以看成是 |
| 112 | for (int i=0; i< scanner.length; i++){ RowResult result[i] = scanner[i]; |
| 113 | |
| 114 | |
| 115 | {{{ |
| 116 | #!java |
| 117 | Scanner scanner = table.getScanner(new String[] { colomn_family+column_quolify }); |
| 118 | |
| 119 | RowResult rowResult = scanner.next(); |
| 120 | |
| 121 | while (rowResult != null) { |
| 122 | |
| 123 | System.out.println("Found row: " |
| 124 | + Bytes.toString(rowResult.getRow()) |
| 125 | + " with value: " |
| 126 | + rowResult.get(Bytes |
| 127 | .toBytes(colomn_family+column_quolify))); |
| 128 | rowResult = scanner.next(); |
| 129 | } |
| 130 | |
| 131 | for (RowResult result : scanner) { |
| 132 | System.out.println("Found row: " |
| 133 | + Bytes.toString(rowResult.getRow()) |
| 134 | + " with value: " |
| 135 | + rowResult.get(Bytes |
| 136 | .toBytes(colomn_family+column_quolify))); |
| 137 | } |
| 138 | |
| 139 | scanner.close(); |