大数据全系列 教程
1869个小节阅读:467.1k
408考研
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
###1.5 HBase Shell操作
打开hbase命令行客户端
x[root@node2 ~]# hbase shell
#查看都有什么命令
hbase(main):001:0> help
COMMAND GROUPS:
Group name: general
Commands: processlist, status, table_help, version, whoami
Group name: ddl # 表、region的ddl
Commands: alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list, list_regions, locate_region, show_filters
Group name: namespace #命名空间的相关操作
Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables
Group name: dml #数据操作
Commands: append, count, delete, deleteall, get, get_counter, get_splits, incr, put, scan, truncate, truncate_preserve
......
查询有哪些namespace
xxxxxxxxxx
hbase(main):001:0> list_namespace
NAMESPACE default hbase 2 row(s)
Took 1.3671 seconds
创建命名空间:
xxxxxxxxxx
hbase(main):002:0> create_namespace "mydb"
Took 0.4451 seconds # 输入命令的一部分然后Tab键,如果情况唯一自动补全,如果有两种或两种以上的情况,列出来
hbase(main):003:0> list_namespace
list_namespace list_namespace_tables
hbase(main):003:0> list_namespace
NAMESPACE default hbase mydb #自己创建的命名空间
xxxxxxxxxx
create_namespace "mydb1", {"author"=>"gtjin", "create_time"=>"2021-12-20"}
#查看属性
describe_namespace "mydb1"
添加或者修改属性:
alter_namespace 'ns1', {METHOD => 'set', 'PROPERTY_NAME' => 'PROPERTY_VALUE'}
删除属性:
alter_namespace 'ns1', {METHOD => 'unset', NAME => ' PROPERTY_NAME '}
xxxxxxxxxx
alter_namespace "mydb1", {METHOD => 'set', 'author' => 'jinxf'}
describe_namespace "mydb1"
alter_namespace "mydb1", {METHOD => 'unset', NAME => 'create_time'}
describe_namespace "mydb1"
删除namespace
xxxxxxxxxx
drop_namespace "mydb1"
注意: 要删除的namespace必须是空的,其下没有表。
创建表
使用create命令创建表,必须指定表名和列族的名称
xxxxxxxxxx
hbase(main):001:0> create 'test', 'cf'
0 row(s) in 0.4170 seconds
=> Hbase::Table - test
列出表的信息
使用list命令列出信息。
xxxxxxxxxx
hbase(main):002:0> list
TABLE
test
1 row(s) in 0.0180 seconds
=> ["test"]
向table插入数据:使用put命令。
xxxxxxxxxx
hbase(main):003:0> put 'test', 'row1', 'cf:a', 'value1'
0 row(s) in 0.0850 seconds
hbase(main):004:0> put 'test', 'row2', 'cf:b', 'value2'
0 row(s) in 0.0110 seconds
hbase(main):005:0> put 'test', 'row3', 'cf:c', 'value3'
0 row(s) in 0.0100 seconds
以上,插入了三个值,每次一个。第一次在row1的位置,cf:a列插入值value1。hbase中的列由列族前缀cf跟一个冒号,再跟一个列标识符后缀比如a。
一次性扫描表中的所有数据
要查询表中的数据,可以使用scan命令。可以在该命令中使用限定条件,下面获取了表中的所有数据:
xxxxxxxxxx
hbase(main):016:0> scan 'test'
ROW COLUMN+CELL
row1 column=cf:a, timestamp=1568514191889, value=value1
row2 column=cf:b, timestamp=1568514198429, value=value2
row3 column=cf:c, timestamp=1568514205341, value=value3 3 row(s) in 0.0350 seconds
查询一行记录:使用get命令一次获取一行记录
xxxxxxxxxx
hbase(main):017:0> get 'test', 'row1'
COLUMN CELL
cf:a timestamp=1568514191889, value=value1
1 row(s) in 0.0140 seconds
禁用一张表
如果要删除表或更改表的设置,需要先使用disable命令禁用该表,之后也可以使用enable命令重新使用该表。
xxxxxxxxxx
hbase(main):020:0> drop 'test'
ERROR: Table test is enabled. Disable it first.
Drop the named table. Table must first be disabled:
hbase(main):021:0> disable 'test'
0 row(s) in 1.3350 seconds
hbase(main):022:0> enable 'test'
0 row(s) in 0.2520 seconds
#再次使用disable命令禁用该表:
hbase(main):023:0> disable 'test'
0 row(s) in 1.2580 seconds
删除表
在表已经禁用的情况下,使用drop命令删除表
xxxxxxxxxx
hbase(main):021:0> drop 'test'
0 row(s) in 0.1550 seconds
退出HBase的shell
退出shell断开到集群的连接,可以使用quit命令。但是hbase服务进程仍然在后台运行。
xxxxxxxxxx
hbase(main):022:0> quit