| 1 | [[PageOutline]] |
| 2 | |
| 3 | ◢ <[wiki:III131207/Lab11 實作十一]> | <[wiki:III131207 回課程大綱]> ▲ | ◣ |
| 4 | |
| 5 | = 實作十二 Lab 12 = |
| 6 | |
| 7 | {{{ |
| 8 | #!html |
| 9 | <div style="text-align: center;"><big style="font-weight: bold;"><big>HBase 行為觀察<br/>HBase in Practice</big></big></div> |
| 10 | }}} |
| 11 | |
| 12 | {{{ |
| 13 | #!text |
| 14 | 以下練習,請使用 hadoop4win 環境執行。 |
| 15 | }}} |
| 16 | |
| 17 | * 首先請先下 start-hbase 指令或點選"開始"->"stat-hbase",來開啟 HBase 與 Hadoop 的 Daemon |
| 18 | * 請回到家目錄,並輸入指令『__'''hbase shell hbase-test'''__』。 |
| 19 | * 註一:這個 hbase-test 存放在安裝帳號身分的家目錄中,倘若用其他帳號登入,可能會找不到。 |
| 20 | * 註二:如果剛剛有下 stop-hadoop 的話,一定要記得 start-hadoop,因為 HBase 的資料表格是存在 HDFS 之上。 |
| 21 | {{{ |
| 22 | $ cd ~ |
| 23 | $ hbase shell hbase-test |
| 24 | }}} |
| 25 | * [[BR]][[Image(Hadoop4Win:hadoop4win_24.jpg,width=600)]] |
| 26 | |
| 27 | * STEP 11: 您也可以輸入指令『__'''hbase shell'''__』進入互動式的 HBase 指令列。您可以嘗試用底下列舉的指令,重現 hbase-test 的過程。 |
| 28 | {{{ |
| 29 | $ hbase shell |
| 30 | }}} |
| 31 | {{{ |
| 32 | create 'test','data' |
| 33 | put 'test','row1','data:1','v1' |
| 34 | put 'test','row2','data:2','v2' |
| 35 | put 'test','row3','data:3','v3' |
| 36 | scan 'test' |
| 37 | disable 'test' |
| 38 | drop 'test' |
| 39 | list |
| 40 | exit |
| 41 | }}} |
| 42 | * [[BR]][[Image(Hadoop4Win:hadoop4win_25.jpg,width=600)]] |
| 43 | |
| 44 | * 欲離開 Cygwin 環境前或者要暫時關閉 HBase 系統時,請輸入指令『__'''stop-hbase'''__』。需提醒您的是 HBase 因採用 !ZooKeeper,有時關閉 master 的速度會比較慢,請耐心等候。註:目前下 exit 指令登出時會執行 stop-hbase 與 stop-hadoop 的動作。 |
| 45 | {{{ |
| 46 | $ stop-hbase |
| 47 | }}} |
| 48 | * [[BR]][[Image(Hadoop4Win:hadoop4win_26.jpg,width=600)]] |
| 49 | |
| 50 | * 倘若是已經關閉 Cygwin 視窗,第二次重新執行 C:\hadoop4win\Cygwin.bat,請輸入指令『__'''start-hbase'''__』,目前 start-hbase 會視目前 java process 判斷 hadoop 是否存在而執行『__'''start-hadoop'''__』。 |
| 51 | {{{ |
| 52 | $ start-hbase |
| 53 | }}} |
| 54 | * [[BR]][[Image(Hadoop4Win:hadoop4win_27.jpg,width=600)]] |
| 55 | |
| 56 | {{{ |
| 57 | #!html |
| 58 | <div style="text-align: center; color:#151B8D"><big style="font-weight: bold;"><big><big> |
| 59 | Hbase 使用方法 |
| 60 | </big></big></big></div> <div style="text-align: center; color:#7E2217"><big style="font-weight: bold;"><big> |
| 61 | HBase 0.20 + Hadoop 0.20 |
| 62 | </big></big></div> |
| 63 | }}} |
| 64 | |
| 65 | = 列出所有 table = |
| 66 | |
| 67 | {{{ |
| 68 | hbase(main):> list |
| 69 | }}} |
| 70 | |
| 71 | = 新增 table = |
| 72 | |
| 73 | A . 直接增加一個表 t2 |
| 74 | |
| 75 | {{{ |
| 76 | hbase(main):> create 't2' |
| 77 | }}} |
| 78 | |
| 79 | B . 增加一個擁有 'f1','f2','fn' 為 column family 的表: t1 |
| 80 | |
| 81 | {{{ |
| 82 | hbase(main):> create 't1','f1','f2','fn' |
| 83 | }}} |
| 84 | |
| 85 | = 查詢 Table 欄位 = |
| 86 | |
| 87 | {{{ |
| 88 | hbase(main):> describe 't1' |
| 89 | }}} |
| 90 | |
| 91 | 執行結果參考 |
| 92 | |
| 93 | {{{ |
| 94 | #!text |
| 95 | hbase(main):> describe 't1' |
| 96 | DESCRIPTION ENABLED |
| 97 | {NAME => 't1', FAMILIES => [{NAME => 'f1', COMPRESSION => 'NONE', VERS true |
| 98 | IONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => ' |
| 99 | false', BLOCKCACHE => 'true'}, {NAME => 'f2', COMPRESSION => 'NONE', V |
| 100 | ERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY = |
| 101 | > 'false', BLOCKCACHE => 'true'}, {NAME => 'fn', COMPRESSION => 'NONE' |
| 102 | , VERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMOR |
| 103 | Y => 'false', BLOCKCACHE => 'true'}]} |
| 104 | }}} |
| 105 | |
| 106 | = 加入cell-value = |
| 107 | |
| 108 | 需先擁有表 t1 與column-family : f1 |
| 109 | 並且加入一個 column-quantifier c1 |
| 110 | |
| 111 | {{{ |
| 112 | hbase(main):> put 't1', 'r1', 'f1', 'v1' |
| 113 | hbase(main):> put 't1', 'r1', 'f1:c1', 'v2' |
| 114 | hbase(main):> put 't1', 'r2', 'f2', 'v3' |
| 115 | hbase(main):> put 't1', 'r2', 'f2:c2', 'v4' |
| 116 | }}} |
| 117 | |
| 118 | {{{ |
| 119 | #!html |
| 120 | <table border='1'> |
| 121 | <tr><td colspan='7'>Table: 't1'</td></tr> |
| 122 | <tr><td rowspan='2'>row-key</td><td colspan='2'>'f1'</td><td colspan='2'>'f2'</td><td>'fn'</td><td>column-family</td></tr> |
| 123 | <tr><td>*</td><td>'c1'</td><td>*</td><td>'c2'</td><td>*</td><td>column-quantifier</td></tr> |
| 124 | <tr><td rowspan='2'>r1</td><td>v1</td><td></td><td></td><td></td><td></td></tr> |
| 125 | <tr><td></td><td>v2</td><td></td><td></td><td></td></tr> |
| 126 | <tr><td rowspan='2'>r2</td><td></td><td></td><td>v3</td><td></td><td></td></tr> |
| 127 | <tr><td></td><td></td><td></td><td>v4</td><td></td></tr> |
| 128 | </table> |
| 129 | }}} |
| 130 | |
| 131 | = 列出cell-value = |
| 132 | |
| 133 | A . 列出一列(row) |
| 134 | |
| 135 | {{{ |
| 136 | hbase(main):> get 't1', 'r1' |
| 137 | }}} |
| 138 | |
| 139 | 執行結果參考 |
| 140 | |
| 141 | {{{ |
| 142 | #!text |
| 143 | COLUMN CELL |
| 144 | f1: timestamp=1285737082689, value=v1 |
| 145 | f1:c1 timestamp=1285737085874, value=v2 |
| 146 | }}} |
| 147 | |
| 148 | {{{ |
| 149 | #!html |
| 150 | <table border='1'> |
| 151 | <tr><td colspan='7'>Table: 't1'</td></tr> |
| 152 | <tr><td rowspan='2'>row-key</td><td colspan='2'>'f1'</td><td colspan='2'>'f2'</td><td>'fn'</td><td>column-family</td></tr> |
| 153 | <tr><td>*</td><td>'c1'</td><td>*</td><td>'c2'</td><td>*</td><td>column-quantifier</td></tr> |
| 154 | <tr><td rowspan='2' bgcolor='red'>r1</td><td bgcolor='red'>v1</td><td></td><td></td><td></td><td></td></tr> |
| 155 | <tr><td></td><td bgcolor='red'>v2</td><td></td><td></td><td></td></tr> |
| 156 | <tr><td rowspan='2'>r2</td><td></td><td></td><td>v3</td><td></td><td></td></tr> |
| 157 | <tr><td></td><td></td><td></td><td>v4</td><td></td></tr> |
| 158 | </table> |
| 159 | }}} |
| 160 | |
| 161 | B . 列出一個 cell 的值 |
| 162 | |
| 163 | {{{ |
| 164 | hbase(main):> get 't1', 'r1', {COLUMN => 'f1:c1'} |
| 165 | }}} |
| 166 | |
| 167 | 執行結果參考 |
| 168 | |
| 169 | {{{ |
| 170 | #!text |
| 171 | COLUMN CELL |
| 172 | f1:c1 timestamp=1285737085874, value=v2 |
| 173 | }}} |
| 174 | |
| 175 | {{{ |
| 176 | #!html |
| 177 | <table border='1'> |
| 178 | <tr><td colspan='7'>Table: 't1'</td></tr> |
| 179 | <tr><td rowspan='2'>row-key</td><td colspan='2' bgcolor='red'>'f1'</td><td colspan='2'>'f2'</td><td>'fn'</td><td>column-family</td></tr> |
| 180 | <tr><td>*</td><td bgcolor='red'>'c1'</td><td>*</td><td>'c2'</td><td>*</td><td>column-quantifier</td></tr> |
| 181 | <tr><td rowspan='2' bgcolor='red'>r1</td><td>v1</td><td></td><td></td><td></td><td></td></tr> |
| 182 | <tr><td></td><td bgcolor='red'>v2</td><td></td><td></td><td></td></tr> |
| 183 | <tr><td rowspan='2'>r2</td><td></td><td></td><td>v3</td><td></td><td></td></tr> |
| 184 | <tr><td></td><td></td><td></td><td>v4</td><td></td></tr> |
| 185 | </table> |
| 186 | }}} |
| 187 | |
| 188 | = 刪除 cell-value = |
| 189 | |
| 190 | {{{ |
| 191 | hbase(main):> deleteall 't1','r1' |
| 192 | }}} |
| 193 | |
| 194 | 執行結果:會把 row-key 是 '!r1' 的所有紀錄。此時資料表 't1' 會變成如下表所示。 |
| 195 | {{{ |
| 196 | hbase(main):> scan 't1' |
| 197 | ROW COLUMN+CELL |
| 198 | r2 column=f2:, timestamp=1285737091644, value=v3 |
| 199 | r2 column=f2:c2, timestamp=1285737094157, value=v4 |
| 200 | }}} |
| 201 | |
| 202 | {{{ |
| 203 | #!html |
| 204 | <table border='1'> |
| 205 | <tr><td colspan='7'>Table: 't1'</td></tr> |
| 206 | <tr><td rowspan='2'>row-key</td><td colspan='2'>'f1'</td><td colspan='2'>'f2'</td><td>'fn'</td><td>column-family</td></tr> |
| 207 | <tr><td>*</td><td>'c1'</td><td>*</td><td>'c2'</td><td>*</td><td>column-quantifier</td></tr> |
| 208 | <tr><td bgcolor='red'> </td><td bgcolor='red'></td><td bgcolor='red'></td><td bgcolor='red'></td><td bgcolor='red'></td><td bgcolor='red'></td></tr> |
| 209 | <tr><td rowspan='2'>r2</td><td></td><td></td><td>v3</td><td></td><td></td></tr> |
| 210 | <tr><td></td><td></td><td></td><td>v4</td><td></td></tr> |
| 211 | </table> |
| 212 | }}} |
| 213 | |
| 214 | = 加入column family = |
| 215 | |
| 216 | {{{ |
| 217 | hbase(main):> disable 't1' |
| 218 | hbase(main):> alter 't1', {NAME => 'f3'} |
| 219 | hbase(main):> enable 't1' |
| 220 | }}} |
| 221 | |
| 222 | 執行結果:多了一個 column-family 'f3' 可以用 describe 指令查詢,結果示意如下表: |
| 223 | |
| 224 | {{{ |
| 225 | hbase(main):021:0> describe 't1' |
| 226 | DESCRIPTION ENABLED |
| 227 | {NAME => 't1', FAMILIES => [{NAME => 'f1', COMPRESSION => 'NONE', VERS true |
| 228 | IONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => ' |
| 229 | false', BLOCKCACHE => 'true'}, {NAME => 'f2', COMPRESSION => 'NONE', V |
| 230 | ERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY = |
| 231 | > 'false', BLOCKCACHE => 'true'}, {NAME => 'f3', VERSIONS => '3', COMP |
| 232 | RESSION => 'NONE', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMOR |
| 233 | Y => 'false', BLOCKCACHE => 'true'}, {NAME => 'fn', COMPRESSION => 'NO |
| 234 | NE', VERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_ME |
| 235 | MORY => 'false', BLOCKCACHE => 'true'}]} |
| 236 | }}} |
| 237 | |
| 238 | |
| 239 | {{{ |
| 240 | #!html |
| 241 | <table border='1'> |
| 242 | <tr><td colspan='8'>Table: 't1'</td></tr> |
| 243 | <tr><td rowspan='2'>row-key</td><td colspan='2'>'f1'</td><td colspan='2'>'f2'</td><td bgcolor='red'>'f3'</td><td>'fn'</td><td>column-family</td></tr> |
| 244 | <tr><td>*</td><td>'c1'</td><td>*</td><td>'c2'</td><td bgcolor='red'>*</td><td>*</td><td>column-quantifier</td></tr> |
| 245 | <tr><td rowspan='2'>r2</td><td></td><td></td><td>v3</td><td></td><td bgcolor='red'></td><td></td></tr> |
| 246 | <tr><td></td><td></td><td></td><td>v4</td><td bgcolor='red'></td><td></td></tr> |
| 247 | </table> |
| 248 | }}} |
| 249 | |
| 250 | = 刪除column family = |
| 251 | |
| 252 | {{{ |
| 253 | hbase(main):> disable 't1' |
| 254 | hbase(main):> alter 't1', {NAME => 'f1', METHOD => 'delete'} |
| 255 | hbase(main):> enable 't1' |
| 256 | }}} |
| 257 | |
| 258 | 執行結果:會移除 column family 為 'f1' 的所有欄位,可用 describe 指令確認,結果如下表所示。 |
| 259 | |
| 260 | {{{ |
| 261 | hbase(main):053:0> describe 't1' |
| 262 | DESCRIPTION ENABLED |
| 263 | {NAME => 't1', FAMILIES => [{NAME => 'f2', COMPRESSION => 'NONE', VERS true |
| 264 | IONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => ' |
| 265 | false', BLOCKCACHE => 'true'}, {NAME => 'f3', COMPRESSION => 'NONE', V |
| 266 | ERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY = |
| 267 | > 'false', BLOCKCACHE => 'true'}, {NAME => 'fn', COMPRESSION => 'NONE' |
| 268 | , VERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMOR |
| 269 | Y => 'false', BLOCKCACHE => 'true'}]} |
| 270 | 1 row(s) in 0.0200 seconds |
| 271 | }}} |
| 272 | |
| 273 | {{{ |
| 274 | #!html |
| 275 | <table border='1'> |
| 276 | <tr><td colspan='8'>Table: 't1'</td></tr> |
| 277 | <tr><td rowspan='2'>row-key</td><td bgcolor='red'> </td><td colspan='2'>'f2'</td><td>'f3'</td><td>'fn'</td><td>column-family</td></tr> |
| 278 | <tr><td bgcolor='red'></td><td>*</td><td>'c2'</td><td>*</td><td>*</td><td>column-quantifier</td></tr> |
| 279 | <tr><td rowspan='2'>r2</td><td bgcolor='red'></td><td>v3</td><td></td><td></td><td></td></tr> |
| 280 | <tr><td bgcolor='red'></td><td></td><td>v4</td><td></td><td></td></tr> |
| 281 | </table> |
| 282 | }}} |
| 283 | |
| 284 | |
| 285 | |
| 286 | = 節點狀態 = |
| 287 | |
| 288 | {{{ |
| 289 | hbase(main):> status |
| 290 | }}} |
| 291 | |
| 292 | = 刪除整張table = |
| 293 | |
| 294 | {{{ |
| 295 | hbase(main):> truncate 't1' |
| 296 | }}} |
| 297 | |
| 298 | * 執行完 truncate 後,所有 't1' 的內容都會被移除。但此時,下 list 還會有 't1' 這個表格存在。 |
| 299 | |
| 300 | {{{ |
| 301 | hbase(main):> disable 't1' |
| 302 | hbase(main):> drop 't1' |
| 303 | }}} |
| 304 | |
| 305 | * 要完整移除 't1' 資料表,必須使用 disable 先將 't1' 停用,再用 drop 指令把 't1' 完全刪除。 |