HBase Shell操作
1. 启动HBase Shell
sh
[jack@hadoop102 hbase-2.6.1]$ hbase shell
## 查看帮助命令
hbase(main):001:0> help
## 查看当前数据库中有哪些表
hbase(main):002:0> list
TABLE
0 row(s)
Took 0.5050 seconds
=> []
2. 命名空间
2.1 查看命令空间
sh
hbase:003:0> list_namespace
NAMESPACE
default
hbase
2 row(s)
Took 0.0780 seconds
2.2 创建命名空间
sh
hbase:004:0> create_namespace 'ns1'
Took 0.3820 seconds
hbase:005:0> list_namespace
NAMESPACE
default hbase
ns1
3 row(s)
Took 0.0290 seconds
3. 创建表
3.1 建表方式1
sh
hbase:006:0> create 'student','info','msg'
2024-11-27 11:56:56,275 INFO [main] client.HBaseAdmin (HBaseAdmin.java:postOperationResult(3751)) - Operation: CREATE, Table Name: default:student, procId: 10 completed
Created table student
Took 2.2750 seconds
=> Hbase::Table - student
除了使用list命令查看表,还可以在页面上查看:
3.2 建表方式2
sh
hbase:010:0> create 'ns1:person',{NAME => 'info', VERSIONS => 5}, {NAME => 'age', VERSIONS => 5}
2024-11-27 12:38:49,190 INFO [main] client.HBaseAdmin (HBaseAdmin.java:postOperationResult(3751)) - Operation: CREATE, Table Name: ns1:person, procId: 13 completed
Created table ns1:person
Took 1.1760 seconds
=> Hbase::Table - ns1:person
4. 查看表
sh
hbase:013:0> list
TABLE
student
ns1:person
2 row(s)
Took 0.0120 seconds
=> ["student", "ns1:person"]
查看具体表的字段信息
sh
hbase:015:0> describe 'student'
2024-11-27 12:41:46,303 INFO [ReadOnlyZKClient-hadoop102:2181,hadoop103:2181,hadoop104:2181@0x5a899811] zookeeper.ZooKeeper (ZooKeeper.java:<init>(637)) - Initiating client connection, connectString=hadoop102:2181,hadoop103:2181,hadoop104:2181 sessionTimeout=90000 watcher=org.apache.hadoop.hbase.zookeeper.ReadOnlyZKClient$$Lambda$935/95952335@38147282
2024-11-27 12:41:46,304 INFO [ReadOnlyZKClient-hadoop102:2181,hadoop103:2181,hadoop104:2181@0x5a899811] zookeeper.ClientCnxnSocket (ClientCnxnSocket.java:initProperties(239)) - jute.maxbuffer value is 1048575 Bytes
2024-11-27 12:41:46,305 INFO [ReadOnlyZKClient-hadoop102:2181,hadoop103:2181,hadoop104:2181@0x5a899811] zookeeper.ClientCnxn (ClientCnxn.java:initRequestTimeout(1747)) - zookeeper.request.timeout value is 0. feature enabled=false
2024-11-27 12:41:46,309 INFO [ReadOnlyZKClient-hadoop102:2181,hadoop103:2181,hadoop104:2181@0x5a899811-SendThread(hadoop102:2181)] zookeeper.ClientCnxn (ClientCnxn.java:logStartConnect(1177)) - Opening socket connection to server hadoop102/192.168.101.102:2181.
2024-11-27 12:41:46,310 INFO [ReadOnlyZKClient-hadoop102:2181,hadoop103:2181,hadoop104:2181@0x5a899811-SendThread(hadoop102:2181)] zookeeper.ClientCnxn (ClientCnxn.java:logStartConnect(1179)) - SASL config status: Will not attempt to authenticate using SASL (unknown error)
2024-11-27 12:41:46,312 INFO [ReadOnlyZKClient-hadoop102:2181,hadoop103:2181,hadoop104:2181@0x5a899811-SendThread(hadoop102:2181)] zookeeper.ClientCnxn (ClientCnxn.java:primeConnection(1013)) - Socket connection established, initiating session, client: /192.168.101.102:60456, server: hadoop102/192.168.101.102:2181
2024-11-27 12:41:46,337 INFO [ReadOnlyZKClient-hadoop102:2181,hadoop103:2181,hadoop104:2181@0x5a899811-SendThread(hadoop102:2181)] zookeeper.ClientCnxn (ClientCnxn.java:onConnected(1453)) - Session establishment complete on server hadoop102/192.168.101.102:2181, session id = 0x10009f5cfab000a, negotiated timeout = 40000
Table student is ENABLED
student, {TABLE_ATTRIBUTES => {METADATA => {'hbase.store.file-tracker.impl' => 'DEFAULT'}}}
COLUMN FAMILIES DESCRIPTION
{NAME => 'info', INDEX_BLOCK_ENCODING => 'NONE', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS
=> '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536 B (64KB)'}
{NAME => 'msg', INDEX_BLOCK_ENCODING => 'NONE', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS =
> '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536 B (64KB)'}
2 row(s)
Quota is disabled
Took 0.3270 seconds
5. 修改表
sh
hbase:019:0> alter 'student',NAME=>'info',VERSIONS=>3
Updating all regions with the new schema...
1/1 regions updated.
Done.
Took 2.2730 seconds
## 如果不存在的列族,会被插入
hbase:020:0> alter 'student',{NAME=>'info1',VERSIONS=>3}
Updating all regions with the new schema...
1/1 regions updated.
Done.
Took 2.2910 seconds
hbase:021:0> desc 'student'
Table student is ENABLED
student, {TABLE_ATTRIBUTES => {METADATA => {'hbase.store.file-tracker.impl' => 'DEFAULT'}}}
COLUMN FAMILIES DESCRIPTION
{NAME => 'info', INDEX_BLOCK_ENCODING => 'NONE', VERSIONS => '3', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS
=> '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536 B (64KB)'}
{NAME => 'info1', INDEX_BLOCK_ENCODING => 'NONE', VERSIONS => '3', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS
=> '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536 B (64KB)'}
{NAME => 'msg', INDEX_BLOCK_ENCODING => 'NONE', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS =
> '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536 B (64KB)'}
3 row(s)
Quota is disabled
Took 0.0370 seconds
6. 删除列族
sh
hbase:024:0> alter 'student', 'delete'=>'info1'
Updating all regions with the new schema...
1/1 regions updated.
Done.
Took 2.1450 seconds
hbase:025:0> desc 'student'
Table student is ENABLED
student, {TABLE_ATTRIBUTES => {METADATA => {'hbase.store.file-tracker.impl' => 'DEFAULT'}}}
COLUMN FAMILIES DESCRIPTION
{NAME => 'info', INDEX_BLOCK_ENCODING => 'NONE', VERSIONS => '3', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS
=> '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536 B (64KB)'}
{NAME => 'msg', INDEX_BLOCK_ENCODING => 'NONE', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS =
> '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE => '65536 B (64KB)'}
2 row(s)
Quota is disabled
Took 0.0270 second
7. 删除表
需要两步操作:1. 需要先让该表为disable状态;2. 执行drop
sh
hbase:026:0> disable 'student'
2024-11-27 12:58:16,228 INFO [main] client.HBaseAdmin (HBaseAdmin.java:rpcCall(967)) - Started disable of student
2024-11-27 12:58:16,880 INFO [main] client.HBaseAdmin (HBaseAdmin.java:postOperationResult(3751)) - Operation: DISABLE, Table Name: default:student, procId: 31 completed
Took 0.6780 seconds
hbase:027:0> drop 'student'
2024-11-27 12:58:25,439 INFO [main] client.HBaseAdmin (HBaseAdmin.java:postOperationResult(3751)) - Operation: DELETE, Table Name: default:student, procId: 35 completed
Took 0.3530 seconds
8. 写入数据
插入数据格式为put '表名', 'rowKey', '指定列族:列', 'value'
sh
hbase:035:0> put 'ns1:person', '2000', 'info:name', 'jack'
Took 0.0290 seconds
hbase:036:0> put 'ns1:person', '2000', 'info:age', 33
Took 0.0180 seconds
hbase:037:0> put 'ns1:person', '2000', 'info:age', 34
Took 0.0550 seconds
多次写入相同rowKey,如果列族相同,会被覆盖,可用于更新。
9. 查询数据
9.1 使用get查询单行数据
默认get读取最新版本的cell数据
sh
hbase:069:0> put 'ns1:person', '2000', 'info:name', 'test'
Took 0.0750 seconds hbase:070:0> put 'ns1:person', '2000', 'info:name', 'test123'
Took 0.0590 seconds
hbase:049:0>hbase:073:0> get 'ns1:person','2000'
COLUMN CELL
info:age timestamp=2024-11-27T15:11:10.124, value=34
info:name timestamp=2024-11-27T17:56:15.341, value=test123
1 row(s)
Took 0.0270 seconds
查询指定版本的cell数据,返回从版本1到当前版本的所有数据:
sh
hbase:075:0> get 'ns1:person','2000',{COLUMN=>'info:name',VERSIONS=>3}
COLUMN CELL
info:name timestamp=2024-11-27T17:56:15.341, value=test123
info:name timestamp=2024-11-27T17:56:11.884, value=test
info:name timestamp=2024-11-27T15:09:00.389, value=jack
1 row(s)
Took 0.0290 seconds
也可以指定列名得到数据
sh
hbase:074:0> get 'ns1:person', '2000', {COLUMN=>['info:age', 'info:name']}
COLUMN CELL
info:age timestamp=2024-11-27T15:11:10.124, value=34
info:name timestamp=2024-11-27T17:56:15.341, value=test123
1 row(s)
Took 0.0190 second
9.2 使用Scan查询多行数据
展示有多行如果rowKey相同其实是一行数据,默认读取最新版本的字段数据,使用scan默认会查出所有数据:
sh
hbase:044:0> scan 'ns1:person'
ROW COLUMN+CELL
1003 column=info:wangwu, timestamp=2024-11-27T14:46:31.065, value=10
2000 column=info:age, timestamp=2024-11-27T15:11:10.124, value=34
2000 column=info:name, timestamp=2024-11-27T15:09:00.389, value=jack
指定获取数据,指定起始和结束rowKey, 范围是左闭右开:
sh
hbase:048:0> scan 'ns1:person', {STARTROW=>'2000',STOPROW=>'2000'}
ROW COLUMN+CELL
2000 column=info:age, timestamp=2024-11-27T15:11:10.124, value=34
2000 column=info:name, timestamp=2024-11-27T15:09:00.389, value=jack
1 row(s)
Took 0.0110 seconds
6. 删除数据
删除某rowkey的全部版本数据:
sh
hbase:052:0> deleteall 'ns1:person','1003'
Took 0.0410 seconds
删除某rowkey的某一列数据:
sh
hbase:054:0> delete 'student','1002','info:sex'
删除某rowkey的版本数据:
sh
hbase:076:0> alter 'ns1:person',{NAME=>'info',VERSIONS=>1}
Updating all regions with the new schema...
1/1 regions updated.
Done.
Took 2.6600 seconds
hbase:077:0> get 'ns1:person','2000',{COLUMN=>'info:name',VERSIONS=>3}
COLUMN CELL
info:name timestamp=2024-11-27T17:56:15.341, value=test123
1 row(s)
Took 0.0370 seconds