[[PageOutline]]
◢ <[wiki:YZU130807/Lab11 實作十四]> | <[wiki:YZU130807 回課程大綱]> ▲ | ◣
= 實作十五 Lab 15 =
{{{
#!html
HBase 行為觀察
HBase in Practice
}}}
{{{
#!text
以下練習,請使用 hadoop4win 環境執行。
}}}
 * 首先請先下 start-hbase 指令或點選"開始"->"stat-hbase",來開啟 HBase 與 Hadoop 的 Daemon
 * 請回到家目錄,並輸入指令『__'''hbase shell hbase-test'''__』。
 * 註一:這個 hbase-test 存放在安裝帳號身分的家目錄中,倘若用其他帳號登入,可能會找不到。
 * 註二:如果剛剛有下 stop-hadoop 的話,一定要記得 start-hadoop,因為 HBase 的資料表格是存在 HDFS 之上。
{{{
$ cd ~
$ hbase shell hbase-test
}}}
   * [[BR]][[Image(Hadoop4Win:hadoop4win_24.jpg,width=600)]]
 * STEP 11: 您也可以輸入指令『__'''hbase shell'''__』進入互動式的 HBase 指令列。您可以嘗試用底下列舉的指令,重現 hbase-test 的過程。
{{{
$ hbase shell
}}}
{{{
create 'test','data'
put 'test','row1','data:1','v1'
put 'test','row2','data:2','v2'
put 'test','row3','data:3','v3'
scan 'test'
disable 'test'
drop 'test'
list
exit
}}}
   * [[BR]][[Image(Hadoop4Win:hadoop4win_25.jpg,width=600)]]
 * 欲離開 Cygwin 環境前或者要暫時關閉 HBase 系統時,請輸入指令『__'''stop-hbase'''__』。需提醒您的是 HBase 因採用 !ZooKeeper,有時關閉 master 的速度會比較慢,請耐心等候。註:目前下 exit 指令登出時會執行 stop-hbase 與 stop-hadoop 的動作。
{{{
$ stop-hbase
}}}
   * [[BR]][[Image(Hadoop4Win:hadoop4win_26.jpg,width=600)]]
 * 倘若是已經關閉 Cygwin 視窗,第二次重新執行 C:\hadoop4win\Cygwin.bat,請輸入指令『__'''start-hbase'''__』,目前 start-hbase 會視目前 java process 判斷 hadoop 是否存在而執行『__'''start-hadoop'''__』。
{{{
$ start-hbase
}}}
   * [[BR]][[Image(Hadoop4Win:hadoop4win_27.jpg,width=600)]]
{{{
#!html
Hbase 使用方法
 
HBase 0.20 + Hadoop 0.20
}}}
= 列出所有 table = 
{{{
hbase(main):>	list
}}}
 = 新增 table = 
 A .  直接增加一個表 t2
{{{
hbase(main):>	create 't2'
}}}
 B . 增加一個擁有 'f1','f2','fn' 為 column family 的表: t1
{{{
hbase(main):>	create 't1','f1','f2','fn'   
}}}
 = 查詢 Table 欄位 =
{{{
hbase(main):> describe 't1'
}}}
執行結果參考 
{{{
#!text
hbase(main):> describe 't1'
DESCRIPTION                                                             ENABLED                               
 {NAME => 't1', FAMILIES => [{NAME => 'f1', COMPRESSION => 'NONE', VERS true                                  
 IONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => '                                       
 false', BLOCKCACHE => 'true'}, {NAME => 'f2', COMPRESSION => 'NONE', V                                       
 ERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY =                                       
 > 'false', BLOCKCACHE => 'true'}, {NAME => 'fn', COMPRESSION => 'NONE'                                       
 , VERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMOR                                       
 Y => 'false', BLOCKCACHE => 'true'}]} 
}}}
 = 加入cell-value = 
需先擁有表 t1 與column-family : f1
並且加入一個 column-quantifier c1 
 
{{{
hbase(main):>	put 't1', 'r1', 'f1', 'v1'    
hbase(main):>	put 't1', 'r1', 'f1:c1', 'v2'    
hbase(main):>	put 't1', 'r2', 'f2', 'v3'
hbase(main):>	put 't1', 'r2', 'f2:c2', 'v4'
}}}
{{{
#!html
| Table: 't1' | 
| row-key | 'f1' | 'f2' | 'fn' | column-family | 
| * | 'c1' | * | 'c2' | * | column-quantifier | 
| r1 | v1 |  |  |  |  | 
 | v2 |  |  |  | 
| r2 |  |  | v3 |  |  | 
 |  |  | v4 |  | 
}}}
 = 列出cell-value = 
 A . 列出一列(row)
{{{
hbase(main):>	get 't1', 'r1'
}}}
 
執行結果參考
   {{{
#!text
COLUMN                   	CELL                
 f1:                         timestamp=1285737082689, value=v1                                                
 f1:c1                       timestamp=1285737085874, value=v2                                          
   }}}
{{{
#!html
| Table: 't1' | 
| row-key | 'f1' | 'f2' | 'fn' | column-family | 
| * | 'c1' | * | 'c2' | * | column-quantifier | 
| r1 | v1 |  |  |  |  | 
 | v2 |  |  |  | 
| r2 |  |  | v3 |  |  | 
 |  |  | v4 |  | 
}}}
 B . 列出一個 cell 的值
{{{
hbase(main):>   get 't1', 'r1', {COLUMN => 'f1:c1'}
}}}    
   
執行結果參考
   {{{
#!text
COLUMN                       CELL                             
 f1:c1                       timestamp=1285737085874, value=v2
   }}}
{{{
#!html
| Table: 't1' | 
| row-key | 'f1' | 'f2' | 'fn' | column-family | 
| * | 'c1' | * | 'c2' | * | column-quantifier | 
| r1 | v1 |  |  |  |  | 
 | v2 |  |  |  | 
| r2 |  |  | v3 |  |  | 
 |  |  | v4 |  | 
}}}
 = 刪除 cell-value = 
{{{
hbase(main):>	deleteall 't1','r1'
}}}
執行結果:會把 row-key 是 '!r1' 的所有紀錄。此時資料表 't1' 會變成如下表所示。
{{{
hbase(main):> scan 't1'                 
ROW    COLUMN+CELL                                                                      
 r2    column=f2:, timestamp=1285737091644, value=v3                                    
 r2    column=f2:c2, timestamp=1285737094157, value=v4
}}}
{{{
#!html
| Table: 't1' | 
| row-key | 'f1' | 'f2' | 'fn' | column-family | 
| * | 'c1' | * | 'c2' | * | column-quantifier | 
|   |  |  |  |  |  | 
| r2 |  |  | v3 |  |  | 
 |  |  | v4 |  | 
}}}
 = 加入column family = 
{{{
hbase(main):>	disable 't1'
hbase(main):>   alter 't1', {NAME => 'f3'}
hbase(main):>   enable 't1'
}}}    
執行結果:多了一個 column-family 'f3' 可以用 describe 指令查詢,結果示意如下表:
{{{
hbase(main):021:0> describe 't1'
DESCRIPTION                                                             ENABLED                               
 {NAME => 't1', FAMILIES => [{NAME => 'f1', COMPRESSION => 'NONE', VERS true                                  
 IONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => '                                       
 false', BLOCKCACHE => 'true'}, {NAME => 'f2', COMPRESSION => 'NONE', V                                       
 ERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY =                                       
 > 'false', BLOCKCACHE => 'true'}, {NAME => 'f3', VERSIONS => '3', COMP                                       
 RESSION => 'NONE', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMOR                                       
 Y => 'false', BLOCKCACHE => 'true'}, {NAME => 'fn', COMPRESSION => 'NO                                       
 NE', VERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_ME                                       
 MORY => 'false', BLOCKCACHE => 'true'}]}
}}}
{{{
#!html
| Table: 't1' | 
| row-key | 'f1' | 'f2' | 'f3' | 'fn' | column-family | 
| * | 'c1' | * | 'c2' | * | * | column-quantifier | 
| r2 |  |  | v3 |  |  |  | 
 |  |  | v4 |  |  | 
}}}
 = 刪除column family = 
{{{
hbase(main):>	disable 't1'
hbase(main):>	alter 't1', {NAME => 'f1', METHOD => 'delete'}
hbase(main):>   enable 't1'
}}}    
執行結果:會移除 column family 為 'f1' 的所有欄位,可用 describe 指令確認,結果如下表所示。
{{{
hbase(main):053:0> describe 't1'
DESCRIPTION                                                             ENABLED                               
 {NAME => 't1', FAMILIES => [{NAME => 'f2', COMPRESSION => 'NONE', VERS true                                  
 IONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => '                                       
 false', BLOCKCACHE => 'true'}, {NAME => 'f3', COMPRESSION => 'NONE', V                                       
 ERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY =                                       
 > 'false', BLOCKCACHE => 'true'}, {NAME => 'fn', COMPRESSION => 'NONE'                                       
 , VERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMOR                                       
 Y => 'false', BLOCKCACHE => 'true'}]}                                                                        
1 row(s) in 0.0200 seconds
}}}
{{{
#!html
| Table: 't1' | 
| row-key |   | 'f2' | 'f3' | 'fn' | column-family | 
 | * | 'c2' | * | * | column-quantifier | 
| r2 |  | v3 |  |  |  | 
 |  | v4 |  |  | 
}}}
 = 節點狀態 = 
{{{
hbase(main):>	status
}}}    
 = 刪除整張table = 
{{{
hbase(main):>	truncate 't1'
}}}
 * 執行完 truncate 後,所有 't1' 的內容都會被移除。但此時,下 list 還會有 't1' 這個表格存在。
{{{
hbase(main):>	disable 't1'
hbase(main):>	drop 't1'
}}}    
 * 要完整移除 't1' 資料表,必須使用 disable 先將 't1' 停用,再用 drop 指令把 't1' 完全刪除。