Hapoop 搭建 (十)Hbase shell 命令操作
1、连接Hbase
[hadoop@centos01 ~]$ sh /opt/modules/hbase-2.1.9/bin/hbase shell SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/opt/modules/hadoop-2.8.2/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/opt/modules/hbase-2.1.9/lib/client-facing-thirdparty/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] HBase Shell Use "help" to get list of supported commands. Use "exit" to quit this interactive shell. For Reference, please visit: http://hbase.apache.org/2.0/book.html#shell Version 2.1.9, rec4145ba5dc9cd30d4eb700bca2de2d08104207c, Tue Feb 11 03:49:57 UTC 2020 Took 0.0183 seconds hbase(main):001:0>
2、创建表
创建表名t1,列族名f1的表
create 't1','f1'
hbase(main):001:0> create 't1','f1' Created table t1 Took 6.8652 seconds => Hbase::Table - t1
3、添加数据
向表t1添加一条记录,rowkey为row1,列name的值为test
hbase(main):003:0> put 't1','row1','f1:name','test' Took 0.0546 seconds
向表t1添加一条记录,rowkey为row2,列age的值为18
hbase(main):004:0> put 't1','row1','f1:age','18' Took 0.0680 seconds
4、全表扫描
使用scan命令通过对表的扫描来获取表中全部的数据
hbase(main):005:0> scan 't1' ROW COLUMN+CELL row1 column=f1:age, timestamp=1584245218160, value=18 row1 column=f1:name, timestamp=1584245156133, value=test 1 row(s) Took 0.5072 seconds
hbase(main):007:0> put 't1','row2','f1:age','28' Took 0.0241 seconds hbase(main):008:0> scan 't1' ROW COLUMN+CELL row1 column=f1:age, timestamp=1584245218160, value=18 row1 column=f1:name, timestamp=1584245156133, value=test row2 column=f1:age, timestamp=1584245459696, value=28 2 row(s) Took 0.0355 seconds
5、查询一行数据
使用get命令查询表中一整行数据,查询表t1中rowkey为row1的一整行记录
hbase(main):006:0> get 't1','row1' COLUMN CELL f1:age timestamp=1584245218160, value=18 f1:name timestamp=1584245156133, value=test 1 row(s) Took 0.2588 seconds
6、修改表
修改表使用put,修改表t1中行键row1对应的name
hbase(main):009:0> put 't1','row1','f1:name','updatetest' Took 0.0447 seconds hbase(main):010:0> get 't1','row1' COLUMN CELL f1:age timestamp=1584245218160, value=18 f1:name timestamp=1584245593210, value=updatetest 1 row(s) Took 0.0276 seconds
7、删除特定单元格
hbase(main):018:0> put 't1','row1','f1:name','test' Took 0.0341 seconds hbase(main):019:0> get 't1','row1' COLUMN CELL f1:age timestamp=1584245218160, value=18 f1:name timestamp=1584245839263, value=test 1 row(s) Took 0.0478 seconds hbase(main):020:0> scan 't1' ROW COLUMN+CELL row1 column=f1:age, timestamp=1584245218160, value=18 row1 column=f1:name, timestamp=1584245839263, value=test row2 column=f1:age, timestamp=1584245459696, value=28 2 row(s) Took 0.0328 seconds hbase(main):021:0> delete 't1','row1','f1:name' Took 0.0241 seconds hbase(main):022:0> scan 't1' ROW COLUMN+CELL row1 column=f1:age, timestamp=1584245218160, value=18 row1 column=f1:name, timestamp=1584245830651, value=updatetest row2 column=f1:age, timestamp=1584245459696, value=28 2 row(s) Took 0.0290 seconds hbase(main):023:0> delete 't1','row1','f1:name' Took 0.0171 seconds hbase(main):024:0> scan 't1' ROW COLUMN+CELL row1 column=f1:age, timestamp=1584245218160, value=18 row2 column=f1:age, timestamp=1584245459696, value=28 2 row(s) Took 0.0629 seconds
8、删除一整行数据
hbase(main):025:0> put 't1','row1','f1:name','test' Took 0.0313 seconds hbase(main):026:0> scan 't1' ROW COLUMN+CELL row1 column=f1:age, timestamp=1584245218160, value=18 row1 column=f1:name, timestamp=1584245977696, value=test row2 column=f1:age, timestamp=1584245459696, value=28 2 row(s) Took 0.0286 seconds hbase(main):027:0> deleteall 't1','row1' Took 0.0232 seconds hbase(main):028:0> scan 't1' ROW COLUMN+CELL row2 column=f1:age, timestamp=1584245459696, value=28 1 row(s) Took 0.0304 seconds
9、删除整张表
disable 禁用表,使表无效 drop 删除表 要完全删除表需要先disable,在drop hbase(main):029:0> disable 't1' Took 1.6444 seconds hbase(main):030:0> scan 't1' ROW COLUMN+CELL org.apache.hadoop.hbase.TableNotEnabledException: t1 is disabled. at org.apache.hadoop.hbase.client.ConnectionImplementation.relocateRegion(ConnectionImplementation.java:736) at org.apache.hadoop.hbase.client.RpcRetryingCallerWithReadReplicas.getRegionLocations(RpcRetryingCallerWithReadReplicas.java:328) at org.apache.hadoop.hbase.client.ScannerCallable.prepare(ScannerCallable.java:139) at org.apache.hadoop.hbase.client.ScannerCallableWithReplicas$RetryingRPC.prepare(ScannerCallableWithReplicas.java:408) at org.apache.hadoop.hbase.client.RpcRetryingCallerImpl.callWithRetries(RpcRetryingCallerImpl.java:105) at org.apache.hadoop.hbase.client.ResultBoundedCompletionService$QueueingFuture.run(ResultBoundedCompletionService.java:80) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) ERROR: Table t1 is disabled! For usage try 'help "scan"' Took 0.1647 seconds hbase(main):031:0> drop 't1' Took 1.5094 seconds hbase(main):032:0> scan 't1' ROW COLUMN+CELL ERROR: Unknown table t1! For usage try 'help "scan"' Took 0.0262 seconds
10、列出所有表
hbase(main):034:0> list TABLE t2 1 row(s) Took 0.1668 seconds => ["t2"]
11、查询表中的记录数
通过count 表名获取记录数
hbase(main):055:0> count 't2' 3 row(s) Took 0.0519 seconds => 3
12、查询表是否存在
hbase(main):056:0> exists 't2' Table t2 does exist Took 0.1182 seconds => true hbase(main):057:0> exists 't1' Table t1 does not exist Took 0.0160 seconds => false
13、批量执行命令
[hadoop@centos01 ~]$ cat hbase_commad.txt create 't3','f1' list put 't3','row1','f1:name','test1' put 't3','row1','f1:age','20' put 't3','row2','f1:name','test2','f1:age','22' scan 't3' exit [hadoop@centos01 ~]$ sh /opt/modules/hbase-2.1.9/bin/hbase shell ./hbase_commad.txt SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/opt/modules/hadoop-2.8.2/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/opt/modules/hbase-2.1.9/lib/client-facing-thirdparty/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory] Created table t3 Took 5.7051 seconds TABLE t2 t3 2 row(s) Took 0.1234 seconds Took 1.3711 seconds Took 0.0152 seconds ERROR: undefined method `any?' for "22":String For usage try 'help "put"' Took 0.9263 seconds ROW COLUMN+CELL row1 column=f1:age, timestamp=1584248928631, value=20 row1 column=f1:name, timestamp=1584248928570, value=test1 1 row(s) Took 0.2708 seconds [hadoop@centos01 ~]$