HBASE的shell操作


学习重点:
hbase中数据查询的方式:
1)全表扫描
2)指定rowkey范围进行扫描
3)单条数据查询

进入shell界面(启动hbase客户端): hbase shell

hbase shell
在这里插入图片描述

进入客户端后,可以使用help命令来查看命令分组:

help
在这里插入图片描述
eg:help “list_namespace”
List all namespaces in hbase. Optional regular expression parameter could
be used to filter the output. Examples:
hbase> list_namespace
hbase> list_namespace ‘abc.*’

命令分组为:

COMMAND GROUPS:
  Group name: general
  Commands: status, table_help, version, whoami

  Group name: 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, 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

  Group name: tools
  Commands: assign, balance_switch, balancer, balancer_enabled, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, close_region, compact, compact_rs, flush, major_compact, merge_region, move, normalize, normalizer_enabled, normalizer_switch, split, trace, unassign, wal_roll, zk_dump

  Group name: replication
  Commands: add_peer, append_peer_tableCFs, disable_peer, disable_table_replication, enable_peer, enable_table_replication, list_peers, list_replicated_tables, remove_peer, remove_peer_tableCFs, set_peer_tableCFs, show_peer_tableCFs

  Group name: snapshots
  Commands: clone_snapshot, delete_all_snapshot, delete_snapshot, list_snapshots, restore_snapshot, snapshot

  Group name: configuration
  Commands: update_all_config, update_config

  Group name: quotas
  Commands: list_quotas, set_quota

  Group name: security
  Commands: grant, list_security_capabilities, revoke, user_permission

  Group name: procedures
  Commands: abort_procedure, list_procedures

  Group name: visibility labels
  Commands: add_labels, clear_auths, get_auths, list_labels, set_auths, set_visibility

Group name: general 通用命令

Commands: status, table_help, version, whoami

查看集群状态:status

status

查看hbase的版本:version

version

查看安装用户:whoami

whoami

Group name: namespace

Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables

创建namespace: create_namespace

在hbase中,是没有数据库database的概念的,替代概念是namespace

create_namespace 'namespace名字'
eg:create_namespace 'bd1906'
查看namespace列表:list_namespace

list_namespace

出现如下:

NAMESPACE                                             
bd1904:自己创建的namespace                                      
default:是默认namespace,建表时如果没有指定namespace,则这个表属于这个default namespace的。      
hbase:hbase的系统namespace 
查看namespace的详细信息:describe_namespace

describe_namespace ‘ns’
eg:describe_namespace ‘bd1904’
注意:因此是csdn的问题,’ ’ 应该是英文的,但此除却显示是中文。

修改 namespace:alter_namespace(了解)

alter_namespace :针对属性修改的

删除namespace:drop_namespace

drop_namespace ‘ns’
eg:drop_namespace ‘bd1906’

Group name: 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, locate_region, show_filters

创建表:create

建表的时候至少指定一个列族

  • 语法1: 可以指定每一个列族的属性的
create 'namespace:表名', {NAME => '列族1的名'}, {NAME => '列族2的名字'}, {NAME => '列族3的名字'}
create 't1', {NAME => 'f1',  TTL => 2592000}
eg:
create 'bd1906:test01',{NAME => 'info01'},{NAME => 'info02'}
create 'bd1906:test02',{NAME => 'info01',VERSION => 3},{NAME => 'info02'}

注意NAME,VERSIONS必须大写,否则系统不识别

  • 语法2: 使用默认属性
create '表名', '列族1', '列族2', '列族3'
eg:
create 'bd1904_01','info01','info02'
查看表列表:list
list:显示所有表
list 'namespace:.*'   : 查看指定namespace下的所有表
eg:list 'bd1906:.*' 
list 'namespace:b.*'  :  查看指定namespace下的某些特征(b开头)的表名
list_namespace_tables 'namespace'  :查看指定namespace下的所有表
eg:list_namespace_tables 'bd1904' 
查看表的详细描述信息:describe|desc

describe|desc ‘表名’
eg:
describe “bd1904:test02”
desc “bd1904:test02”

hbase(main):012:0> describe "bd1904:test02"
Table bd1904:test02 is ENABLED                                                            
bd1904:test02                                                                             
COLUMN FAMILIES DESCRIPTION                                                               
{NAME => 'info01', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELET
ED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE
', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '
0'}                                                                                       
{NAME => 'info02', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELET
ED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE
', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '
0'}  

上面一个大括号{}描述一个列族信息。

修改表:alter
  • 添加列族
alter '表名', NAME => '新的列族名', VERSIONS => 5
alter 'bd1906:test01',NAME => 'info03', VERSIONS => 2
  • 删除列族
alter 'namespace:表名', NAME => '需要删除的列族', METHOD => 'delete'
alter 'namespace:表名', 'delete' => '需要删除的列族'
eg:
alter "bd1906:test01",NAME => "info01",METHOD => "delete"
alter "bd1906:test01","delete" => "info03"

注意:如果表中只剩下一个列族,是不允许删除的

修改表状态信息
禁用表:disable
disable '表名' 禁用某一个表
disable_all 'namespace:.*' 禁用指定 namespace 下的所有表

disable 'bd1906:test01'
disable_all 'bd1906:.*'
启用表:enable
enable '表名' 启用表
enable_all

enable 'bd1906:test01'
enable_all 'bd1906:.*'
查看表状态
hbase中, 表具有两个状态:
enable:启用状态   可以执行任何操作的
disable:禁用状态   不允许执行写操作

is_disabled '表名':  查看表是否被禁用  true   false
is_enabled '表名':  查看表是否被启用  true   false
is_disabled 'bd1906:test01'
is_enabled 'bd1906:test01'
删除表

先禁用表,再删除表

drop 'ns:t' 删除指定表
drop_all 'namespace:.*'  删除指定namespace下的所有表
eg:
disable 'bd1906_01'  (先禁用表)
drop 'bd1906_01'   (再删除表)
eg:
disable_all 'bd1906:.*'
drop_all "bd1906:.*"

Group name: dml

Commands: append, count, delete, deleteall, get, get_counter, get_splits, incr, put, scan, truncate, truncate_preserve

先创建一个表:
create “bd1906:test01”,“info01”,“info02”

表数据: 行健、 列 、 列值、 时间戳

数据插入
hbase> put 'namespace:表名', '行健', '列族:列名', '列的值', 时间戳

put 'bd1906:test01','rk001','info01:name','zs'
put 'bd1906:test01','rk001','info02:name','ww'
put 'bd1906:test01','rk001','info02:age','15'
put 'bd1906:test01','rk000','info01:age','13'
put 'bd1906:test01','rk0000','info01:age','13'

每一次只能插入一行的 一列

扫描查询:scan (一次返回多条数据)
help 'scan'
hbase> scan 'namespace:表名'  全表扫描
hbase> scan 'namespace:表名', {COLUMNS => ['c1', 'c2']}  指定需要扫描的列
hbase> scan 'namespace:表名', {COLUMNS => ['c1', 'c2'],LIMIT => 10}   指定查询的行数  LIMIT  按照行健排序 返回指定的前几行的数据
hbase> scan 'namespace:t1', {COLUMNS => ['c1', 'c2'], STARTROW => ,ENDROW => }  指定行健范围  扫描
hbase> scan 'namespace:t1', {COLUMNS => 'c1', TIMERANGE => [1303668804, 1303668904]}  指定时间戳范围扫描

全表扫描: scan “表名”

eg:
hbase(main):183:0> scan 'bd1906:test01'
ROW                            COLUMN+CELL                                                                             
 rk000                         column=info01:age, timestamp=1583218552512, value=13                                    
 rk0000                        column=info01:age, timestamp=1583218556485, value=13                                    
 rk001                         column=info01:name, timestamp=1583218552117, value=zs                                   
 rk001                         column=info02:age, timestamp=1583218552401, value=15                                    
 rk001                         column=info02:name, timestamp=1583218552245, value=ww 

指定需要扫描的列: scan ‘表名’, {COLUMNS => ‘列’}

eg:
hbase(main):198:0> scan 'bd1906:test01',{COLUMNS => "info01:name"}
ROW                            COLUMN+CELL                                                                             
 rk001                         column=info01:name, timestamp=1583218552117, value=zs  


hbase(main):199:0> scan 'bd1906:test01',{COLUMNS => ["info01:name","info01:age"]}
ROW                            COLUMN+CELL                                                                             
 rk000                         column=info01:age, timestamp=1583218552512, value=13                                    
 rk0000                        column=info01:age, timestamp=1583218556485, value=13                                    
 rk001                         column=info01:name, timestamp=1583218552117, value=zs 

指定从起始行健开始,返回指定条数的数据

eg:指定显示10行:(默认从第一条数据开始扫描的)
hbase(main):200:0> scan 'user_info',{COLUMNS => ["base_info:name","base_info:age"],LIMIT=>10}
ROW                            COLUMN+CELL                                                                             
 baiyc_20150716_0001           column=base_info:age, timestamp=1583269100408, value=21                                 
 baiyc_20150716_0001           column=base_info:name, timestamp=1583269100016, value=baiyc1                            
 baiyc_20150716_0002           column=base_info:age, timestamp=1583269100447, value=22                                 
 baiyc_20150716_0002           column=base_info:name, timestamp=1583269100037, value=baiyc2                            
 baiyc_20150716_0003           column=base_info:age, timestamp=1583269100475, value=23                                 
 baiyc_20150716_0003           column=base_info:name, timestamp=1583269100061, value=baiyc3                            
 baiyc_20150716_0004           column=base_info:age, timestamp=1583269100595, value=24                                 
 baiyc_20150716_0004           column=base_info:name, timestamp=1583269100087, value=baiyc4                            
 baiyc_20150716_0005           column=base_info:age, timestamp=1583269100629, value=25                                 
 baiyc_20150716_0005           column=base_info:name, timestamp=1583269100115, value=baiyc5                            
 baiyc_20150716_0006           column=base_info:age, timestamp=1583269100664, value=26                                 
 baiyc_20150716_0006           column=base_info:name, timestamp=1583269100140, value=baiyc6                            
 baiyc_20150716_0007           column=base_info:age, timestamp=1583269100688, value=27                                 
 baiyc_20150716_0007           column=base_info:name, timestamp=1583269100170, value=baiyc7                            
 baiyc_20150716_0008           column=base_info:age, timestamp=1583269100727, value=28                                 
 baiyc_20150716_0008           column=base_info:name, timestamp=1583269100311, value=baiyc8                            
 rk0001                        column=base_info:name, timestamp=1583269121269, value=zhangsan                          
 user0000                      column=base_info:age, timestamp=1583268901476, value=18                                 
 user0000                      column=base_info:name, timestamp=1583268900928, value=luoyufeng 

指定需要扫描的起始行健(从起始行健开始扫描): scan ‘ns1:t1’, {COLUMNS => [‘c1’, ‘c2’],STARTROW => ‘xyz’}

eg:  scan 'user_info',{STARTROW => "zhangsan_20150701_0001"}  从指定行开始,默认到末尾 (包含开始行健)

hbase(main):207:0> scan 'user_info',{STARTROW => "zhangsan_20150701_0001"} 
ROW                            COLUMN+CELL                                                                             
 zhangsan_20150701_0001        column=base_info:age, timestamp=1583269099211, value=21                                 
 zhangsan_20150701_0001        column=base_info:name, timestamp=1583269079865, value=zhangsan1                         
 zhangsan_20150701_0001        column=extra_info:Hobbies, timestamp=1583269099799, value=music                         
 zhangsan_20150701_0002        column=base_info:age, timestamp=1583269099260, value=22                                 
 zhangsan_20150701_0002        column=base_info:name, timestamp=1583269080007, value=zhangsan2                         
 zhangsan_20150701_0002        column=extra_info:Hobbies, timestamp=1583269099833, value=sport                         
 zhangsan_20150701_0003        column=base_info:age, timestamp=1583269099380, value=23                                 
 zhangsan_20150701_0003        column=base_info:name, timestamp=1583269080084, value=zhangsan3                         
 zhangsan_20150701_0003        column=extra_info:Hobbies, timestamp=1583269099866, value=music                         
 zhangsan_20150701_0004        column=base_info:age, timestamp=1583269099549, value=24                                 
 zhangsan_20150701_0004        column=base_info:name, timestamp=1583269080154, value=zhangsan4                         
 zhangsan_20150701_0004        column=extra_info:Hobbies, timestamp=1583269099892, value=sport                         
 zhangsan_20150701_0005        column=base_info:age, timestamp=1583269099585, value=25                                 
 zhangsan_20150701_0005        column=base_info:name, timestamp=1583269080200, value=zhangsan5                         
 zhangsan_20150701_0005        column=extra_info:Hobbies, timestamp=1583269099923, value=music                         
 zhangsan_20150701_0006        column=base_info:age, timestamp=1583269099614, value=26                                 
 zhangsan_20150701_0006        column=base_info:name, timestamp=1583269080236, value=zhangsan6                         
 zhangsan_20150701_0006        column=extra_info:Hobbies, timestamp=1583269099962, value=sport                         
 zhangsan_20150701_0007        column=base_info:age, timestamp=1583269099648, value=27                                 
 zhangsan_20150701_0007        column=base_info:name, timestamp=1583269080335, value=zhangsan7                         
 zhangsan_20150701_0007        column=extra_info:Hobbies, timestamp=1583269099985, value=music                         
 zhangsan_20150701_0008        column=base_info:age, timestamp=1583269099699, value=28                                 
 zhangsan_20150701_0008        column=base_info:name, timestamp=1583269081472, value=zhangsan8 

指定需要扫描的结束行健: scan ‘t1’, {COLUMNS => [‘c1’, ‘c2’], STARTROW =>"", ENDROW => ‘xyz’}

scan 'user_info',{ENDROW => "zhangsan_20150701_0002"}  默认从最开始,到指定行健结束(不包含结尾行健)

hbase(main):218:0> scan 'user_info',{ENDROW => "zhangsan_20150701_0002"} 
ROW                            COLUMN+CELL                                                                             
 baiyc_20150716_0001           column=base_info:age, timestamp=1583269100408, value=21                                 
 baiyc_20150716_0001           column=base_info:name, timestamp=1583269100016, value=baiyc1                            
 baiyc_20150716_0001           column=extra_info:Hobbies, timestamp=1583269100749, value=music                         
 baiyc_20150716_0002           column=base_info:age, timestamp=1583269100447, value=22                                 
 baiyc_20150716_0002           column=base_info:name, timestamp=1583269100037, value=baiyc2                            
 baiyc_20150716_0002           column=extra_info:Hobbies, timestamp=1583269100770, value=sport                         
 baiyc_20150716_0003           column=base_info:age, timestamp=1583269100475, value=23                                 
 baiyc_20150716_0003           column=base_info:name, timestamp=1583269100061, value=baiyc3                            
 baiyc_20150716_0003           column=extra_info:Hobbies, timestamp=1583269100811, value=music                         
 baiyc_20150716_0004           column=base_info:age, timestamp=1583269100595, value=24                                 
 baiyc_20150716_0004           column=base_info:name, timestamp=1583269100087, value=baiyc4                            
 baiyc_20150716_0004           column=extra_info:Hobbies, timestamp=1583269100834, value=sport                         
 baiyc_20150716_0005           column=base_info:age, timestamp=1583269100629, value=25                                 
 baiyc_20150716_0005           column=base_info:name, timestamp=1583269100115, value=baiyc5                            
 baiyc_20150716_0005           column=extra_info:Hobbies, timestamp=1583269100853, value=music                         
 baiyc_20150716_0006           column=base_info:age, timestamp=1583269100664, value=26                                 
 baiyc_20150716_0006           column=base_info:name, timestamp=1583269100140, value=baiyc6                            
 baiyc_20150716_0006           column=extra_info:Hobbies, timestamp=1583269100895, value=sport                         
 baiyc_20150716_0007           column=base_info:age, timestamp=1583269100688, value=27                                 
 baiyc_20150716_0007           column=base_info:name, timestamp=1583269100170, value=baiyc7                            
 baiyc_20150716_0007           column=extra_info:Hobbies, timestamp=1583269100915, value=music                         
 baiyc_20150716_0008           column=base_info:age, timestamp=1583269100727, value=28                                 
 baiyc_20150716_0008           column=base_info:name, timestamp=1583269100311, value=baiyc8                            
 baiyc_20150716_0008           column=extra_info:Hobbies, timestamp=1583269102034, value=sport                         
 rk0001                        column=base_info:name, timestamp=1583269121269, value=zhangsan                          
 user0000                      column=base_info:age, timestamp=1583268901476, value=18                                 
 user0000                      column=base_info:gender, timestamp=1583268901568, value=female                          
 user0000                      column=base_info:name, timestamp=1583268900928, value=luoyufeng                         
 user0000                      column=extra_info:size, timestamp=1583268903567, value=34                               
 user0001                      column=base_info:name, timestamp=1583269068552, value=zhangsan1                         
 zhangsan_20150701_0001        column=base_info:age, timestamp=1583269099211, value=21                                 
 zhangsan_20150701_0001        column=base_info:name, timestamp=1583269079865, value=zhangsan1                         
 zhangsan_20150701_0001        column=extra_info:Hobbies, timestamp=1583269099799, value=music  

指定开始行键和结束行键(含头不含尾):

eg:
hbase(main):001:0> scan "user_info",{COLUMNS => "base_info:age",STARTROW => "rk0001",ENDROW => "zhangsan_20150701_0005"}
ROW                            COLUMN+CELL                                                                             
 user0000                      column=base_info:age, timestamp=1583268901476, value=18                                 
 zhangsan_20150701_0001        column=base_info:age, timestamp=1583269099211, value=21                                 
 zhangsan_20150701_0002        column=base_info:age, timestamp=1583269099260, value=22                                 
 zhangsan_20150701_0003        column=base_info:age, timestamp=1583269099380, value=23                                 
 zhangsan_20150701_0004        column=base_info:age, timestamp=1583269099549, value=24 

指定时间戳范围的数据:(时间戳范围:含头不含尾)

所有时间戳范围内的数据都会显示出来,此语法效率不高。
eg:
scan "user_info",{COLUMNS => "base_info:age",TIMERANGE => [1565468004512,1565468005371]}

hbase(main):012:0> scan "user_info",{COLUMNS => "base_info:age",TIMERANGE => [1565468004512,1565468005371]}
ROW                            COLUMN+CELL                                                                             
0 row(s) in 0.0460 seconds

注意:
hbase中的表数据按照行健排序的,底层存储,是按照rk的字典顺序进行升序排的

查询单条数据:get (一条一个行健)
hbase> get 'namespace:表名', '行健'  查询某一行的所有数据
hbase> get 'namespace:表名', '行健', {TIMERANGE => [ts1, ts2]} 查询某一行  返回指定时间戳范围的列的值
hbase> get 'namespace:t1', '行健', {COLUMN => '列'}  查询某一行指定列的数据
hbase> get 'namespace:t1', '行健', {COLUMN => '列', TIMESTAMP => 时间戳}  指定某一行的某一个列的  某一个时间戳对应的值  cell的定位   
hbase> get 'namespace:t1', '行健', {COLUMN => '列', VERSIONS => 4}  查询某一行 某一列的 指定版本的数据

eg:
get 'user_info','zhangsan_20150701_0001'
get 'user_info','zhangsan_20150701_0001',{TIMERANGE => [1571642275758,1571642295063]}   

get "user_info","zhangsan_20150701_0001",{COLUMN => "base_info:name"}
get "user_info","zhangsan_20150701_0001",{COLUMN => ["base_info:name","extra_info:Hobbies"]}

get "user_info","zhangsan_20150701_0001",{COLUMN => "base_info:name",VERSIONS => 3}
get "user_info","zhangsan_20150701_0001",{COLUMN => "base_info:name",TIMESTAMP => 1571643964566}
数据删除:delete
delete 'namespace:表名', '行健', '列族:列', 版本(时间戳)
delete "user_info","zhangsan_20150701_0001","base_info:name",1571643964566
清空表:truncate

清空表的顺序是:先禁用表—> 再清空数据—> 再启用表

truncate '表名’
truncate "bd1906:test01"
统计表数据行数:count
count 'namespace:表名'
count 'user_info'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值