HBase Shell一些基本操作命令的说明:
base shell命令 | 描述 |
alter | 修改列族(column family)模式 |
count | 统计表中行的数量 |
create | 创建表 |
describe | 显示表相关的详细信息 |
delete | 删除指定对象的值(可以为表,行,列对应的值,另外也可以指定时间戳的值) |
deleteall | 删除指定行的所有元素值 |
disable | 使表无效 |
drop | 删除表 |
enable | 使表有效 |
exists | 测试表是否存在 |
exit | 退出hbase shell |
get | 获取行或单元(cell)的值 |
incr | 增加指定表,行或列的值 |
list | 列出hbase中存在的所有表 |
put | 向指向的表单元添加值 |
tools | 列出hbase所支持的工具 |
scan | 通过对表的扫描来获取对用的值 |
status | 返回hbase集群的状态信息 |
shutdown | 关闭hbase集群(与exit不同) |
truncate | 重新创建指定表 |
version | 查看hbase版本信息 |
一、一般操作
1,查询服务器状态
hbase(main):006:0> status
3 servers, 0 dead, 0.6667 average load
2,查询版本信息
hbase(main):007:0> version
0.98.15-hadoop2, r8a356c2c07e1db40f3c10e0aec30a72cc69937b7, Fri Oct 2 19:12:23 PDT 201
3,列出所支持的工具
hbase(main):008:0> tools
Command: assign
Assign a region. Use with caution. If region already assigned,
this command will do a force reassign. For experts only.
Examples:
hbase> assign 'REGIONNAME'
hbase> assign 'ENCODED_REGIONNAME'
Command: balance_switch
Enable/Disable balancer. Returns previous balancer state.
Examples:
hbase> balance_switch true
hbase> balance_switch false
Command: balancer
Trigger the cluster balancer. Returns true if balancer ran and was able to
tell the region servers to unassign all the regions to balance (the re-assignment itself is async).
Otherwise false (Will not run if regions in transition).
Command: balancer_enabled
Query the balancer's state.
Examples:
hbase> balancer_enabled
Command: catalogjanitor_enabled
Query for the CatalogJanitor state (enabled/disabled?)
Examples:
hbase> catalogjanitor_enabled
Command: catalogjanitor_run
Catalog janitor command to run the (garbage collection) scan from command line.
hbase> catalogjanitor_run
Command: catalogjanitor_switch
Enable/Disable CatalogJanitor. Returns previous CatalogJanitor state.
Examples:
hbase> catalogjanitor_switch true
hbase> catalogjanitor_switch false
Command: close_region
Close a single region. Ask the master to close a region out on the cluster
or if 'SERVER_NAME' is supplied, ask the designated hosting regionserver to
close the region directly. Closing a region, the master expects 'REGIONNAME'
to be a fully qualified region name. When asking the hosting regionserver to
directly close a region, you pass the regions' encoded name only. A region
name looks like this:
TestTable,0094429456,1289497600452.527db22f95c8a9e0116f0cc13c680396.
or
Namespace:TestTable,0094429456,1289497600452.527db22f95c8a9e0116f0cc13c680396.
The trailing period is part of the regionserver name. A region's encoded name
is the hash at the end of a region name; e.g. 527db22f95c8a9e0116f0cc13c680396
(without the period). A 'SERVER_NAME' is its host, port plus startcode. For
example: host187.example.com,60020,1289493121758 (find servername in master ui
or when you do detailed status in shell). This command will end up running
close on the region hosting regionserver. The close is done without the
master's involvement (It will not know of the close). Once closed, region will
stay closed. Use assign to reopen/reassign. Use unassign or move to assign
the region elsewhere on cluster. Use with caution. For experts only.
Examples:
hbase> close_region 'REGIONNAME'
hbase> close_region 'REGIONNAME', 'SERVER_NAME'
hbase> close_region 'ENCODED_REGIONNAME'
hbase> close_region 'ENCODED_REGIONNAME', 'SERVER_NAME'
Command: compact
Compact all regions in passed table or pass a region row
to compact an individual region. You can also compact a single column
family within a region.
Examples:
Compact all regions in a table:
hbase> compact 'ns1:t1'
hbase> compact 't1'
Compact an entire region:
hbase> compact 'r1'
Compact only a column family within a region:
hbase> compact 'r1', 'c1'
Compact a column family within a table:
hbase> compact 't1', 'c1'
Command: compact_rs
Compact all regions on passed regionserver.
Examples:
Compact all regions on a regionserver:
hbase> compact_rs 'host187.example.com,60020'
or
hbase> compact_rs 'host187.example.com,60020,1289493121758'
Major compact all regions on a regionserver:
hbase> compact_rs 'host187.example.com,60020,1289493121758', true
Command: flush
Flush all regions in passed table or pass a region row to
flush an individual region. For example:
hbase> flush 'TABLENAME'
hbase> flush 'REGIONNAME'
hbase> flush 'ENCODED_REGIONNAME'
Command: hlog_roll
Roll the log writer. That is, start writing log messages to a new file.
The name of the regionserver should be given as the parameter. A
'server_name' is the host, port plus startcode of a regionserver. For
example: host187.example.com,60020,1289493121758 (find servername in
master ui or when you do detailed status in shell)
Command: major_compact
Run major compaction on passed table or pass a region row
to major compact an individual region. To compact a single
column family within a region specify the region name
followed by the column family name.
Examples:
Compact all regions in a table:
hbase> major_compact 't1'
hbase> major_compact 'ns1:t1'
Compact an entire region:
hbase> major_compact 'r1'
Compact a single column family within a region:
hbase> major_compact 'r1', 'c1'
Compact a single column family within a table:
hbase> major_compact 't1', 'c1'
Command: merge_region
Merge two regions. Passing 'true' as the optional third parameter will force
a merge ('force' merges regardless else merge will fail unless passed
adjacent regions. 'force' is for expert use only).
NOTE: You must pass the encoded region name, not the full region name so
this command is a little different from other region operations. The encoded
region name is the hash suffix on region names: e.g. if the region name were
TestTable,0094429456,1289497600452.527db22f95c8a9e0116f0cc13c680396. then
the encoded region name portion is 527db22f95c8a9e0116f0cc13c680396
Examples:
hbase> merge_region 'ENCODED_REGIONNAME', 'ENCODED_REGIONNAME'
hbase> merge_region 'ENCODED_REGIONNAME', 'ENCODED_REGIONNAME', true
Command: move
Move a region. Optionally specify target regionserver else we choose one
at random. NOTE: You pass the encoded region name, not the region name so
this command is a little different to the others. The encoded region name
is the hash suffix on region names: e.g. if the region name were
TestTable,0094429456,1289497600452.527db22f95c8a9e0116f0cc13c680396. then
the encoded region name portion is 527db22f95c8a9e0116f0cc13c680396
A server name is its host, port plus startcode. For example:
host187.example.com,60020,1289493121758
Examples:
hbase> move 'ENCODED_REGIONNAME'
hbase> move 'ENCODED_REGIONNAME', 'SERVER_NAME'
Command: split
Split entire table or pass a region to split individual region. With the
second parameter, you can specify an explicit split key for the region.
Examples:
split 'tableName'
split 'namespace:tableName'
split 'regionName' # format: 'tableName,startKey,id'
split 'tableName', 'splitKey'
split 'regionName', 'splitKey'
Command: trace
Start or Stop tracing using HTrace.
Always returns true if tracing is running, otherwise false.
If the first argument is 'start', new span is started.
If the first argument is 'stop', current running span is stopped.
('stop' returns false on success.)
If the first argument is 'status', just returns if or not tracing is running.
On 'start'-ing, you can optionally pass the name of span as the second argument.
The default name of span is 'HBaseShell'.
Repeating 'start' does not start nested span.
Examples:
hbase> trace 'start'
hbase> trace 'status'
hbase> trace 'stop'
hbase> trace 'start', 'MySpanName'
hbase> trace 'stop'
Command: unassign
Unassign a region. Unassign will close region in current location and then
reopen it again. Pass 'true' to force the unassignment ('force' will clear
all in-memory state in master before the reassign. If results in
double assignment use hbck -fix to resolve. To be used by experts).
Use with caution. For expert use only. Examples:
hbase> unassign 'REGIONNAME'
hbase> unassign 'REGIONNAME', true
hbase> unassign 'ENCODED_REGIONNAME'
hbase> unassign 'ENCODED_REGIONNAME', true
Command: zk_dump
Dump status of HBase cluster as seen by ZooKeeper.
--------------------------------------------------------------------------------
WARNING: Above commands are for 'experts'-only as misuse can damage an install
二,DDL操作
1,创建一个表(创建一个学生表,有两个列族。info列族表示的学生的基本信息,class列族表示的他所学科目
hbase(main):009:0> create 'student','info','class'
0 row(s) in 0.5060 seconds
=> Hbase::Table - student
2,查询所创建的表
hbase(main):010:0> list
TABLE
student
1 row(s) in 0.0090 seconds
=> ["student"]
3,查询表的描述
hbase(main):011:0> describe 'student'
Table student is ENABLED
student
COLUMN FAMILIES DESCRIPTION
{NAME => 'class', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN
_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
{NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_
VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
2 row(s) in 0.0680 seconds
4,删除一个列族
hbase(main):012:0> create 'student_temp','info','class','temp'
0 row(s) in 0.7050 seconds
=> Hbase::Table - student_temp
hbase(main):013:0> alter 'student_temp',{NAME=>'temp',METHOD=>'delete'}
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 2.2710 seconds
hbase(main):014:0> describe 'student_temp'
Table student_temp is ENABLED
student_temp
COLUMN FAMILIES DESCRIPTION
{NAME => 'class', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN
_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
{NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '1', COMPRESSION => 'NONE', MIN_
VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 'true'}
2 row(s) in 0.0630 seconds
5,表生效和失效的操作
表失效
hbase(main):019:0> disable 'student_temp'
0 row(s) in 1.3730 seconds
表生效
hbase(main):020:0> enable 'student_temp'
0 row(s) in 0.2690 seconds
6,删除一张表(先disable表再删)
hbase(main):021:0> drop 'student_temp'
ERROR: Table student_temp is enabled. Disable it first.
Here is some help for this command:
Drop the named table. Table must first be disabled:
hbase> drop 't1'
hbase> drop 'ns1:t1'
hbase(main):022:0> disable 'student_temp'
0 row(s) in 1.3390 seconds
hbase(main):023:0> drop 'student_temp'
0 row(s) in 0.2190 seconds
7,查询表是否存在
hbase(main):024:0> exists 'student'
Table student does exist
0 row(s) in 0.0450 seconds
hbase(main):025:0> exists 'student1'
Table student1 does not exist
0 row(s) in 0.0380 seconds
8,判断表是否enable
hbase(main):026:0> is_enabled 'student'
true
0 row(s) in 0.0350 seconds
9,判断表是否disable
hbase(main):027:0> is_disabled 'student'
false
0 row(s) in 0.0340 seconds
三,DML操作
1,插入数据
hbase(main):028:0> put'student','xiaoming_20100901','info:name','xiaoming'
0 row(s) in 0.0590 seconds
hbase(main):029:0> put'student','xiaoming_20100901','info:age','12'
0 row(s) in 0.0070 seconds
hbase(main):030:0> put'student','xiaoming_20100901','info:address','tianhe'
0 row(s) in 0.0070 seconds
hbase(main):031:0> put'student','xiaoming_20100901','class:chinese','95'
0 row(s) in 0.0060 seconds
hbase(main):032:0> put'student','xiaoming_20100901','class:math','98'
0 row(s) in 0.0060 seconds
2,查询表中一条数据
hbase(main):033:0> get 'student','xiaoming_20100901'
COLUMN CELL
class:chinese timestamp=1530519477861, value=95
class:math timestamp=1530519495115, value=98
info:address timestamp=1530519338245, value=tianhe
info:age timestamp=1530519329836, value=12
info:name timestamp=1530519318696, value=xiaoming
5 row(s) in 0.0150 seconds
3,查询表中一条数据指定列族的数据
hbase(main):034:0> get 'student','xiaoming_20100901','info'
COLUMN CELL
info:address timestamp=1530519338245, value=tianhe
info:age timestamp=1530519329836, value=12
info:name timestamp=1530519318696, value=xiaoming
3 row(s) in 0.0070 seconds
4,查询表中一条数据中指定列族中指定列的数据
hbase(main):035:0> get 'student','xiaoming_20100901','info:name'
COLUMN CELL
info:name timestamp=1530519318696, value=xiaoming
1 row(s) in 0.0080 seconds
5,更新一条数据
hbase(main):036:0> put 'student','xiaoming_20100901','class:math','100'
0 row(s) in 0.0070 seconds
hbase(main):037:0> get 'student','xiaoming_20100901','class:math'
COLUMN CELL
class:math timestamp=1530520110007, value=100
1 row(s) in 0.0060 seconds
6,查询一条数据多个版本的数据
hbase(main):002:0> get 'student','xiaoming_20100901',{COLUMN=>'class:math',VERSIONS=>3}
COLUMN CELL
class:math timestamp=1530520936345, value=80
1 row(s) in 0.0060 seconds
7,全表扫描查询
hbase(main):008:0> scan 'student'
ROW COLUMN+CELL
xiaohua_20100901 column=class:chinese, timestamp=1530521757616, value=92
xiaohua_20100901 column=class:math, timestamp=1530521759563, value=85
xiaohua_20100901 column=info:address, timestamp=1530521757602, value=tianhe
xiaohua_20100901 column=info:age, timestamp=1530521757585, value=13
xiaohua_20100901 column=info:name, timestamp=1530521757555, value=xiaohua
xiaoming_20100901 column=class:chinese, timestamp=1530519477861, value=95
xiaoming_20100901 column=class:math, timestamp=1530520936345, value=80
xiaoming_20100901 column=info:address, timestamp=1530519338245, value=tianhe
xiaoming_20100901 column=info:age, timestamp=1530519329836, value=12
xiaoming_20100901 column=info:name, timestamp=1530519318696, value=xiaoming
2 row(s) in 0.0180 seconds
8,删除一条数据指定列族指定列
hbase(main):009:0> get 'student','xiaohua_20100901'
COLUMN CELL
class:chinese timestamp=1530521757616, value=92
class:math timestamp=1530521759563, value=85
info:address timestamp=1530521757602, value=tianhe
info:age timestamp=1530521757585, value=13
info:name timestamp=1530521757555, value=xiaohua
5 row(s) in 0.0120 seconds
hbase(main):010:0> delete 'student','xiaohua_20100901','class:math'
0 row(s) in 0.0130 seconds
hbase(main):011:0> get 'student','xiaohua_20100901'
COLUMN CELL
class:chinese timestamp=1530521757616, value=92
info:address timestamp=1530521757602, value=tianhe
info:age timestamp=1530521757585, value=13
info:name timestamp=1530521757555, value=xiaohua
4 row(s) in 0.0110 seconds
9,删除一条数据
hbase(main):012:0> scan 'student'
ROW COLUMN+CELL
xiaohua_20100901 column=class:chinese, timestamp=1530521757616, value=92
xiaohua_20100901 column=info:address, timestamp=1530521757602, value=tianhe
xiaohua_20100901 column=info:age, timestamp=1530521757585, value=13
xiaohua_20100901 column=info:name, timestamp=1530521757555, value=xiaohua
xiaoming_20100901 column=class:chinese, timestamp=1530519477861, value=95
xiaoming_20100901 column=class:math, timestamp=1530520936345, value=80
xiaoming_20100901 column=info:address, timestamp=1530519338245, value=tianhe
xiaoming_20100901 column=info:age, timestamp=1530519329836, value=12
xiaoming_20100901 column=info:name, timestamp=1530519318696, value=xiaoming
2 row(s) in 0.0250 seconds
hbase(main):013:0> deleteall 'student','xiaohua_20100901'
0 row(s) in 0.0070 seconds
hbase(main):014:0> scan 'student'
ROW COLUMN+CELL
xiaoming_20100901 column=class:chinese, timestamp=1530519477861, value=95
xiaoming_20100901 column=class:math, timestamp=1530520936345, value=80
xiaoming_20100901 column=info:address, timestamp=1530519338245, value=tianhe
xiaoming_20100901 column=info:age, timestamp=1530519329836, value=12
xiaoming_20100901 column=info:name, timestamp=1530519318696, value=xiaoming
1 row(s) in 0.0080 seconds
10,查询表中有多少行
hbase(main):015:0> count 'student'
1 row(s) in 0.0140 seconds
=> 1
11,清空一张表
hbase(main):016:0> truncate 'student'
Truncating 'student' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 1.9410 seconds
hbase(main):017:0> scan 'student'
ROW COLUMN+CELL
0 row(s) in 0.3110 seconds
注:更多的操作会在实践中去总结和修改,如有不足望大家批评和指正