Hbase的Shell命令
# Hbase的基本命令
参考文档:Hbase shell常用命令 (opens new window)
# hbase shell
进入安装有Hbase的设备,执行hbase shell命令。这里最好使用hbase用户
[root@test028 opt]# hbase shell
Java HotSpot(TM) 64-Bit Server VM warning: Using incremental CMS is deprecated and will likely be removed in a future release
17/12/20 16:35:45 INFO 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 1.2.0-cdh5.11.0, rUnknown, Wed Apr 5 20:10:34 PDT 2017
hbase(main):001:0> #从这里你就进入了Hbase的控制界面
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# help
help命令用于查看Hbase里的所有命令及示例
hbase(main):041:0* help
HBase Shell, version 1.2.0-cdh5.7.1, rUnknown, Wed Jun 1 16:30:06 PDT 2016
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_mob, compact_rs, flush, major_compact, major_compact_mob, merge_region, move, normalize, normalizer_enabled, normalizer_switch, split, 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, 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, list_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):042:0>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
通过help命令,可以展示出所有的命令组,可以使用help '组名称'
来查看某一组命令的详细示例。
例如:help 'ddl'
也可以查看某个命令的详细使用, 例如:help 'create'
hbase(main):050:0* help 'create'
Creates a table. Pass a table name, and a set of column family
specifications (at least one), and, optionally, table configuration.
Column specification can be a simple string (name), or a dictionary
(dictionaries are described below in main help output), necessarily
including NAME attribute.
Examples:
Create a table with namespace=ns1 and table qualifier=t1
hbase> create 'ns1:t1', {NAME => 'f1', VERSIONS => 5}
Create a table with namespace=default and table qualifier=t1
hbase> create 't1', {NAME => 'f1'}, {NAME => 'f2'}, {NAME => 'f3'}
hbase> # The above in shorthand would be the following:
hbase> create 't1', 'f1', 'f2', 'f3'
hbase> create 't1', {NAME => 'f1', VERSIONS => 1, TTL => 2592000, BLOCKCACHE => true}
hbase> create 't1', {NAME => 'f1', CONFIGURATION => {'hbase.hstore.blockingStoreFiles' => '10'}}
Table configuration options can be put at the end.
Examples:
hbase> create 'ns1:t1', 'f1', SPLITS => ['10', '20', '30', '40']
hbase> create 't1', 'f1', SPLITS => ['10', '20', '30', '40']
hbase> create 't1', 'f1', SPLITS_FILE => 'splits.txt', OWNER => 'johndoe'
hbase> create 't1', {NAME => 'f1', VERSIONS => 5}, METADATA => { 'mykey' => 'myvalue' }
hbase> # Optionally pre-split the table into NUMREGIONS, using
hbase> # SPLITALGO ("HexStringSplit", "UniformSplit" or classname)
hbase> create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit'}
hbase> create 't1', 'f1', {NUMREGIONS => 15, SPLITALGO => 'HexStringSplit', REGION_REPLICATION => 2, CONFIGURATION => {'hbase.hregion.scan.loadColumnFamiliesOnDemand' => 'true'}}
hbase> create 't1', {NAME => 'f1', DFS_REPLICATION => 1}
You can also keep around a reference to the created table:
hbase> t1 = create 't1', 'f1'
Which gives you a reference to the table named 't1', on which you can then
call methods.
hbase(main):051:0>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# status
status命令可以查看hbase组成
hbase(main):001:0> status
1 active master, 0 backup masters, 4 servers, 0 dead, 1.5000 average load
1
2
2
# version
该命令返回HBase系统使用的版本。它的语法如下:
hbase(main):002:0> version
1.2.0-cdh5.11.0, rUnknown, Wed Apr 5 20:10:34 PDT 2017
1
2
2
# whoami
该命令返回HBase用户详细信息。如果执行这个命令,返回当前执行HBase shell 的用户
hbase(main):003:0> whoami
hdfs (auth:SIMPLE)
groups: hdfs, hadoop
1
2
3
2
3
# create
在HBase shell中创建表的语法如下所示
create '<table name>','<column family>' #其中column family 就是列族的意思
1
下面给出的是一个表名为test的样本模式。它有两个列族:'c1'和'c2'
Row key c1 c2
-- -- --
-- -- --
1
2
3
2
3
创建命令 create 'test', 'c1', 'c2'
hbase(main):003:0> create 'test','c1','c2'
0 row(s) in 28.3230 seconds
=> Hbase::Table - test
1
2
3
4
2
3
4
# list
列出Hbase中的所有的表
hbase(main):002:0> list
TABLE
TEST_IA_TF
ismhash
ismproinfo
mauser_info
test #可以看到我们刚刚创建的test表
test01
6 row(s) in 0.0080 seconds
=> ["TEST_IA_TF", "ismhash", "ismproinfo", "mauser_info", "test", "test01"]
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# exists
查看某个表是否存在 exists 'test'
hbase(main):008:0> exists 'test'
Table test does exist
0 row(s) in 0.0670 seconds
1
2
3
2
3
# describe
查看表的说明 describe 'table name'
hbase(main):018:0> describe 'test'
Table test is ENABLED #显示这个表是enable的状态
test
COLUMN FAMILIES DESCRIPTION
{NAME => 'c1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BL
OCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
{NAME => 'c2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BL
OCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
2 row(s) in 0.1250 seconds
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# disable 和 is_disabled
Hbase 禁用表 和 is_disabled '表名'
检查表是否被禁用
hbase(main):034:0> disable 'test'
0 row(s) in 1.2890 seconds
禁用表之后,仍然可以通过 list 命令查看到。
hbase(main):043:0> is_disabled 'test'
true
0 row(s) in 0.0110 seconds
也可以使用disable_all 来批量禁用,但是不建议这么做,除非你比较熟悉
hbase> disable_all 'r.*'
此命令用于禁用所有匹配给定正则表达式的表。disable_all命令的语法如下。
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# enable 和 is_enabled
启用表 enable 'test'
,is_enabled 'test'
检查表是否启用
hbase(main):049:0> enable 'test'
0 row(s) in 1.2670 seconds
hbase(main):050:0> is_enabled 'test'
true
0 row(s) in 0.0080 seconds
1
2
3
4
5
6
2
3
4
5
6
# drop
删除表 用drop命令可以删除表。在删除一个表之前必须先将其禁用。
hbase(main):009:0> disable 'test' #禁用表
0 row(s) in 1.2970 seconds
hbase(main):012:0> is_disabled 'test' #检测表是否被禁用了
true
0 row(s) in 0.0080 seconds
hbase(main):013:0> drop 'test' #删除表
0 row(s) in 1.2480 seconds
hbase(main):015:0> exists 'test' #检验表是否存在
Table test does not exist
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# put
在Hbase中插入数据
创建表
hbase(main):003:0> create 'test','c1','c2'
0 row(s) in 28.3230 seconds
=> Hbase::Table - test
1
2
3
4
2
3
4
插入数据
hbase(main):003:0> put 'test','1','c1:name','dailiang' # 1 是行
0 row(s) in 0.1290 seconds
数据结构如下:
Row key c1 c2
ID name --
1 dailiang --
当然c1 这个列族还可 以插入别的列,目前只插入了name列
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# get
使用get读取数据
hbase(main):001:0> get 'test','1'
COLUMN CELL
c1:name timestamp=1513775745720, value=dailiang
1 row(s) in 0.2120 seconds
1
2
3
4
2
3
4
下面给出的是语法,使用get方法读取指定列。
hbase>get 'table name', 'rowid', {COLUMN => 'column family:column name '}
1
下面给出的示例,是用于读取HBase表中的特定列。
hbase(main):003:0> get 'test', '1', {COLUMN=>'c1:name'}
COLUMN CELL
c1:name timestamp=1513775745720, value=dailiang
1 row(s) in 0.0120 seconds
1
2
3
4
2
3
4
# scan
scan 命令用于查看HTable数据。使用 scan 命令可以得到表中的数据。它的语法如下:
scan '<table name>'
下面的示例演示了如何使用scan命令从表中读取数据。在这里读取的是test表。
hbase(main):004:0> scan test1
NameError: undefined local variable or method 'test1' for #<Object:0x49fdbe2b>
hbase(main):005:0> scan 'test'
ROW COLUMN+CELL
1 column=c1:name, timestamp=1513775745720, value=dailiang
1 row(s) in 0.0450 seconds
1
2
3
4
5
6
7
2
3
4
5
6
7
# Hbase和Hive的集成
# 外部表
-- 先在Hbase中创建hbase表
disable 'test_zfang'
create 'test_zfang','c1'
put 'test_zfang', 'row1', 'c1:user_name', 'zhangsan'
put 'test_zfang', 'row1', 'c1:user_age', '29'
put 'test_zfang', 'row1', 'c1:user_addr', '北京市'
-- 然后在Hive中创建外部表,这样数据会引用hbase中的数据
create external table pet_medical.hbase_hive_zfang(
key string
,user_name string
,user_age int
,user_addr string
)
stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler'
with serdeproperties ("hbase.columns.mapping"=":key,c1:user_name,c1:user_age,c1:user_addr")
tblproperties("hbase.table.name"="test_zfang");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
最后更新时间: 2022/7/23 10:17:11