09-Hbase-shell入门操作:
HBase Shell 操作
DDL基本操作
1.进入 HBase 客户端命令行
[root@hadoop102 hbase-1.3.1]# bin/hbase shell
2.查看帮助命令
hbase(main):001:0> help
3.查看当前数据库中有哪些表
hbase(main):002:0> list
创建表
hbase(main):005:0> create ‘student’,‘info’
hbase(main):007:0> create ‘stu’,‘info1’,‘info2’
hbase(main):008:0> list
TABLE
stu
student
=> [“stu”, “student”]
查看表信息
hbase(main):009:0> describe ‘stu’
更改表信息
hbase(main):012:0> alter ‘student’,{NAME=>‘info’,VERSIONS=>3}
删除表
hbase(main):014:0> drop ‘student’
ERROR: Table student 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):015:0> disable ‘student’
0 row(s) in 3.0520 seconds
hbase(main):016:0> drop ‘student’
0 row(s) in 3.0470 seconds
hbase(main):017:0> list
命名空间:
创建命名空间
hbase(main):004:0> create ‘bigdata:stu’,“info”
hbase(main):005:0> list
删除命名空间
hbase(main):006:0> drop_namespace ‘bigdata’
ERROR: org.apache.hadoop.hbase.constraint.ConstraintException: Only empty namespaces can be removed. Namespace bigdata has 1 tables
Drop the named namespace. The namespace must be empty.
hbase(main):007:0> disable ‘bigdata:stu’
hbase(main):008:0> drop ‘bigdata:stu’
hbase(main):009:0> drop_namespace ‘bigdata’
hbase(main):010:0> list
DML基本操作(重)
增加
hbase> put ‘ns1:t1’, ‘r1’, ‘c1’, ‘value’
查
hbase(main):002:0> scan ‘stu’
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1618235653696, value=zhangsan
1 row(s) in 1.4550 seconds
ghbase(main):002:0> get
ghbase(main):002:0> get ‘stu’,‘1001’
hbase(main):003:0> put ‘stu’,‘1001’,‘info1:sex’,‘male’
hbase(main):004:0> put ‘stu’,‘1001’,‘info2:addr’,‘shanghai’
hbase(main):005:0> put ‘stu’,‘1002’,‘info1:name’,‘lisi’
hbase(main):006:0> put ‘stu’,‘1002’,‘info1:phone’,‘18332456666’
hbase(main):007:0> put ‘stu’,‘1003’,‘info2:addr’,‘beijing’
hbase(main):008:0> scan ‘stu’
hbase(main):009:0> get ‘stu’,‘1001’
hbase(main):010:0> get ‘stu’,‘1001’,‘info1:name’
hbase(main):011:0> get ‘stu’,‘1001’,‘info1’
hbase(main):012:0> scan ‘stu’,{STARTROW=>‘1001’,STOPROW=>‘1003’}//左闭右开
hbase(main):013:0> scan ‘stu’,{STARTROW=>‘1001’}=10’,‘info1:name’,‘xiaohong’
hbase(main):014:0> put ‘stu’,‘10010’,‘info1:name’,‘xiaohong’
0 row(s) in 0.4240 seconds
hbase(main):015:0> scan ‘stu’,{STARTROW=>‘1001’}//注意顺序
改
hbase(main):002:0> put ‘stu’,‘1001’,‘info1:name’, ‘zangsan02’
hbase(main):003:0> scan ‘stu’
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1618237274701, value=zangsan02
1001 column=info1:sex, timestamp=1618235992343, value=male
1001 column=info2:addr, timestamp=1618236016442, value=shanghai
10010 column=info1:name, timestamp=1618236476571, value=xiaohong
1002 column=info1:name, timestamp=1618236039169, value=lisi
1002 column=info1:phone, timestamp=1618236064087, value=18332456666
1003 column=info2:addr, timestamp=1618236088958, value=beijing
4 row(s) in 0.0670 seconds
hbase(main):004:0> scan ‘stu’,{RAW=>true,VERSIONS=>10}
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1618237274701, value=zangsan02
1001 column=info1:name, timestamp=1618235653696, value=zhangsan
1001 column=info1:sex, timestamp=1618235992343, value=male
1001 column=info2:addr, timestamp=1618236016442, value=shanghai
10010 column=info1:name, timestamp=1618236476571, value=xiaohong
1002 column=info1:name, timestamp=1618236039169, value=lisi
1002 column=info1:phone, timestamp=1618236064087, value=18332456666
1003 column=info2:addr, timestamp=1618236088958, value=beijing
4 row(s) in 0.2650 seconds
hbase(main):007:0> put ‘stu’,‘1001’,‘info1:name’,‘zangsan03’,1618235653670
hbase(main):008:0> get ‘stu’,‘1001’,‘info1:name’
COLUMN CELL
info1:name timestamp=1618237274701, value=zangsan02
hbase(main):010:0> scan ‘stu’,{RAW=>true,VERSIONS=>10}
ROW COLUMN+CELL
1001 column=info1:name, timestamp=1618237274701, value=zangsan02
1001 column=info1:name, timestamp=1618235653696, value=zhangsan
1001 column=info1:name, timestamp=1618235653670, value=zangsan03
删除(以时间戳最大返回)
hbase(main):001:0> delete ‘stu’,‘1001’,‘info1:sex’
hbase(main):002:0> scan ‘stu’ //扫描表
hbase(main):003:0> delete ‘stu’,‘1001’,‘info1:name’ //删除列
hbase(main):004:0> scan ‘stu’,{RAW=>true,VERSIONS=>10}//扫描全部版本
hbase(main):005:0> deleteall //deleteall的用法
hbase(main):006:0> deleteall ‘stu’,‘1003’
hbase(main):008:0> truncate ‘stu’ //删除表
Truncating ‘stu’ table (it may take a while):
- Disabling table…
- Truncating table…
0 row(s) in 12.5740 seconds
hbase(main):009:0> scan ‘stu’
ROW COLUMN+CELL
0 row(s) in 1.0580 seconds
版本VERSIONS的初步理解(hbase以后最大的保存的版本数量)
hbase(main):013:0> create ‘stu2’,‘info’
hbase(main):014:0> put ‘stu2’,‘1005’,‘info:name’,‘lisi’
hbase(main):015:0> put ‘stu2’,‘1005’,‘info:name’,‘zhangsan’
0 row(s) in 0.0270 seconds
hbase(main):016:0> alert ‘stu2’,{NAME=>‘info’,VERSIONS=>2}
NoMethodError: undefined method `alert’ for #Object:0x46fb0c33
hbase(main):017:0> alter ‘stu2’,{NAME=>‘info’,VERSIONS=>2}
Updating all regions with the new schema…
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 4.3310 seconds
hbase(main):018:0> put ‘stu2’,‘1001’,‘info:name’,‘zhangsan’
0 row(s) in 0.0190 seconds
hbase(main):019:0> put ‘stu2’,‘1001’,‘info:name’,‘lisi’
0 row(s) in 0.0110 seconds
hbase(main):020:0> get ‘stu2’,‘1001’,{COLUMN=>‘info:name’,VERSIONS=>3}
COLUMN CELL
info:name timestamp=1618239366717, value=lisi
info:name timestamp=1618239356363, value=zhangsan
0> get ‘stu2’,‘1001’,{COLUMN=>‘info:name’,VERSIONS=>3}
COLUMN CELL
info:name timestamp=1618239366717, value=lisi
info:name timestamp=1618239356363, value=zhangsan
1 row(s) in 0.0200 seconds