为了完成本关任务,你需要掌握:1.如何使用HBase shell
命令添加数据、2.如何使用命令删除表。
首先启动HBASE
启动HBASEshell
添加数据
我们来给上一关创建的test
表的列data
添加一些数据:
hbase(main):002:0> create 'test','data'
Created table test
Took 0.9443 seconds
=> Hbase::Table - test
hbase(main):003:0> put 'test','row1','data:1','value1'
Took 0.0520 seconds
hbase(main):004:0> put 'test','row2','data:2','value2'
Took 0.0037 seconds
hbase(main):005:0> put 'test','row3','data:3','value3'
Took 0.0049 seconds
从上面例子我们可以看出,使用put
命令可以用来添加数据,使用get
命令可以获取数据。
当然我们肯定还会有一个需求:查看所有的数据。
hbase(main):006:0> get 'test','row'
COLUMN CELL
0 row(s)
Took 0.0641 seconds
输入scan
命令就可以查看所有的数据了。
hbase(main):007:0> scan 'test'
ROW COLUMN+CELL
row1 column=data:1, timestamp=1669877544097, value=value1
row2 column=data:2, timestamp=1669877560974, value=value2
row3 column=data:3, timestamp=1669877574977, value=value3
3 row(s)
Took 0.0253 seconds
删除数据、删除表
我们经常会添加错数据,想要删除然后重新添加应该怎么做呢?
删除整行数据:
deleteall 'test','row1'
指令:deleteall 表名,行名称
即可删除整行数据。
有时候我们还想将创建好的表删除,怎么做呢?
为了移除test
这个表,首先我们要把它设为禁用,然后在删除:
hbase(main):008:0> deleteall 'test','row1'
Took 0.0501 seconds
hbase(main):009:0> disable 'test'
Took 0.4625 seconds
hbase(main):010:0> drop 'test'
Took 0.2602 seconds
hbase(main):011:0> list
TABLE
0 row(s)
Took 0.0280 seconds
=> []
hbase(main):012:0>
可以发现删除表需要两个步骤:
disable 表名
drop 表名
接着我们ctrl + c
或者 输入exit
退出HBase shell
命令行。
编程要求
好了,到你啦,在HBase
中创建表mytable
,列为data
,并在列族data
中添加三行数据:
- 行号分别为:
row1
,row2
,row3
; - 列名分别为:
data:1
,data:2
,data:3
; - 值分别为:
zhangsan
,zhangsanfeng
,zhangwuji
。
hbase(main):012:0> create 'mytable','data'
Created table mytable
Took 0.7300 seconds
=> Hbase::Table - mytable
hbase(main):019:0> put 'mytable','row1','data:1','zhangsan'
Took 0.0044 seconds
hbase(main):020:0> put 'mytable','row2','data:2','zhangsanfeng'
Took 0.0042 seconds
hbase(main):021:0> put 'mytable','row3','data:3','zhangwuji'
Took 0.0033 seconds
hbase(main):022:0> get 'mytable','row'
COLUMN CELL
0 row(s)
Took 0.0070 seconds
hbase(main):023:0> scan 'mytable'
ROW COLUMN+CELL
row1 column=data:1, timestamp=1669878583082, value=zhangsan
row2 column=data:2, timestamp=1669878596532, value=zhangsanfeng
row3 column=data:3, timestamp=1669878608978, value=zhangwuji
3 row(s)
Took 0.0072 seconds
hbase(main):024:0>