1. 基本操作
1.1 進入Hbase客戶端命令行
執行命令root@master:/opt/module/cdh/hbase-1.3.1/bin# ./hbase shell
進入客戶端。
1.2 查看幫助命令
執行help
得到結果:
hbase(main):001:0> help
HBase Shell, version 1.3.1, r930b9a55528fe45d8edce7af42fef2d35e77677a, Thu Apr 6 19:36:54 PDT 2017
Type 'help "COMMAND"', (e.g. 'help "get"' -- the quotes are necessary) for help on a specific command.
Commands are grouped. Type 'help "COMMAND_GROUP"', (e.g. 'help "general"') for help on a command group.
COMMAND GROUPS:
Group name: general
Commands: status, table_help, version, whoami
Group name: ddl
Commands: alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list, locate_region, show_filters
Group name: namespace
Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables
Group name: dml
Commands: append, count, delete, deleteall, get, get_counter, get_splits, incr, put, scan, truncate, truncate_preserve
Group name: tools
Commands: assign, balance_switch, balancer, balancer_enabled, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, close_region, compact, compact_rs, flush, major_compact, merge_region, move, normalize, normalizer_enabled, normalizer_switch, split, splitormerge_enabled, splitormerge_switch, trace, unassign, wal_roll, zk_dump
Group name: replication
Commands: add_peer, append_peer_tableCFs, disable_peer, disable_table_replication, enable_peer, enable_table_replication, get_peer_config, list_peer_configs, list_peers, list_replicated_tables, remove_peer, remove_peer_tableCFs, set_peer_tableCFs, show_peer_tableCFs
Group name: snapshots
Commands: clone_snapshot, delete_all_snapshot, delete_snapshot, delete_table_snapshots, list_snapshots, list_table_snapshots, restore_snapshot, snapshot
Group name: configuration
Commands: update_all_config, update_config
Group name: quotas
Commands: list_quotas, set_quota
Group name: security
Commands: grant, list_security_capabilities, revoke, user_permission
Group name: procedures
Commands: abort_procedure, list_procedures
Group name: visibility labels
Commands: add_labels, clear_auths, get_auths, list_labels, set_auths, set_visibility
SHELL USAGE:
Quote all names in HBase Shell such as table and column names. Commas delimit
command parameters. Type <RETURN> after entering a command to run it.
Dictionaries of configuration used in the creation and alteration of tables are
Ruby Hashes. They look like this:
{'key1' => 'value1', 'key2' => 'value2', ...}
and are opened and closed with curley-braces. Key/values are delimited by the
'=>' character combination. Usually keys are predefined constants such as
NAME, VERSIONS, COMPRESSION, etc. Constants do not need to be quoted. Type
'Object.constants' to see a (messy) list of all constants in the environment.
If you are using binary keys or values and need to enter them in the shell, use
double-quote'd hexadecimal representation. For example:
hbase> get 't1', "key\x03\x3f\xcd"
hbase> get 't1', "key\003\023\011"
hbase> put 't1', "test\xef\xff", 'f1:', "\x01\x33\x40"
The HBase shell is the (J)Ruby IRB with the above HBase-specific commands added.
For more on the HBase Shell, see http://hbase.apache.org/book.html
hbase(main):002:0>
最重要的是:
Group name: ddl
Commands: alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list, locate_region, show_filters
Group name: dml
Commands: append, count, delete, deleteall, get, get_counter, get_splits, incr, put, scan, truncate, truncate_preserve
1.3 查看錶列表
執行命令list
2. 表的操作-DDL
2.1 創建表-create
執行命令:create 'stu','info1','info2'
,創建了一個stu
表,有兩個列簇。
2.2 查看錶結構-describe
執行命令:describe 'stu'
這裏面的VERSIONS
是1 表示數據版本號只能存放一個版本。只能返回一個數據。這個是可以修改的,可以定義存放的版本數。比如是2,則留下數據的兩個版本號。
2.3 變更表信息-alter
修改的最小單元是列簇。執行命令:alter 'stu',{NAME=>'info1',VERSIONS=>3}
改表結構還是ddl操作,修改的是元數據。
查看錶結構:
2.4 刪除表
要想刪除表,需要先disable
該表,然後才能drop
表。
執行命令:disable 'student'
刪除表:drop 'student'
這樣就能刪除成功了。
並且創建表必須要有至少一個列族,否則會創建失敗。
2.5 命名空間-namespace
-
查看命名空間,命令是
list_namespace
-
創建命名空間,命令是
create_namespace
查看列表 -
在命名空間
bigdata
中創建表執行命令:
create 'bigdata:stu','info1'
,一定是命名空間:表名
呢麼如何刪除命名空間?首先要清楚命名空間的所有表,然後刪除命名空間。
2. 表的操作-DML
2.1 插入數據-put
插入命令:put '表','rowkey','列名','value值'
示例:put 'stu','1001','info1:name','zhanglaing'
2.2 查詢數據表-scan,get
scan
的示例有很多:
scan
既可以整表掃描,也可以使用過濾條件等等
scan
查詢示例:
scan
可以直接查詢表,但是get
不可以,get
需要指定表名
和rowkey
get
示例:
操作示例:
對stu
表插入一些數據,並做一些查詢操作:
hbase(main):007:0> put 'stu','1001','info1:sex','male'
0 row(s) in 0.1020 seconds
hbase(main):008:0> put 'stu','1001','info2:addr','shanghai'
0 row(s) in 0.0170 seconds
hbase(main):009:0> put 'stu','1002','info1:phone','1231231'
0 row(s) in 0.0360 seconds
hbase(main):010:0> put 'stu','1002','info2:addr','beijing'
0 row(s) in 0.0070 seconds
hbase(main):011:0> scan 'stu'
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1577015138401, value=zhanglaing
1001 column=info1:sex, timestamp=1577016467811, value=male
1001 column=info2:addr, timestamp=1577016497189, value=shanghai
1002 column=info1:phone, timestamp=1577016555983, value=1231231
1002 column=info2:addr, timestamp=1577016586094, value=beijing
2 row(s) in 0.0260 seconds
hbase(main):012:0> get 'stu','1001','info1'
COLUMN CELL
info1:name timestamp=1577015138401, value=zhanglaing
info1:sex timestamp=1577016467811, value=male
1 row(s) in 0.0260 seconds
hbase(main):013:0> get 'stu','1001'
COLUMN CELL
info1:name timestamp=1577015138401, value=zhanglaing
info1:sex timestamp=1577016467811, value=male
info2:addr timestamp=1577016497189, value=shanghai
1 row(s) in 0.0090 seconds
hbase(main):014:0> get 'stu','1001','info1:name'
COLUMN CELL
info1:name timestamp=1577015138401, value=zhanglaing
1 row(s) in 0.0670 seconds
hbase(main):015:0>
呢麼如果我們使用scan
來做這些操作:
hbase(main):001:0> scan 'stu'
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1577017030315, value=lisi
1001 column=info1:sex, timestamp=1577016467811, value=male
1001 column=info2:addr, timestamp=1577016497189, value=shanghai
1002 column=info1:phone, timestamp=1577016555983, value=1231231
1002 column=info2:addr, timestamp=1577016586094, value=beijing
2 row(s) in 0.4540 seconds
hbase(main):002:0> scan 'stu',{COLUMNS => ['info1:name', 'info1:sex']}
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1577017030315, value=lisi
1001 column=info1:sex, timestamp=1577016467811, value=male
1 row(s) in 0.0650 seconds
hbase(main):003:0> scan 'stu',{COLUMNS=>'info1:name'}
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1577017030315, value=lisi
1 row(s) in 0.0130 seconds
hbase(main):004:0> scan 'stu',{STARTROW=>'1001'}
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1577017030315, value=lisi
1001 column=info1:sex, timestamp=1577016467811, value=male
1001 column=info2:addr, timestamp=1577016497189, value=shanghai
1002 column=info1:phone, timestamp=1577016555983, value=1231231
1002 column=info2:addr, timestamp=1577016586094, value=beijing
2 row(s) in 0.0450 seconds
hbase(main):005:0> scan 'stu',{STARTROW=>'1001',STOPROW=>'1002'}
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1577017030315, value=lisi
1001 column=info1:sex, timestamp=1577016467811, value=male
1001 column=info2:addr, timestamp=1577016497189, value=shanghai
1 row(s) in 0.0310 seconds
hbase(main):006:0>
這裏注意一下,rowkey
是按照字典排序
的,當我們插入10010
時,你會發現他會在1001的後面
2.3 修改數據-put
HBase是沒有UPDATE
這種命令,但是可以通過put
通過添加新數據去覆蓋舊數據。
修改示例:
呢麼以前的舊數據在哪裏?其實還在Region Servers
的內存中,可以通過scan
顯示出來:
scan 'stu',{RAW=>true,VERSIONS=>10}
表示顯示全量數據,並且顯示版本號在10個以內的數據。
顯示多個版本也可以使用get
命令:
get 'stu','1001',{COLUMN=>'info1:name',VERSIONS=>3}
在我們查詢數據的時候,只顯示時間戳最大的數據。
還有一點,這裏的版本VERSIONS
是指HBase會存儲的最大的版本數,但是如果設置的默認存儲版本爲1,呢麼這裏的VERSIONS
設置多少都只能顯示一個。所以這裏的數字要小於等於設置的版本數,即:alter 'stu',{NAME=>'info',VERSIONS=>3}
這裏的數字3。(我寫的是3,可以使其他數字)。
2.4 刪除數據-delete,deleteall,truncate
delete
刪除示例:(三個參數)
刪除操作其實也是在插入新數據,只不過插入的不是value而是type類型。
操作示例:
掃描一下整個表:
我們刪除1001,info1:name
,
可以看到我們已經沒有這條數據了,但是在全量顯示中是怎麼顯示的呢?
1001 column=info1:name, timestamp=1577021426252, type=DeleteColumn
沒錯,在後面插入了一個type=DeleteColumn
的 數據,當我們掃描整張表的時候獲取最大的時間戳,得到了這一條數據,但是是個刪除標記,所以不會顯示該列數據。
驗證:(添加一條數據,但是時間戳在刪除的時間戳小几秒)
執行delete 'stu','1001','info1'
,在命令行是沒有效果的,但是在API中是可以刪除的。
如果想刪除一整行數據,可以使用deleteall
操作示例deleteall 'stu','1002'
:
如果需要清楚整個表,呢麼就使用truncate
操作實例truncate 'stu'
:
先禁掉表,再刪除表。
最後總結
無論是增刪改查使用的最重要的就是時間戳
!!!