1. 常用hbase命令
--进入habase
[grid@gc ~]$ hbase-0.90.5/bin/hbase shell
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 0.90.5, r1212209, Fri Dec 9 05:40:36 UTC 2011
hbase(main):001:0>
--查看数据库状态
hbase(main):002:0> status
2 servers, 0 dead, 1.0000 average load
--查询数据库版本
hbase(main):004:0> version
0.90.5, r1212209, Fri Dec 9 05:40:36 UTC 2011
--帮助命令
hbase(main):003:0> help
HBase Shell, version 0.90.5, r1212209, Fri Dec 9 05:40:36 UTC 2011
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, version
Group name: ddl
Commands: alter, create, describe, disable, drop, enable, exists, is_disabled, is_enabled, list
Group name: dml
Commands: count, delete, deleteall, get, get_counter, incr, put, scan, truncate
Group name: tools
Commands: assign, balance_switch, balancer, close_region, compact, flush, major_compact, move, split, unassign, zk_dump
Group name: replication
Commands: add_peer, disable_peer, enable_peer, remove_peer, start_replication, stop_replication
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', "keyx03x3fxcd"
hbase> get 't1', "key 03 23 11"
hbase> put 't1', "testxefxff", 'f1:', "x01x33x40"
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/docs/current/book.html
2. Hbase数据库操作命令
--创建表
resume表逻辑模型:
行键 |
时间戳 |
列族binfo |
列族edu |
列族work |
lichangzai |
T2 |
binfo:age=’1980-1-1’ |
||
T3 |
binfo:sex=’man’ |
|||
T5 |
edu:mschool=’rq no.1’ |
|||
T6 |
edu:university=’qhddx’ |
|||
T7 |
work:company1=’12580’ |
|||
changfei |
T10 |
binfo:age=’1986-2-1’ |
||
T11 |
edu:university=’bjdx’ |
|||
T12 |
work:company1=’LG’ |
|||
…… |
Tn |
--创建表
hbase(main):005:0> create 'resume','binfo','edu','work'
0 row(s) in 16.5710 seconds
--列出表
hbase(main):006:0> list
TABLE
resume
1 row(s) in 1.6080 seconds
--查看表结构
hbase(main):007:0> describe 'resume'
DESCRIPTION ENABLED
{NAME => 'resume', FAMILIES => [{NAME => 'binfo', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', C true
OMPRESSION => 'NONE', VERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => 'fals
e', BLOCKCACHE => 'true'}, {NAME => 'edu', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', COMPRESS
ION => 'NONE', VERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLO
CKCACHE => 'true'}, {NAME => 'work', BLOOMFILTER => 'NONE', REPLICATION_SCOPE => '0', COMPRESSION =>
'NONE', VERSIONS => '3', TTL => '2147483647', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACH
E => 'true'}]}
1 row(s) in 1.8590 seconds
--添加列族
hbase(main):014:0> disable 'resume'
0 row(s) in 4.2630 seconds
hbase(main):015:0> alter 'resume',name='f1'
0 row(s) in 4.6990 seconds
--删除列族
hbase(main):017:0> alter 'resume',{NAME=>'f1',METHOD=>'delete'}
0 row(s) in 1.1390 seconds
--或是
hbase(main):021:0> alter 'resume','delete' => 'f1'
0 row(s) in 1.9310 seconds
hbase(main):022:0> enable 'resume'
0 row(s) in 5.9060 seconds
注意:
(1) ddl命令是区分大小写的,像ddl中的alter,create, drop, enable等都必需用小写。而{}中的属性名都必需用大写。
(2) alter、drop表之前必需在先禁用(disabel)表,修改完后再启用表(enable)表,否则会报错
--查询禁用状态
hbase(main):024:0> is_disabled 'resume'
false
0 row(s) in 0.4930 seconds
hbase(main):021:0> is_enabled 'resume'
true
0 row(s) in 0.2450 seconds
--删除表
hbase(main):015:0> create 't1','f1'
0 row(s) in 15.3730 seconds
hbase(main):016:0> disable 't1'
0 row(s) in 6.4840 seconds
hbase(main):017:0> drop 't1'
0 row(s) in 7.3730 seconds
--查询表是否存在
hbase(main):018:0> exists 'resume'
Table resume does exist
0 row(s) in 2.3900 seconds
hbase(main):019:0> exists 't1'
Table t1 does not exist
0 row(s) in 1.3270 seconds
--插入数据
put 'resume','lichangzai','binfo:age','1980-1-1'
put 'resume','lichangzai','binfo:sex','man'
put 'resume','lichangzai','edu:mschool','rq no.1'
put 'resume','lichangzai','edu:university','qhddx'
put 'resume','lichangzai','work:company1','12580'
put 'resume','lichangzai','work:company2','china mobile'
put 'resume','lichangzai','binfo:site','blog.csdn.NET/lichangzai'
put 'resume','lichangzai','binfo:mobile','13712345678'
put 'resume','changfei','binfo:age','1986-2-1'
put 'resume','changfei','edu:university','bjdx'
put 'resume','changfei','work:company1','LG'
put 'resume','changfei','binfo:mobile','13598765401'
put 'resume','changfei','binfo:site','hi.baidu/lichangzai'
--获取一行键的所有数据
hbase(main):014:0> get 'resume','lichangzai'
COLUMN CELL
binfo:age timestamp=1356485720612, value=1980-1-1
binfo:mobile timestamp=1356485865523, value=13712345678
binfo:sex timestamp=1356485733603, value=man
binfo:site timestamp=1356485859806, value=blog.csdn.Net/lichangzai
edu:mschool timestamp=1356485750361, value=rq no.1
edu:university timestamp=1356485764211, value=qhddx
work:company1 timestamp=1356485837743, value=12580
work:company2 timestamp=1356485849365, value=china mobile
8 row(s) in 2.1090 seconds
注意:必须通过行键Row Key来查询数据
--获取一个行键,一个列族的所有数据
hbase(main):015:0> get 'resume','lichangzai','binfo'
COLUMN CELL
binfo:age timestamp=1356485720612, value=1980-1-1
binfo:mobile timestamp=1356485865523, value=13712345678
binfo:sex timestamp=1356485733603, value=man
binfo:site timestamp=1356485859806, value=blog.csdn.net/lichangzai
4 row(s) in 1.6010 seconds
--获取一个行键,一个列族中一个列的所有数据
hbase(main):017:0> get 'resume','lichangzai','binfo:sex'
COLUMN CELL
binfo:sex timestamp=1356485733603, value=man
1 row(s) in 0.8980 seconds
--更新一条记录
hbase(main):018:0> put 'resume','lichangzai','binfo:mobile','13899999999'
0 row(s) in 1.7640 seconds
hbase(main):019:0> get 'resume','lichangzai','binfo:mobile'
COLUMN CELL
binfo:mobile timestamp=1356486691591, value=13899999999
1 row(s) in 1.5710 seconds
注意:更新实质就是插入一条带有时间戳的记录,get查询时只显示最新时间的记录
--通过timestamp来获取数据
------查询最新的时间戳的数据
hbase(main):020:0> get 'resume','lichangzai',{COLUMN=>'binfo:mobile',TIMESTAMP=>1356486691591}
COLUMN CELL
binfo:mobile timestamp=1356486691591, value=13899999999
1 row(s) in 0.4060 seconds
------查之前(即删除)时间戳的数据
hbase(main):021:0> get 'resume','lichangzai',{COLUMN=>'binfo:mobile',TIMESTAMP=>1356485865523}
COLUMN CELL
binfo:mobile timestamp=1356485865523, value=13712345678
1 row(s) in 0.7780 seconds
--全表扫描
hbase(main):022:0> scan 'resume'
ROW COLUMN+CELL
changfei column=binfo:age, timestamp=1356485874056, value=1986-2-1
changfei column=binfo:mobile, timestamp=1356485897477, value=13598765401
changfei column=binfo:site, timestamp=1356485906106, value=hi.baidu/lichangzai
changfei column=edu:university, timestamp=1356485880977, value=bjdx
changfei column=work:company1, timestamp=1356485888939, value=LG
lichangzai column=binfo:age, timestamp=1356485720612, value=1980-1-1
lichangzai column=binfo:mobile, timestamp=1356486691591, value=13899999999
lichangzai column=binfo:sex, timestamp=1356485733603, value=man
lichangzai column=binfo:site, timestamp=1356485859806, value=blog.csdn.net/lichangzai
lichangzai column=edu:mschool, timestamp=1356485750361, value=rq no.1
lichangzai column=edu:university, timestamp=1356485764211, value=qhddx
lichangzai column=work:company1, timestamp=1356485837743, value=12580
lichangzai column=work:company2, timestamp=1356485849365, value=china mobile
2 row(s) in 3.6300 seconds
--删除指定行键的列族字段
hbase(main):023:0> put 'resume','changfei','binfo:sex','man'
0 row(s) in 1.2630 seconds
hbase(main):024:0> delete 'resume','changfei','binfo:sex'
0 row(s) in 0.5890 seconds
hbase(main):026:0> get 'resume','changfei','binfo:sex'
COLUMN CELL
0 row(s) in 0.5560 seconds
--删除整行
hbase(main):028:0> create 't1','f1','f2'
0 row(s) in 8.3950 seconds
hbase(main):029:0> put 't1','a','f1:col1','xxxxx'
0 row(s) in 2.6790 seconds
hbase(main):030:0> put 't1','a','f1:col2','xyxyx'
0 row(s) in 0.5130 seconds
hbase(main):031:0> put 't1','b','f2:cl1','ppppp'
0 row(s) in 1.2620 seconds
hbase(main):032:0> deleteall 't1','a'
0 row(s) in 1.2030 seconds
hbase(main):033:0> get 't1','a'
COLUMN CELL
0 row(s) in 0.8980 seconds
--查询表中有多少行
hbase(main):035:0> count 'resume'
2 row(s) in 2.8150 seconds
hbase(main):036:0> count 't1'
1 row(s) in 0.9500 seconds
--清空表
hbase(main):037:0> truncate 't1'
Truncating 't1' table (it may take a while):
- Disabling table...
- Dropping table...
- Creating table...
0 row(s) in 21.0060 seconds
注意:Truncate表的处理过程:由于Hadoop的HDFS文件系统不允许直接修改,所以只能先删除表在重新创建已达到清空表的目的