进入hbase命令行
./hbase shell
显示hbase中的表
list
创建user表,里面包含info,date两个列族
create 'user','info','data'
create 'user',{NAME => 'info', VERSIONS => 5},{NAME => 'data' , VERSIONS => 5 }
插入数据 put
hbase> put 'ns1:t1', 'r1', 'c1', 'value'
hbase> put 't1', 'r1', 'c1', 'value'
hbase> put 't1', 'r1', 'c1', 'value', ts1
hbase> put 't1', 'r1', 'c1', 'value', {ATTRIBUTES=>{'mykey'=>'myvalue'}}
hbase> put 't1', 'r1', 'c1', 'value', ts1, {ATTRIBUTES=>{'mykey'=>'myvalue'}}
hbase> put 't1', 'r1', 'c1', 'value', ts1, {VISIBILITY=>'PRIVATE|SECRET'}
put 'user' , 'rk0001' , 'info:name' , 'zjs'
put 'user' , 'rk0001' , 'info:age' , '18'
put 'user' , 'rk0001' , 'info:sex' , '男'
put 'user' , 'rk0001' , 'data:name' , 'lt'
put 'user' , 'rk0001' , 'data:age' , '19'
put 'user' , 'rk0001' , 'data:sex' , '女'
put 'user' , 'rk0002' , 'info:name' , 'zjszjs'
put 'user' , 'rk0002' , 'info:age' , '1818'
put 'user' , 'rk0002' , 'info:sex' , '男男'
put 'user' , 'rk0002' , 'data:name' , 'ltlt'
put 'user' , 'rk0002' , 'data:age' , '1919'
put 'user' , 'rk0002' , 'data:sex' , '女女'
查询数据 get
hbase> t.get 'r1'
hbase> t.get 'r1', {TIMERANGE => [ts1, ts2]}
hbase> t.get 'r1', {COLUMN => 'c1'}
hbase> t.get 'r1', {COLUMN => ['c1', 'c2', 'c3']}
hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1}
hbase> t.get 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}
hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4}
hbase> t.get 'r1', {FILTER => "ValueFilter(=, 'binary:abc')"}
hbase> t.get 'r1', 'c1'
hbase> t.get 'r1', 'c1', 'c2'
hbase> t.get 'r1', ['c1', 'c2']
hbase> t.get 'r1', {CONSISTENCY => 'TIMELINE'}
hbase> t.get 'r1', {CONSISTENCY => 'TIMELINE', REGION_REPLICA_ID => 1}
get 'user' , 'rk0001'
get 'user' , 'rk0002' , {COLUMN => 'info:name', VERSIONS => 10}
扫描数据 scan
hbase> scan 'hbase:meta'
hbase> scan 'hbase:meta', {COLUMNS => 'info:regioninfo'}
hbase> scan 'ns1:t1', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'}
hbase> scan 't1', {COLUMNS => ['c1', 'c2'], LIMIT => 10, STARTROW => 'xyz'}
hbase> scan 't1', {COLUMNS => 'c1', TIMERANGE => [1303668804, 1303668904]}
hbase> scan 't1', {REVERSED => true}
hbase> scan 't1', {ROWPREFIXFILTER => 'row2', FILTER => "
(QualifierFilter (>=, 'binary:xyz')) AND (TimestampsFilter ( 123, 456))"}
hbase> scan 't1', {FILTER =>
org.apache.hadoop.hbase.filter.ColumnPaginationFilter.new(1, 0)}
hbase> scan 't1', {CONSISTENCY => 'TIMELINE'}
hbase> scan 't1', { COLUMNS => ['c1', 'c2'], ATTRIBUTES => {'mykey' => 'myvalue'}}
hbase> scan 't1', { COLUMNS => ['c1', 'c2'], AUTHORIZATIONS => ['PRIVATE','SECRET']}
hbase> scan 't1', {COLUMNS => ['c1', 'c2'], CACHE_BLOCKS => false}
hbase> scan 't1', {RAW => true, VERSIONS => 10}
hbase> t = get_table 't'
hbase> t.scan
scan 'user'
创建user表,里面包含info,date两个列族
create 'user','info','data'(不指定VERSIONS时,默认VERSIONS=1)
create 'user',{NAME => 'info', VERSIONS => 5},{NAME => 'data' , VERSIONS => 5 }
向user表中插入信息,row key 值为rk0001 列族info中添加name列标识符,值为zhangsan
put 'user','rk0001','info:name','zhansan'
向user表中插入信息,row key 值为rk0001 列族info中添加gender列标识符,female
put 'user','rk0001','info:gender','female'
向user表中插入信息,row key 值为rk0001 列族info中添加age列标识符,值为18
put 'user','rk0001','info:age','18'
向user表中插入信息,row key 值为rk0001 列族info中添加pic列标识符,值为picture
put 'user','rk0001','info:pic','picture'
获取user表中,row key为rk0001的所有信息
get 'user','rk0001'
获取user表中,row key为rk0001,info列族中的所有信息
get 'user', 'rk0001', 'info'
获取user表中,row key为rk0001,info列族中的name和age列标识符中的信息
get 'user', 'rk0001', 'info:name', 'info:age'
获取user表中,row key为rk0001,info,data 列族的信息
get 'user', 'rk0001', 'info', 'data'
get 'user', 'rk0001', {COLUMN => ['info' ,'data']}
get 'user', 'rk0001', {COLUMN => ['info:name' ,'data:age']}
获取user表中,row key为rk0001,列族为info,版本号最新5个的信息
get 'user', 'rk0001', {COLUMN =>'info'},{VERSIONS =>5}
get 'user', 'rk0001', {COLUMN =>'info:name'},{VERSIONS =>5}
获取user表中row key 为rk0001,cell的值为zjs的信息
get 'user', 'rk0001', {FILTER => "ValueFilter(=, 'binary:zjs')"}
后去user表中row key为rk0001,列标示符中含有’a‘的信息
get 'user', 'rk0001' ,{FILTER => "(QualifierFilter(=,'substring:z'))"}
查询user表中所有的信息
scan 'user'
查询user表中列族为info的信息
scan 'user', {COLUMUN => 'info'}
scan 'user', {COLUMN => 'info',RAW =>true, VERSIONS =>5}
查询user表中,列族为info,data的信息
scan 'user', {COLUMN => ['info', 'data']}
scan 'user', {COLUMN => ['info:name', 'data:age']}
查询user表中列族为info,列标识符为name的信息
scan 'user', {COLUMN =>'info:name'}
查询user表中列族为info,列标识符为name的信息,版本最多5个
scan 'user', {COLUMN =>'info:name'},{VERSIONS => 5}
查询user表中,列族为info个data且列标识符含有a字符的信息
scan 'user' ,{COLUMN =>['info' , 'data'], FILTER => "(QualifierFilter(=,'substring:a'))"}
scan 'user' ,{COLUMN =>['info' , 'data'], FILTER => "(QualifierFilter(=,'substring:a'))"}
ROW COLUMN+CELL
rk0001 column=data:age, timestamp=1529670010281, value=19
rk0001 column=data:name, timestamp=1529670010202, value=lt
rk0001 column=info:age, timestamp=1529670009879, value=18
rk0001 column=info:name, timestamp=1529669995801, value=zjs
rk0002 column=data:age, timestamp=1529670128578, value=1919
rk0002 column=data:name, timestamp=1529670128531, value=ltlt
rk0002 column=info:age, timestamp=1529670128445, value=1818
rk0002 column=info:name, timestamp=1529670128315, value=zjszjs
2 row(s) in 0.0410 seconds
查询user表中,列族为info,rk范围是[rk0001,rk0003]的数据
scan 'user', {CLOUMN =>'info' , STARTROW => 'rk0001' , ENDROW => 'rk0003'}
scan 'user', {COLUMN =>'info' , STARTROW => 'rk0001' , ENDROW => 'rk0003'}
ROW COLUMN+CELL
rk0001 column=info:age, timestamp=1529670009879, value=18
rk0001 column=info:name, timestamp=1529669995801, value=zjs
rk0001 column=info:sex, timestamp=1529670010078, value=\xE7\x94\xB7
rk0002 column=info:age, timestamp=1529670128445, value=1818
rk0002 column=info:name, timestamp=1529670128315, value=zjszjs
rk0002 column=info:sex, timestamp=1529670128489, value=\xE7\x94\xB7\xE7\x94\xB7
2 row(s) in 0.0320 seconds
查询user表中,row key 以rk字符开头的
scan 'user' ,{FILTER => "PrefixFilter('rk')"}
scan 'user' ,{FILTER => "PrefixFilter('rk')"}
ROW COLUMN+CELL
rk0001 column=data:age, timestamp=1529670010281, value=19
rk0001 column=data:name, timestamp=1529670010202, value=lt
rk0001 column=data:sex, timestamp=1529670012330, value=\xE5\xA5\xB3
rk0001 column=info:age, timestamp=1529670009879, value=18
rk0001 column=info:name, timestamp=1529669995801, value=zjs
rk0001 column=info:sex, timestamp=1529670010078, value=\xE7\x94\xB7
rk0002 column=data:age, timestamp=1529670128578, value=1919
rk0002 column=data:name, timestamp=1529670128531, value=ltlt
rk0002 column=data:sex, timestamp=1529670129123, value=\xE5\xA5\xB3\xE5\xA5\xB3
rk0002 column=info:age, timestamp=1529670128445, value=1818
rk0002 column=info:name, timestamp=1529670128315, value=zjszjs
rk0002 column=info:sex, timestamp=1529670128489, value=\xE7\x94\xB7\xE7\x94\xB7
2 row(s) in 0.0640 seconds
查询user中,指定范围的数据
scan 'user', {TIMERANGE => [1529670010202,1529670012330]}
ROW COLUMN+CELL
rk0001 column=data:age, timestamp=1529670010281, value=19
rk0001 column=data:name, timestamp=1529670010202, value=lt
1 row(s) in 0.0190 seconds
删除数据
删除user表中row key为rk0001,info列族中列标识符为name的数据
delete 'user','rk0001','info:name'
0 row(s) in 0.4830 seconds
hbase(main):002:0> scan 'user'
ROW COLUMN+CELL
rk0001 column=data:age, timestamp=1529670010281, value=19
rk0001 column=data:name, timestamp=1529670010202, value=lt
rk0001 column=data:sex, timestamp=1529670012330, value=\xE5\xA5\xB3
rk0001 column=info:age, timestamp=1529670009879, value=18
rk0001 column=info:sex, timestamp=1529670010078, value=\xE7\x94\xB7
rk0002 column=data:age, timestamp=1529670128578, value=1919
rk0002 column=data:name, timestamp=1529670128531, value=ltlt
rk0002 column=data:sex, timestamp=1529670129123, value=\xE5\xA5\xB3\xE5\xA5\xB3
rk0002 column=info:age, timestamp=1529670128445, value=1818
rk0002 column=info:name, timestamp=1529670128315, value=zjszjs
rk0002 column=info:sex, timestamp=1529670128489, value=\xE7\x94\xB7\xE7\x94\xB7
2 row(s) in 0.0870 seconds
删除user表,row key为rk0002,列标识符为info:name,timestamp为1529670128315的数据
delete 'user', 'rk0002', 'info:name', 1529670128315
0 row(s) in 0.0100 seconds
hbase(main):004:0> scan 'user'
ROW COLUMN+CELL
rk0001 column=data:age, timestamp=1529670010281, value=19
rk0001 column=data:name, timestamp=1529670010202, value=lt
rk0001 column=data:sex, timestamp=1529670012330, value=\xE5\xA5\xB3
rk0001 column=info:age, timestamp=1529670009879, value=18
rk0001 column=info:sex, timestamp=1529670010078, value=\xE7\x94\xB7
rk0002 column=data:age, timestamp=1529670128578, value=1919
rk0002 column=data:name, timestamp=1529670128531, value=ltlt
rk0002 column=data:sex, timestamp=1529670129123, value=\xE5\xA5\xB3\xE5\xA5\xB3
rk0002 column=info:age, timestamp=1529670128445, value=1818
rk0002 column=info:sex, timestamp=1529670128489, value=\xE7\x94\xB7\xE7\x94\xB7
2 row(s) in 0.0480 seconds
清理user表中的数据
truncate 'user'
修改表结构
首先停用user表(新版本不需要)
disable 'user'
添加两个列族
alter 'user', NAME =>'f1'
alter 'user', NAME =>'f2'
启用表
enable 'user'
删除一个列族
disable 'user'
alter 'user' ,NAME => 'f1', METHOD => 'delete'
或者
alter 'user', 'delete' => 'f1'
添加列族f1,同时删除列族f2
alter 'user', {NAME => 'f1'},{NAME => 'f2', METHOD => 'delete'}
将user表中的info列族的版本号改成10
alter 'user', NAME => info , VERSIONS =>5
disable 'user'
0 row(s) in 2.9250 seconds
hbase(main):002:0> desc 'user'
Table user is DISABLED
user
COLUMN FAMILIES DESCRIPTION
{NAME => 'data', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '5', COMPRESSION => 'NONE
', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 't
rue'}
{NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '5', COMPRESSION => 'NONE
', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 't
rue'}
2 row(s) in 0.0620 seconds
hbase(main):003:0> alter 'user', NAME=> 'info', VERSIONS => 15
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.0130 seconds
hbase(main):004:0> enable
enable enable_all enable_peer enable_table_replication
hbase(main):004:0> enable 'user'
0 row(s) in 1.4350 seconds
hbase(main):005:0> desc 'user'
Table user is ENABLED
user
COLUMN FAMILIES DESCRIPTION
{NAME => 'data', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '5', COMPRESSION => 'NONE
', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => 't
rue'}
{NAME => 'info', DATA_BLOCK_ENCODING => 'NONE', BLOOMFILTER => 'ROW', REPLICATION_SCOPE => '0', VERSIONS => '15', COMPRESSION => 'NON
E', MIN_VERSIONS => '0', TTL => 'FOREVER', KEEP_DELETED_CELLS => 'FALSE', BLOCKSIZE => '65536', IN_MEMORY => 'false', BLOCKCACHE => '
true'}
2 row(s) in 0.0210 seconds
启用表
enable 'user'
删除表
disable 'user'
drop 'user'