HBase Shell操作 详解

hbase 的命令行 :

# bin/hbase shell
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/module/hbase-1.6.0/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/module/hadoop-2.10.0/share/hadoop/common/lib/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.
Version 1.6.0, r5ec5a5b115ee36fb28903667c008218abd21b3f5, Fri Feb 14 12:00:03 PST 2020

# 1. 查看有哪些表
hbase(main):001:0> list
TABLE
student:stu_info
1 row(s) in 0.2780 seconds

=> ["student:stu_info"]

hbase的简单使用 : help ‘’ 查看命令的用法
注 : delete按钮 点击往后删,control + delete 点击往前删

namespace :

# 1. 创建
hbase(main):001:0> create_namespace 'student'
0 row(s) in 1.2370 seconds

# 2. 查看所有namespace
hbase(main):002:0> list_namespace
NAMESPACE
default # 系统默认有两个数据库,一个 default 另外一个是 hbase
hbase
student
3 row(s) in 0.0460 seconds

# 3. 描述
hbase(main):003:0> describe_namespace 'student'
DESCRIPTION
{NAME => 'student'}
1 row(s) in 0.0350 seconds

# 4. 删除
hbase(main):004:0> drop_namespace 'student'
0 row(s) in 0.9010 seconds

table :

# 1. 创建表 : 创建表时必须指定表名及列簇
hbase(main):005:0> create 't1','f1','f2','f3'
0 row(s) in 1.4800 seconds
=> Hbase::Table - t1

hbase(main):006:0> create_namespace 'student'
0 row(s) in 0.8690 seconds

hbase(main):007:0> create 'student:stu_info','info'
0 row(s) in 1.2720 seconds
=> Hbase::Table - student:stu_info

# 2. 查看所有表 : ns_name:tb_name:唯一确定一张表,除默认namespace
hbase(main):008:0> list
TABLE
student:stu_info
t1
2 row(s) in 0.0230 seconds
=> ["student:stu_info", "t1"]

# 3. 描述表
hbase(main):009:0> desc 'student:stu_info'
Table student:stu_info is ENABLED
student:stu_info
COLUMN FAMILIES DESCRIPTION
{NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER'
, COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
1 row(s) in 0.0470 seconds

# 4. 删除表
hbase(main):011:0> drop 't1'
ERROR: Table t1 is enabled. Disable it first. # 删除时必须先禁用表,如果是修改表则先禁用-修改-启用
Here is some help for this command:
Drop the named table. Table must first be disabled:
  hbase> drop 't1'
  hbase> drop 'ns1:t1'

# 启用表
hbase(main):014:0> enable 't1'
0 row(s) in 1.2930 seconds

# 禁用表
hbase(main):013:0> disable 't1'
0 row(s) in 2.3050 seconds

hbase(main):015:0> desc 't1'
Table t1 is ENABLED
t1
COLUMN FAMILIES DESCRIPTION
{NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER',
COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
{NAME => 'f2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER',
COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
{NAME => 'f3', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER',
COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}
3 row(s) in 0.0250 seconds

hbase(main):018:0> drop 't1'
0 row(s) in 1.2740 seconds

# 5. 变更表信息 : 将 info 列族中的数据存放 3个版本
hbase(main):019:0> alter 't1',{NAME=>'info',VERSIONS=>3}

表的 DML :

# 1. 增 : 增加数据,当不指定时间戳调用 add() 方法时,Put 实例会使用来自构造函数的可选时间戳参数,如果用户在构造 Put 实例时也没有指定的时间戳,则时间戳将会由 region 服务器设定
hbase(main):008:0> put 'student:stu_info','20161204_1001','info:name','laosi'
hbase(main):009:0> put 'student:stu_info','20161204_1001','info:age','18'
hbase(main):010:0> put 'student:stu_info','20161204_1001','info:sex','male'

hbase(main):011:0> put 'student:stu_info','20161204_1002','info:name','laoer'
hbase(main):012:0> put 'student:stu_info','20161204_1002','info:age','20'

hbase(main):013:0> put 'student:stu_info','20161204_1003','info:name','laosan'
hbase(main):014:0> put 'student:stu_info','20161204_1003','info:age','22'

# 2. 查
# get : 查询最快的方式,必须指定rowkey
hbase(main):011:0> get 'student:stu_info','20161204_1001'
COLUMN                                   CELL
 info:age                                timestamp=1602292498914, value=18
 info:name                               timestamp=1602292487848, value=laosi
 info:sex                                timestamp=1602292504135, value=male
1 row(s) in 0.0400 seconds

# scan : 全局扫描,企业中不常用
hbase(main):012:0> scan 'student:stu_info'
ROW                                     COLUMN+CELL
 20161204_1001                           column=info:age, timestamp=1602292498914, value=18
 20161204_1001                           column=info:name, timestamp=1602292487848, value=laosi
 20161204_1001                           column=info:sex, timestamp=1602292504135, value=male
 20161204_1002                           column=info:age, timestamp=1602292522543, value=20
 20161204_1002                           column=info:name, timestamp=1602292512868, value=laoer
 20161204_1003                           column=info:age, timestamp=1602292537942, value=22
 20161204_1003                           column=info:name, timestamp=1602292533522, value=laosan
3 row(s) in 0.0310 seconds

# scan + 过滤:企业中最常用的方式,速度仅次于get,包头不包尾
hbase(main):013:0> scan 'student:stu_info',{STARTROW=>'20161204_1001'}
ROW                                      COLUMN+CELL
 20161204_1001                           column=info:age, timestamp=1602292498914, value=18
 20161204_1001                           column=info:name, timestamp=1602292487848, value=laosi
 20161204_1001                           column=info:sex, timestamp=1602292504135, value=male
 20161204_1002                           column=info:age, timestamp=1602292522543, value=20
 20161204_1002                           column=info:name, timestamp=1602292512868, value=laoer
 20161204_1003                           column=info:age, timestamp=1602292537942, value=22
 20161204_1003                           column=info:name, timestamp=1602292533522, value=laosan
3 row(s) in 0.0250 seconds
hbase(main):014:0> scan 'student:stu_info',{STARTROW=>'20161204_1001',STOPROW=>'20161204_1002'}
ROW                                      COLUMN+CELL
 20161204_1001                           column=info:age, timestamp=1602292498914, value=18
 20161204_1001                           column=info:name, timestamp=1602292487848, value=laosi
 20161204_1001                           column=info:sex, timestamp=1602292504135, value=male
1 row(s) in 0.0320 seconds

# 3. 删 : 在 hbase 中,所有的删除不是立即执行,flush,在溢写到磁盘以后,compact操作时候执行的
# 删除某 rowkey 的某一列数据
hbase(main):015:0> delete 'student:stu_info','20161204_1003','info:name'
0 row(s) in 0.0360 seconds
# 删除某 rowkey 的全部数据
hbase(main):018:0> deleteall 'student:stu_info','20161204_1003'
0 row(s) in 0.0150 seconds

# 4. 改 : hbase中没有改的概念

# 5. 统计表数据行数
hbase(main):017:0> count 'student:stu_info'
3 row(s) in 0.0210 seconds

# 6. 清空表数据 : 清空表的操作顺序为先 disable,然后再 truncate
hbase(main):019:0> truncate 'student:stu_info'
Truncating 'student:stu_info' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 3.4730 seconds
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值