上篇:第2章 HBase安装
1、基本操作
(1)进入HBase客户端命令行
[root@hadoop105 hbase-1.3.1]# bin/hbase shell
.........
..........
hbase(main):001:0>
(2) 查看帮助命令
hbase(main):001:0> help
(3)查看当前数据库中有哪些表
hbase(main):002:0> list
TABLE
0 row(s) in 0.4930 seconds
=> []
2、表的操作
(1)创建表
hbase(main):003:0> create 'student','info'
0 row(s) in 4.5810 seconds
=> Hbase::Table - student
再次,查看当前数据库中有哪些表
hbase(main):004:0> list
TABLE
student
1 row(s) in 0.0680 seconds
=> ["student"]
(2)插入数据到表
hbase(main):003:0> put 'student','1001','info:sex','male'
hbase(main):004:0> put 'student','1001','info:age','18'
hbase(main):005:0> put 'student','1002','info:name','Janna'
hbase(main):006:0> put 'student','1002','info:sex','female'
hbase(main):007:0> put 'student','1002','info:age','20'
(4)扫描查看表数据
hbase(main):010:0> scan 'student'
ROW COLUMN+CELL
1001 column=info:age, timestamp=1579445895597, value=18
1001 column=info:sex, timestamp=1579445890093, value=male
1002 column=info:age, timestamp=1579445920929, value=20
1002 column=info:name, timestamp=1579445905783, value=Janna
1002 column=info:sex, timestamp=1579445914211, value=female
2 row(s) in 0.1640 seconds
若想指定范围内的数据(比如:1001的数据)
hbase(main):002:0> get 'student','1001'
COLUMN CELL
info:age timestamp=1579445895597, value=18
info:sex timestamp=1579445890093, value=male
1 row(s) in 0.0800 seconds
若想指定范围内的数据(比如:1001的数据的名字)
hbase(main):001:0> get 'student','1001','info:name'
COLUMN CELL
0 row(s) in 0.5710 seconds
若想指定范围内的数据(比如:1001的数据的名字)
hbase(main):003:0> scan 'student',{STARTROW => '1001', STOPROW => '1001'}
ROW COLUMN+CELL
1001 column=info:age, timestamp=1579445895597, value=18
1001 column=info:sex, timestamp=1579445890093, value=male
1 row(s) in 0.1110 seconds
(4)查看表结构
hbase(main):004:0> descirbe 'student'
(5)更新指定字段的数据
hbase(main):012:0> put 'student','1001','info:name','Nick'
hbase(main):013:0> put 'student','1001','info:age','100'
(6)查看“指定行”或“指定列族:列”的数据
hbase(main):014:0> get 'student','1001'
hbase(main):015:0> get 'student','1001','info:name'
(7)统计表数据行数
hbase(main):001:0> count 'student'
2 row(s) in 0.5180 seconds
=> 2
(8)删除数据
hbase(main):002:0> deleteall 'student','1001'
0 row(s) in 0.1010 seconds
删除某rowkey的某一列数据:
hbase(main):003:0> delete 'student','1002','info:sex'
0 row(s) in 0.0560 seconds
清空表数据
hbase(main):004:0> truncate 'student'
提示:清空表的操作顺序为先disable,然后再truncate。
(10)删除表
首先需要先让该表为disable状态:
hbase(main):004:0> disable 'student'
0 row(s) in 2.6030 seconds
然后才能drop这个表:
hbase(main):005:0> drop 'student'
0 row(s) in 1.3230 seconds
提示:如果直接drop表,会报错:ERROR: Table student is enabled. Disable it first.
变更表信息
将info列族中的数据存放3个版本:
hbase(main):006:0> alter 'student',{NAME=>'info',VERSIONS=>3}
hbase(main):007:0> get 'student','1001',{COLUMN=>'info:name',VERSIONS=>3}