1、hbase shell 进入hbase
[ hadoop@mdw ~]$
hbase shell
HBase Shell; enter 'help' for list of supported commands.
Type "exit" to leave the HBase Shell
Version 0.94.16, r1557241, Fri Jan 10 20:10:24 UTC 2014
2、whoami 查看当前访问hbase的用户
hbase(main):001:0>
whoami
hadoop
3、version 查看当前hbase的版本信息
hbase(main):002:0>
version
0.94.16, r1557241, Fri Jan 10 20:10:24 UTC 2014
4、status 查看当前hbase的状态
hbase(main):003:0>
status
2 servers, 0 dead, 1.0000 average load
也可以通过增加参数查看,status 'simple' 查看简单的信息、status 'summary' 查看概要信息、status 'detailed' 查看详细信息
5、create 创建表
hbase(main):007:0>
create 'member','member_id','address','info'
0 row(s) in 1.1590 seconds
6、list 查看数据库中的表
hbase(main):008:0>
list
TABLE
member
1 row(s) in 0.0280 seconds
7、describe 表名 查看表的信息
hbase(main):009:0>
describe member
NameError: undefined local variable or method `member' for #
hbase(main):010:0> describe 'member'
DESCRIPTION ENABLED
'member', {NAME => '
address', DATA_BLOCK_ENCODING => 'NONE', BLOOMF true
ILTER => 'NONE', REPLICATION_SCOPE => '0', VERSIONS => '3', COMPRES
SION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', KEEP_DELE
TED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', E
NCODE_ON_DISK => 'true', BLOCKCACHE => 'true'}, {NAME => '
info', DA
TA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICATION_SCO
PE => '0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS =>
'0', TTL => '2147483647', KEEP_DELETED_CELLS => 'false', BLOCKSIZE
=> '65536', IN_MEMORY => 'false', ENCODE_ON_DISK => 'true', BLOCKCA
CHE => 'true'}, {NAME => '
member_id', DATA_BLOCK_ENCODING => 'NONE'
, BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', VERSIONS => '3',
COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', K
EEP_DELETED_CELLS => 'false', BLOCKSIZE => '65536', IN_MEMORY => 'f
alse', ENCODE_ON_DISK => 'true', BLOCKCACHE => 'true'}
1 row(s) in 0.0450 seconds
hbase(main):011:0>
8、删除列族
删除列族
需要先禁用表,否则会报以下错误:
hbase(main):006:0>
alter 'member',NAME=>'member_id',METHOD=>'delete'
ERROR: org.apache.hadoop.hbase.TableNotDisabledException: org.apache.hadoop.hbase.TableNotDisabledException: member
at org.apache.hadoop.hbase.master.HMaster.checkTableModifiable(HMaster.java:1525)
at org.apache.hadoop.hbase.master.handler.TableEventHandler.(TableEventHandler.java:72)
at org.apache.hadoop.hbase.master.handler.TableDeleteFamilyHandler.(TableDeleteFamilyHandler.java:41)
at org.apache.hadoop.hbase.master.HMaster.deleteColumn(HMaster.java:1430)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.hadoop.hbase.ipc.WritableRpcEngine$Server.call(WritableRpcEngine.java:323)
at org.apache.hadoop.hbase.ipc.HBaseServer$Handler.run(HBaseServer.java:1426)
删除列族的正确方法:
先禁用表:
hbase(main):007:0>
disable 'member'
0 row(s) in 1.1690 seconds
然后删除列族(注意NAME和METHOD要大写):
hbase(main):008:0>
alter 'member',NAME=>'member_id',METHOD=>'delete'
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 1.1640 seconds
删除列族之后再启用表:
hbase(main):009:0>
enable 'member'
0 row(s) in 1.2250 seconds
再次查看表信息,便可以发现member_id已经被删除
hbase(main):010:0>
describe 'member'
DESCRIPTION ENABLED
'member', {NAME => '
address', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => ' true
0', VERSIONS => '3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', KEEP_DELETED_CELLS =
> 'false', BLOCKSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK => 'true', BLOCKCACHE => 'true'}, {N
AME => '
info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', VERSIONS =>
'3', COMPRESSION => 'NONE', MIN_VERSIONS => '0', TTL => '2147483647', KEEP_DELETED_CELLS => 'false', BLOC
KSIZE => '65536', IN_MEMORY => 'false', ENCODE_ON_DISK => 'true', BLOCKCACHE => 'true'}
1 row(s) in 0.0420 seconds
9、drop 表名 删除表
删除表时也需要先将表disable,否则会报错,为方便先创建个临时表(user表),然后将user表删除
hbase(main):019:0> create 'user','name','age'
0 row(s) in 1.0900 seconds
没有禁用user表,直接删除,报以下错误:
hbase(main):020:0>
drop 'user'
ERROR: Table user is enabled. Disable it first.'
Here is some help for this command:
Drop the named table. Table must first be disabled: e.g. "hbase> drop 't1'"
正确删除方法如下:
先禁用表:
hbase(main):021:0>
disable 'user'
0 row(s) in 1.1660 seconds
然后删除表:
hbase(main):022:0>
drop 'user'
0 row(s) in 1.0710 seconds
hbase(main):023:0>
10、exists 表名 查看一个表是否存在
hbase(main):023:0>
exists 'user'
Table user
does not exist
0 row(s) in 0.0230 seconds
hbase(main):024:0>
exists 'member'
Table member
does exist
0 row(s) in 0.0750 seconds
11、is_enabled 判断表是否enable
hbase(main):025:0>
is_enabled 'member'
true
0 row(s) in 0.0160 seconds
12、is_disabled 判断表是否disable
hbase(main):026:0>
is_disabled 'member'
false
0 row(s) in 0.0220 seconds
13、put 插入数据
hbase(main):076:0>
put 'member','xueba','info:age','25'
0 row(s) in 0.0170 seconds
为了方便使用下面命令的,这里多插入一些数据:
put 'member','xueba','info:age','25'
put 'member','xueba','info:birthday','1989-06-19'
put 'member','xueba','info:company','tecent'
put 'member','xueba','address:contry','china'
put 'member','xueba','address:province','guangdong'
put 'member','xueba','address:city','shenzhen'
put 'member','xiaoming','info:age','24'
put 'member','xiaoming','info:birthday','1990-03-22'
put 'member','xiaoming','info:company','tecent'
put 'member','xiaoming','info:favorite','movie'
put 'member','xiaoming','address:contry','china'
put 'member','xiaoming','address:province','guangdong'
put 'member','xiaoming','address:city','guangzhou'
14、get 获取表数据
获取xueba的信息:
hbase(main):090:0>
get 'member','xueba'
COLUMN CELL
address:city timestamp=1436932081705, value=shenzhen
address:contry timestamp=1436932081662, value=china
address:province timestamp=1436932081686, value=guangdong
info:age timestamp=1436932081590, value=25
info:birthday timestamp=1436932081619, value=1989-06-19
info:company timestamp=1436932081642, value=tecent
6 row(s) in 0.0080 seconds
获取xiaoming的info信息:
hbase(main):091:0>
get 'member','xiaoming','info'
COLUMN CELL
info:age timestamp=1436932081724, value=24
info:birthday timestamp=1436932081745, value=1990-03-22
info:company timestamp=1436932081764, value=tecent
info:favorite timestamp=1436932081784, value=movie
4 row(s) in 0.0560 seconds
获取xiaoming的age信息
hbase(main):092:0>
get 'member','xiaoming','info:age'
COLUMN CELL
info:age timestamp=1436932081724, value=24
1 row(s) in 0.0060 seconds
15、put 更新记录
将xueba的age改为26:
hbase(main):093:0>
put 'member','xueba','info:age','26'
0 row(s) in 0.0120 seconds
查看修改结果:
hbase(main):094:0>
get 'member','xueba','info:age'
COLUMN CELL
info:age timestamp=1436932900139, value=26
1 row(s) in 0.0070 seconds
16、通过timestamp获取数据
hbase(main):099:0>
get 'member','xueba',{COLUMN=>'info:age',TIMESTAMP=>1436936294957}
COLUMN CELL
info:age timestamp=1436936294957, value=26
1 row(s) in 0.0100 seconds
17、scan 表名 全表扫描
hbase(main):101:0>
scan 'member'
ROW COLUMN+CELL
xiaoming column=address:city, timestamp=1436932131672, value=guangzhou
xiaoming column=address:contry, timestamp=1436932081802, value=china
xiaoming column=address:province, timestamp=1436932081821, value=guangdong
xiaoming column=info:age, timestamp=1436932081724, value=24
xiaoming column=info:birthday, timestamp=1436932081745, value=1990-03-22
xiaoming column=info:company, timestamp=1436932081764, value=tecent
xiaoming column=info:favorite, timestamp=1436932081784, value=movie
xueba column=address:city, timestamp=1436932081705, value=shenzhen
xueba column=address:contry, timestamp=1436932081662, value=china
xueba column=address:province, timestamp=1436932081686, value=guangdong
xueba column=info:age, timestamp=1436936294957, value=26
xueba column=info:birthday, timestamp=1436932081619, value=1989-06-19
xueba column=info:company, timestamp=1436932081642, value=tecent
2 row(s) in 0.0270 seconds
这里数据是2行,分别是xiaoming、xueba
18、delete 删除数据
删除xueba的age数据
hbase(main):102:0>
delete 'member','xueba','info:age'
0 row(s) in 0.0400 seconds
删除后查看xueba信息,已没有age数据
hbase(main):103:0>
get 'member','xueba'
COLUMN CELL
address:city timestamp=1436932081705, value=shenzhen
address:contry timestamp=1436932081662, value=china
address:province timestamp=1436932081686, value=guangdong
info:birthday timestamp=1436932081619, value=1989-06-19
info:company timestamp=1436932081642, value=tecent
5 row(s) in 0.0160 seconds
19、deleteall 删除整行
删除xiaoming的数据:
hbase(main):104:0>
deleteall 'member','xiaoming'
0 row(s) in 0.0100 seconds
查看删除情况,可以看出已没有xiaoming的数据
hbase(main):105:0>
get 'member','xiaoming'
COLUMN CELL
0 row(s) in 0.0050 seconds
20、count 表名 查询表中总共有多少行数据
hbase(main):106:0>
count 'member'
1 row(s) in 0.0200 seconds
总共插入两行数据,上面将xiaoming的数据全部删除了,所以只剩一行xueba的数据
21、truncate 表名 清空表
hbase(main):107:0>
truncate 'member'
Truncating 'member' table (it may take a while):
- Disabling table...
- Dropping table...
- Creating table...
0 row(s) in 3.2460 seconds
通过上面信息可以看出,清空表的操作是先禁用表,然后删除表,再重新创建表