概述
前面寫了一些 Hadoop, Zookeeper 及 Hbase 分佈式環境搭建的文章。或許你在搭建的過程中遇到了一些不如意的事情,但我相信總是可以解決的。如果你已經完成了環境的搭建,那麼就可以盡情玩耍了。
本文就先來玩玩 HBase Shell。
版權說明
著作權歸作者所有。
商業轉載請聯繫作者獲得授權,非商業轉載請註明出處。
本文作者:Q-WHai
發表日期: 2016年6月12日
本文鏈接:http://blog.csdn.net/lemon_tree12138/article/details/51646388
來源:CSDN
更多內容:分類 >> 大數據之 Hadoop
Shell
login shell
想要玩 shell,首先你得登錄進去。登錄很簡單
$ hbase shell
2016-06-11 20:05:29,095 INFO [main] Configuration.deprecation: hadoop.native.lib is deprecated. Instead, use io.native.lib.available
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.96.2-hadoop2, r1581096, Mon Mar 24 16:03:18 PDT 2014
hbase(main):001:0>
這樣就進入了 hbase 的 shell 環境了。
通用命令
序號 | 命令 | 描述 |
---|---|---|
1 | status | 提供 HBase 的狀態,例如,服務器的數量 |
2 | version | 提供正在使用 HBase 版本 |
3 | table_help | 表引用命令提供幫助 |
4 | whoami | 提供有關用戶的信息 |
check status
查詢服務器狀態
hbase(main):001:0> status
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/hadoop/hbase/lib/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/hadoop/hadoop/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
3 servers, 0 dead, 0.6667 average load
這裏顯示了有三臺服務器,沒有服務器掛掉。
version
查詢服務器版本
hbase(main):002:0> version
0.96.2-hadoop2, r1581096, Mon Mar 24 16:03:18 PDT 2014
whoami
hbase(main):040:0> whoami
hadoop (auth:SIMPLE)
create table
現在我們來創建一張數據表,比如數據表 student,字段有: student_id, name, sex, age. 那麼我們可以這樣來寫創建語句:
基本格式:create ‘表名稱’, ‘列名稱1’,’列名稱2’,…,’列名稱N’
hbase(main):003:0> create 'student','student_id','name','sex','age'
0 row(s) in 5.5210 seconds
=> Hbase::Table - student
show table
對於剛剛創建的表,我們可以這樣來查詢是否已經創建成功。list 命令有點類似於 MySQL 中的 show.
hbase(main):004:0> list
TABLE
student
1 row(s) in 0.3350 seconds
=> ["student"]
但是如果你的數據表太多,用肉眼去掃描顯然是一件吃力的事情,這時你可以使用 exists 命令直接去判斷這個數據表是否存在。這裏我用兩個數據表進行測試,一個是已經存在的 student,還有一個就是並不存在的 student1,這樣就可以進行對比了。
hbase(main):006:0> exists 'student'
Table student does exist
0 row(s) in 0.1580 seconds
hbase(main):007:0> exists 'student1'
Table student1 does not exist
0 row(s) in 0.0430 seconds
而如果想要查看具體表的詳情,可以這樣來操作。其實在 MySQL 中也有類似的操作,如果你還記得的話。(偷偷告訴你,是 desc table_name; )
hbase(main):005:0> describe 'student'
DESCRIPTION ENABLED
'student', {NAME => 'age', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'false', DATA_BLOCK_ENCODING => 'NONE', T true
TL => '2147483647', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}, {NAME => 'name',
BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => '2147483647', COMPRE
SSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}, {NAME => 'sex', BLOOMFILTER => 'ROW', VERS
IONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => '2147483647', COMPRESSION => 'NONE', MIN_VERSIO
NS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}, {NAME => 'student_id', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMO
RY => 'false', KEEP_DELETED_CELLS => 'false', DATA_BLOCK_ENCODING => 'NONE', TTL => '2147483647', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACH
E => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
1 row(s) in 0.3310 seconds
insert data
現在就可以進行添加數據。
hbase(main):008:0> put 'student','1001','name','Bob'
0 row(s) in 0.3850 seconds
hbase(main):009:0> put 'student','1001','sex','Male'
0 row(s) in 0.0960 seconds
hbase(main):010:0> put 'student','1001','age','18'
0 row(s) in 0.0500 seconds
上面的這些操作是向 student 表中的學號爲 1001 的行添加了 { name=Bob;sex=Male;age=18 } 的記錄。
select data
我們可以根據學號查詢某一個學號下的所有信息,如下:
hbase(main):012:0> get 'student','1001'
COLUMN CELL
age: timestamp=1465701894640, value=18
name: timestamp=1465701805984, value=Bob
sex: timestamp=1465701881247, value=Male
3 row(s) in 0.0690 seconds
也可以根據學號及列族查詢,如下:
hbase(main):013:0> get 'student','1001','name'
COLUMN CELL
name: timestamp=1465701805984, value=Bob
1 row(s) in 0.0100 seconds
如果你想掃描整個數據表(雖然不建議,但是如果你想),你可以使用 scan.
hbase(main):024:0> scan 'student'
ROW COLUMN+CELL
1001 column=age:, timestamp=1465702655984, value=19
1001 column=name:, timestamp=1465701805984, value=Bob
1001 column=sex:, timestamp=1465701881247, value=Male
1002 column=age:, timestamp=1465703066425, value=17
1002 column=name:, timestamp=1465703048791, value=Alice
1002 column=sex:, timestamp=1465703060804, value=female
2 row(s) in 0.0400 seconds
查詢數據表有多少行,請使用 count 命令。
hbase(main):027:0> count 'student'
2 row(s) in 0.0650 seconds
=> 2
update data
在學習 HBase 的一開始我們就知道了,Hbase 中的 update 其實是一個僞 update 操作。因爲所有的 update 都是在 put 一條新的記錄。
hbase(main):014:0> put 'student','1001','age','19'
0 row(s) in 0.0280 seconds
hbase(main):015:0> get 'student','1001'
COLUMN CELL
age: timestamp=1465702655984, value=19
name: timestamp=1465701805984, value=Bob
sex: timestamp=1465701881247, value=Male
3 row(s) in 0.0380 seconds
如果你懷疑這種說法的可靠性,你可以進行驗證,驗證的方法就查找兩個不同版本的數據。
hbase(main):016:0> get 'student','1001',{COLUMN=>'age',TIMESTAMP=>1465701894640}
COLUMN CELL
age: timestamp=1465701894640, value=18
1 row(s) in 0.0460 seconds
hbase(main):017:0> get 'student','1001',{COLUMN=>'age',TIMESTAMP=>1465702655984}
COLUMN CELL
age: timestamp=1465702655984, value=19
1 row(s) in 0.0180 seconds
delete data
刪除某一個字段
hbase(main):025:0> delete 'student','1001','sex'
0 row(s) in 0.5230 seconds
而在驗證的時候,sex 這個列沒有被打印出來。這就說明刪除成功了。
hbase(main):026:0> get 'student','1001'
COLUMN CELL
age: timestamp=1465702655984, value=19
name: timestamp=1465701805984, value=Bob
2 row(s) in 0.0410 seconds
根據學號刪除一整行數據
如果你想刪除學號爲 1001 這一整行數據,這其實是一件麻煩的事情,因爲你需要依次“刪除” 1001 下的所有字段。當所有字段的數據被“刪除”乾淨的時候,這一行的數據纔會被“刪除”。
hbase(main):025:0> delete 'student','1001','sex'
0 row(s) in 0.5230 seconds
hbase(main):030:0> delete 'student','1001','age'
0 row(s) in 0.0210 seconds
hbase(main):031:0> delete 'student','1001','name'
0 row(s) in 0.0100 seconds
hbase(main):032:0> get 'student','1001'
COLUMN CELL
0 row(s) in 0.0080 second
清空數據表
清空表的操作跟 MySQL 中的清空很像。都是使用 truncate 命令。不同的是清空的過程。
hbase(main):034:0> truncate 'student'
Truncating 'student' table (it may take a while):
- Disabling table...
- Dropping table...
- Creating table...
0 row(s) in 3.5380 seconds
在上面打印出來的信息中可以看出,清空的三個關鍵步驟:disable、drop、create.
這也是由於 hbase 不能修改數據的造成的。下面進行驗證:
hbase(main):036:0> scan 'student'
ROW COLUMN+CELL
0 row(s) in 0.0150 seconds
attributes
這一節是說明一下 HBase 數據庫屬性說明及其設置。
由於之前清空了數據表,這裏我們重新創建一個表 staff。字段爲 ‘name’,’number’,’info’。
序號 | 命令 | 描述 |
---|---|---|
1 | disable | 禁用表 |
2 | is_disabled | 驗證表是否被禁用 |
3 | enable | 啓用一個表 |
4 | is_enabled | 驗證表是否已啓用 |
5 | alter | 改變一個表 |
6 | exists | 驗證表是否存在 |
is_enabled & is_disabled
hbase(main):002:0> is_enabled 'staff'
true
0 row(s) in 0.6660 seconds
hbase(main):003:0> is_disabled 'staff'
false
0 row(s) in 0.1190 seconds
disable & enable
hbase(main):004:0> disable 'staff'
0 row(s) in 1.6220 seconds
這樣我們就設置了 ‘staff’ 表被禁用了,驗證信息如下:
hbase(main):006:0> is_enabled 'staff'
false
0 row(s) in 0.0750 seconds
hbase(main):007:0> is_disabled 'staff'
true
0 row(s) in 0.0350 seconds
現在我們在 ‘staff’ 表被禁用的情況下對錶進行一個 put 操作,就會出現異常狀況。而且從異常信息可以看出,這裏並沒有說明是由於表被禁用引起的異常,所以在以後的代碼編寫中這一點需要注意一下。
hbase(main):008:0> put 'staff','John','number','13812345879'
ERROR: Failed 1 action: NotServingRegionException: 1 time,
Here is some help for this command:
Put a cell 'value' at specified table/row/column and optionally
timestamp coordinates. To put a cell value into table 'ns1:t1' or 't1'
at row 'r1' under column 'c1' marked with the time 'ts1', do:
hbase> put 'ns1:t1', 'r1', 'c1', 'value', ts1
The same commands also can be run on a table reference. Suppose you had a reference
t to table 't1', the corresponding command would be:
hbase> t.put 'r1', 'c1', 'value', ts1
我們再來啓用 ‘staff’ 表,再進行上面的測試試一下。
hbase(main):009:0> enable 'staff'
0 row(s) in 1.1610 seconds
hbase(main):010:0> put 'staff','John','number','13812345879'
0 row(s) in 0.0210 seconds
alter
設置單元的最大數目
hbase(main):003:0> alter 'staff', NAME => 'info', VERSIONS => 5
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.4370 seconds
設置只讀
hbase(main):010:0> alter 'staff', READONLY
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.5380 seconds
exit
退出 shell 就使用 exit 就 ok 了。
hbase(main):015:0> exit