本节内容我们主要讲解下HBase的常用shell操作。首先我我们要启动hbase:
start-hbase.sh
然后连接到hbase服务器:
hbase shell
接下来,我们执行一些常用的shell操作:
1)创建表
#语法格式 create '表名称','列族名称1',''列族名称2',''列族名称N' #比如 create 'table1','cf1','cf2'
执行效果如下:
hbase:002:0> create 'table1','cf1','cf2' Created table table1 Took 6.3768 seconds => Hbase::Table - table1
2)显示所有表
list
执行效果如下:
hbase:003:0> list TABLE table1 1 row(s) Took 0.6862 seconds => ["table1"]
3)增加数据
#语法格式 put '表名称','ROW值','列族名称:列名','列值' #比如 put 'table1','123','cf1:name','panziye' put 'table1','123','cf2:age','18'
4)查询数据数据
#语法格式1 scan '表名称' #比如 scan 'table1' #语法格式2 get '表名称','ROW值' #比如 get 'table1','123' #语法格式3 get '表名称','ROW值','列族名称:列名' #比如 get 'table1','123','cf1:name'
执行效果如下:
hbase:009:0> scan 'table1' ROW COLUMN+CELL 123 column=cf1:name, timestamp=2021-04-26T22:00:36.794, value=panziye 123 column=cf2:age, timestamp=2021-04-26T22:02:33.107, value=18 1 row(s) Took 0.4237 seconds hbase:010:0> get 'table1','123' COLUMN CELL cf1:name timestamp=2021-04-26T22:00:36.794, value=panziye cf2:age timestamp=2021-04-26T22:02:33.107, value=18 1 row(s) Took 0.3048 seconds hbase:011:0> get 'table1','123','cf1:name' COLUMN CELL cf1:name timestamp=2021-04-26T22:00:36.794, value=panziye 1 row(s) Took 0.1682 seconds
5)修改数据
#语法格式 put '表名称','ROW值','列族名称:列名','新列值' #比如 put 'table1','123','cf2:age','28'
执行效果如下:
hbase:023:0> put 'table1','123','cf2:age','28' Took 0.0440 seconds hbase:024:0> get 'table1','123' COLUMN CELL cf1:name timestamp=2021-04-26T22:00:36.794, value=panziye cf2:age timestamp=2021-04-26T22:16:04.812, value=28 1 row(s) Took 0.0533 seconds
6)删除数据
#语法格式 delete '表名称','ROW值','列族名称:列名' #比如 delete 'table1','123','cf2:age'
执行效果如下:
hbase:031:0> delete 'table1','123','cf2:age' Took 0.0169 seconds hbase:032:0> get 'table1','123' COLUMN CELL cf1:name timestamp=2021-04-26T22:00:36.794, value=panziye cf2:age timestamp=2021-04-26T22:02:33.107, value=18 1 row(s) Took 0.0237 seconds
注意:我们发现
cf2:age
只是删除了最新的版本,之前的老版本数据仍然还在,如果继续执行删除操作,就会再删除最后的版本数据7)删除表
删除后表之前需要对表进行disable操作,然后再进行drop操作
#语法格式 disable '表名称' drop '表名称' #比如 disable 'table1' drop 'table1'
执行效果如下:
hbase:033:0> disable 'table1' Took 3.0554 seconds hbase:034:0> drop 'table1' Took 4.6391 seconds hbase:035:0> list TABLE 0 row(s) Took 0.3247 seconds => []