概述
命名空间操作(namespace)
1、创建命名空间
hbase(main):008:0> create_namespace 'test1'
0 row(s) in 0.3370 seconds
2、查看命名空间,default为默认命名空间
hbase(main):009:0> list_namespace
NAMESPACE
default
hbase
test
test1
3、查看指定命名空间下的表
hbase(main):002:0> list_namespace_tables 'default'
TABLE
scores
student
t1
3 row(s) in 0.0550 seconds
4、删除
drop_namespace 'test1'
表结构的操作
1、新建表
语法1:create 't1', {NAME => 'f1'}, {NAME => 'f2'}, {NAME => 'f3'}
hbase(main):005:0> create 'test1:t1','col1','col2'
0 row(s) in 1.6030 seconds
=> Hbase::Table - test1:t1
如果不指定test1,则默认在default下创建表t1
语法2:create 'ns1:t1', {NAME => 'f1', VERSIONS => 5}
hbase(main):013:0> create 'test1:t3',{NAME=>'f2','VERSIONS'=>5}
0 row(s) in 1.2510 seconds
=> Hbase::Table - test1:t3
设置f2列簇里面可以有5个版本
语法3:create 'ns1:t1', 'f1', SPLITS => ['10', '20', '30', '40']
hbase(main):014:0> create 'test1:t4','f1',SPLITS=>['10','20','30']
0 row(s) in 2.2470 seconds
=> Hbase::Table - test1:t4
设置region
2、查看表详情(describe)
hbase(main):016:0> describe 'test1:t3'
Table test1:t3 is ENABLED
test1:t3
COLUMN FAMILIES DESCRIPTION
{NAME => 'f2', BLOOMFILTER => 'ROW', VERSIONS => '5', IN_MEMORY => 'false', KEEP_DELETED_CELL
S => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VER
SIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
1 row(s) in 0.0290 seconds
3、查看所有表(list)
hbase(main):018:0> list
TABLE
scores
student
t1
test1:t1
test1:t2
test1:t3
test1:t4
test:t1
8 row(s) in 0.0270 seconds
=> ["scores", "student", "t1", "test1:t1", "test1:t2", "test1:t3", "test1:t4", "test:t1"]
4、删除表(disable+drop)
hbase(main):021:0> disable 'test1:t4'
0 row(s) in 2.3010 seconds
hbase(main):022:0> drop 'test1:t4'
0 row(s) in 1.3070 seconds
5、修改表(alter)
新增列簇
hbase(main):024:0> alter 'test1:t1','col3'
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.0510 seconds
写入表里面没有出现的列名就行了
更改列的版本数目
hbase(main):025:0> alter 'test1:t1',{NAME=>'col1',VERSIONS=>4}
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.2220 seconds
更改了col1的版本为4,表示col1中列名一样下可以存4个,以时间戳来区别不同的版本
删除列簇
hbase(main):026:0> alter 'test1:t1','delete'=>'col3'
Updating all regions with the new schema...
1/1 regions updated.
Done.
0 row(s) in 2.2640 seconds
删除col3这个簇
表数据
1、添加表数据(put)
HBase数据存储可以看成是 <rowkey , column family:column qualifier, timestamp> --> cell(value t2,valuet3…) ,我们在插入时要指定插入哪个表,哪一行,以及哪个簇: put 'ns1:t1', 'rowkey', ' column family:column', 'value'
先创建了一个t2表来存储数据: create 't2','f1','f2
hbase(main):034:0> put 't2','sk-100','f1:name','zhansan'
0 row(s) in 0.0470 seconds
为了后面的查询多插几条数据
put 't2','sk-100','f1:name','zhansan'
put 't2','sk-100','f1:name','lisi'
put 't2','sk-100','f1:age','20'
put 't2','sk-200','f1:age','30'
put 't2','sk-200','f1:name','wanwu'
put 't2','sk-200','f2:name','wanwu'
put 't2','sk-200','f2:age','88'
put 't2','sk-100','f2:age','88'
put 't2','sk-100','f2:name','wanwu'
2、根据rowkey查询表数据(get)
hbase(main):046:0> get 't2','sk-100'
COLUMN CELL
f1:age timestamp=1571364552897, value=20
f1:name timestamp=1571364467464, value=lisi
f2:age timestamp=1571364743804, value=88
f2:name timestamp=1571364751402, value=wanwu
4 row(s) in 0.1180 seconds
如果只想查询sk-100中f1簇的数据:
hbase(main):047:0> get 't2','sk-100',{COLUMN=>'f1'}
COLUMN CELL
f1:age timestamp=1571364552897, value=20
f1:name timestamp=1571364467464, value=lisi
2 row(s) in 0.0230 seconds
如果有多个版本的话,要查看多个版本必须加入VERSIONS,不然默认显示最新版本,因为我没有设置版本数,所以输出一样
hbase(main):048:0> get 't2','sk-100',{COLUMN=>'f1',VERSIONS=>4}
COLUMN CELL
f1:age timestamp=1571364552897, value=20
f1:name timestamp=1571364467464, value=lisi
2 row(s) in 0.0240 seconds
3、查询全部记录(scan)
hbase(main):050:0> scan 't2'
ROW COLUMN+CELL
sk-100 column=f1:age, timestamp=1571364552897, value=20
sk-100 column=f1:name, timestamp=1571364467464, value=lisi
sk-100 column=f2:age, timestamp=1571364743804, value=88
sk-100 column=f2:name, timestamp=1571364751402, value=wanwu
sk-200 column=f1:age, timestamp=1571364500307, value=30
sk-200 column=f1:name, timestamp=1571364513677, value=wanwu
sk-200 column=f2:age, timestamp=1571364737688, value=88
sk-200 column=f2:name, timestamp=1571364723007, value=wanwu
2 row(s) in 0.0440 seconds
hbase(main):051:0>
hbase(main):052:0*
hbase(main):053:0* scan 't2',{COLUMN=>['f1','f2'],VERSIONS=>4}
ROW COLUMN+CELL
sk-100 column=f1:age, timestamp=1571364552897, value=20
sk-100 column=f1:name, timestamp=1571364467464, value=lisi
sk-100 column=f2:age, timestamp=1571364743804, value=88
sk-100 column=f2:name, timestamp=1571364751402, value=wanwu
sk-200 column=f1:age, timestamp=1571364500307, value=30
sk-200 column=f1:name, timestamp=1571364513677, value=wanwu
sk-200 column=f2:age, timestamp=1571364737688, value=88
sk-200 column=f2:name, timestamp=1571364723007, value=wanwu
2 row(s) in 0.0400 seconds
4、删除表数据
删除列
hbase(main):062:0> delete 't2','zk-100','f1'
0 row(s) in 0.3570 seconds
删除行
hbase(main):064:0> deleteall 't2','zk-100'
0 row(s) in 0.0110 seconds
清空表数据
hbase(main):054:0> truncate_preserve 't2'
Truncating 't2' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 4.0440 seconds
只删除表里面的数据
hbase(main):055:0> truncate 't2'
Truncating 't2' table (it may take a while):
- Disabling table...
- Truncating table...
0 row(s) in 4.3000 seconds
输出数据并且删除表的结构,例如有设计SPLIT的都会被删除
练习题:https://blog.csdn.net/weixin_43832243/article/details/102686300
最后
以上就是能干中心为你收集整理的HBase shell常用命令及练习的全部内容,希望文章能够帮你解决HBase shell常用命令及练习所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复