Hbase详细笔记三[shell客户端] (附带讲解视频)

1 本节目录

  1. 基本命令

  2. DDL命令

  3. DML命令

  4. namespace命令

  5. 高级命令

hbase和HDFS类似也提供了shell端操作指令和java客户端API

进入大hbase的客户端    hbase shell 

例举出系统中所支持的所有的指令  help

查看某个命令的详细使用说明  help "command"

[root@linux01 ~]# hbase shell
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/opt/apps/hbase-2.0.4/lib/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/opt/apps/hadoop-2.8.5/share/hadoop/common/lib/slf4j-log4j12-1.7.10.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]
2020-06-10 21:58:18,209 WARN  [main] util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
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.0.4, r205e39c5704bf38568b34926dde9f1ee76e6b5d0, Fri Dec 28 22:13:42 PST 2018
Took 0.0077 seconds                                                                       
hbase(main):001:0> 

hbase(main):001:0> help
HBase Shell, version 2.0.4, r205e39c5704bf38568b34926dde9f1ee76e6b5d0, Fri Dec 28 22:13:42 PST 2018
Type 'help "COMMAND"', (e.g. 'help "get"' -- the quotes are necessary) for help on a specific command.
Commands are grouped. Type 'help "COMMAND_GROUP"', (e.g. 'help "general"') for help on a command group.

COMMAND GROUPS:
  Group name: general
  Commands: processlist, 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, 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

  Group name: tools
  Commands: assign, balance_switch, balancer, balancer_enabled, catalogjanitor_enabled, catalogjanitor_run, catalogjanitor_switch, cleaner_chore_enabled, cleaner_chore_run, cleaner_chore_switch, clear_block_cache, clear_compaction_queues, clear_deadservers, close_region, compact, compact_rs, compaction_state, flush, is_in_maintenance_mode, list_deadservers, major_compact, merge_region, move, normalize, normalizer_enabled, normalizer_switch, split, splitormerge_enabled, splitormerge_switch, trace, unassign, wal_roll, zk_dump

  Group name: replication
  Commands: add_peer, append_peer_namespaces, append_peer_tableCFs, disable_peer, disable_table_replication, enable_peer, enable_table_replication, get_peer_config, list_peer_configs, list_peers, list_replicated_tables, remove_peer, remove_peer_namespaces, remove_peer_tableCFs, set_peer_bandwidth, set_peer_exclude_namespaces, set_peer_exclude_tableCFs, set_peer_namespaces, set_peer_replicate_all, set_peer_tableCFs, show_peer_tableCFs, update_peer_config

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

  Group name: configuration
  Commands: update_all_config, update_config

  Group name: quotas
  Commands: list_quota_snapshots, list_quota_table_sizes, list_quotas, list_snapshot_sizes, set_quota

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

  Group name: procedures
  Commands: list_locks, list_procedures

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

  Group name: rsgroup
  Commands: add_rsgroup, balance_rsgroup, get_rsgroup, get_server_rsgroup, get_table_rsgroup, list_rsgroups, move_namespaces_rsgroup, move_servers_namespaces_rsgroup, move_servers_rsgroup, move_servers_tables_rsgroup, move_tables_rsgroup, remove_rsgroup, remove_servers_rsgroup

SHELL USAGE:
Quote all names in HBase Shell such as table and column names.  Commas delimit
command parameters.  Type after entering a command to run it.
Dictionaries of configuration used in the creation and alteration of tables are
Ruby Hashes. They look like this:

  {'key1' => 'value1', 'key2' => 'value2', ...}

and are opened and closed with curley-braces.  Key/values are delimited by the
'=>' character combination.  Usually keys are predefined constants such as
NAME, VERSIONS, COMPRESSION, etc.  Constants do not need to be quoted.  Type
'Object.constants' to see a (messy) list of all constants in the environment.

If you are using binary keys or values and need to enter them in the shell, use
double-quote'd hexadecimal representation. For example:

  hbase> get 't1', "key\x03\x3f\xcd"
  hbase> get 't1', "key\003\023\011"
  hbase> put 't1', "test\xef\xff", 'f1:', "\x01\x33\x40"

The HBase shell is the (J)Ruby IRB with the above HBase-specific commands added.
For more on the HBase Shell, see http://hbase.apache.org/book.html

1 基本命令

 1 processlist  当前正在处理的任务列表  比如 region的移动  拆分 合并
 2 version  查看hbase的版本 
      2.0.4, r205e39c5704bf38568b34926dde9f1ee76e6b5d0, Fri Dec 28 22:13:42 PST 2018

 3 whoami  当前用户
        hbase(main):006:0> whoami
        root (auth:SIMPLE)
            groups: root
        Took 0.0617 seconds

 4 table_help  查看表的基本用法


2 DDL命令

   create: 建表语法

  1)  create "a" , "cf1"  创建一个表名为a 列族为cf1的表
  2)  create "b" , "cf1" , "cf2"  创建一个表名为b 有cf1和cf2两个列族的表
  3)  create "tb_b" , {NAME => "cf1" , VERSIONS => 3  , TTL=>60*60*24*7} 建表的时候设置列族的属性  VERSIONS存储数据的版本数  TTL 列族中数据的过期时间

 

    alert: 修改表结构

1) alter "tb_a" , NAME=>"cf1" , VERSIONS=>3   如果列族cf1存在  修改cf1的属性
2) alter "tb_a" , NAME=>"cf2" , VERSIONS=>3, TTL=>240  如果cf2列族不存在  添加列族 并且属性设置
3) alter "tb_a" , 'delete' => 'cf1' 删除列族cf1

 

    describe:  查看表的结构信息的   等同于 desc   命令   "表名"

hbase(main):005:0> describe "tb_b"
Table tb_b is ENABLED                                                                     
tb_b                                                                                      
COLUMN FAMILIES DESCRIPTION                                                               
{NAME => 'cf1', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR =>
 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE => 'false', DATA_BLOCK_ENCODI
NG => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER
 => 'ROW', CACHE_INDEX_ON_WRITE => 'false', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE =>
 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true',
 BLOCKSIZE => '65536'}                                                                    
{NAME => 'cf2', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR =>
 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE => 'false', DATA_BLOCK_ENCODI
NG => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER
 => 'ROW', CACHE_INDEX_ON_WRITE => 'false', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE =>
 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true',
 BLOCKSIZE => '65536'}                             

 

    disable:  禁用表  禁用以后无法对表中的数据进行操作   disable "tb_name"

hbase(main):007:0>  disable "tb_a"
Took 1.5387 seconds  

 

    disable_all:  禁用多张表   disable_all  "tb.*"  根据正则表达式的规则来匹配多张表禁用

hbase(main):008:0> disableall "tb.*"
NoMethodError: undefined method `disableall' for main:Object
Did you mean?  disable_all
               disable

hbase(main):009:0> disable_all "tb.*"
tb_a                                                                                      
tb_b                                                                                      

Disable the above 2 tables (y/n)?
y
2 tables successfully disabled
Took 3.1963 seconds                   

 

    drop:   删除表 drop "tb_name"  首先将表禁用 否则无法删除

hbase(main):013:0> drop "tb_a"

ERROR: Table tb_a is enabled. Disable it first.

Drop the named table. Table must first be disabled:
  hbase> drop 't1'
  hbase> drop 'ns1:t1'

 

    drop_all:  删除多张禁用的表  根据正则表达式[匹配多张表 , 表首先禁用

    enable: 启动被禁用的表  enable  "表名"

    enable_all:    enable_all   "tb.*"  启用多张表  根据正则表达式匹配

hbase(main):012:0> enable_all "tb.*"
tb_a                                                                                      
tb_b                                                                                      

Enable the above 2 tables (y/n)?
y
2 tables successfully enabled
Took 3.2971 seconds          

 

    exists:  exists 表名  判断表是否存在

hbase(main):014:0> exists  "tb_b"
Table tb_b does exist                                                                     
Took 0.0180 seconds                                                                       
=> true
hbase(main):015:0> exists  "tb_x"
Table tb_x does not exist                                                                 
Took 0.0058 seconds                                                                       
=> false

 

    get_table: 获取表对象 

hbase(main):017:0> b=get_table "tb_b"
Took 0.0003 seconds                                                                       
=> Hbase::Table - tb_b
hbase(main):018:0> b.scan
ROW                     COLUMN+CELL                                                       
 rk001                  column=cf1:age, timestamp=1591778604652, value=33                 
 rk001                  column=cf1:cc, timestamp=1591779039918, value=\x00\x00\x00\x00\x00
                        \x00\x00\x15                                                      
 rk001                  column=cf1:name, timestamp=1591778604602, value=ls                
 rk001                  column=cf2:job, timestamp=1591778604687, value=teacher            
 rk002                  column=cf1:name, timestamp=1591778604736, value=ls2               
 rk003                  column=cf1:name, timestamp=1591778604764, value=ls3               
 rk004                  column=cf1:addr, timestamp=1591778605509, value=Beijing           
 rk004                  column=cf1:sal, timestamp=1591778604797, value=10000      

 

    is_disabled:  is_disabled "tb_name" 查看表是否在禁用状态     禁用状态(true)

    is_enabled: is_enabled  "tb_name"  查看表是否在启用用状态

hbase(main):020:0>  is_enabled  "tb_b"
true                                                                                      
Took 0.0189 seconds                                                                       
=> true

 

    list  查看默认名称空间下的所有的表

  

    hbase> list  列出默认名称空间下 default中的所有的表
    hbase> list 'abc.*'  列举出符合正则表达式规则的表
    hbase> list 'ns:abc.*'  列出出指定名称空间下的表
    hbase> list 'ns:.*'


    list_regions,   查看一张表的所有的region信息

hbase(main):023:0> list_regions  "tb_b"
                 SERVER_NAME |                                           REGION_NAME |  START_KEY |    END_KEY |  SIZE |   REQ |   LOCALITY |
 --------------------------- | ----------------------------------------------------- | ---------- | ---------- | ----- | ----- | ---------- |
 linux02,16020,1591783680684 | tb_b,,1591774845465.cedfe661eea04155f9fc0ccb707a96d1. |            |            |     0 |     4 |        1.0 |
 1 rows


    locate_region,  查看表的行建的region位置   hbase> locate_region 'tableName', 'key0'

hbase(main):024:0> locate_region  "tb_b" , "rk004"
HOST                                  REGION                                                                                                       
 linux02:16020                        {ENCODED => cedfe661eea04155f9fc0ccb707a96d1, NAME => 'tb_b,,1591774845465.cedfe661eea04155f9fc0ccb707a96d1.'
                                      , STARTKEY => '', ENDKEY => ''}                                                                              
1 row(s)
Took 0.0020 seconds                                                                                                                                
=> #


    show_filters   列出系统中支持的所有的过滤器

hbase(main):025:0> show_filters
DependentColumnFilter                                                                                                                              
KeyOnlyFilter                                                                                                                                      
ColumnCountGetFilter                                                                                                                               
SingleColumnValueFilter                                                                                                                            
PrefixFilter                                                                                                                                       
SingleColumnValueExcludeFilter                                                                                                                     
FirstKeyOnlyFilter                                                                                                                                 
ColumnRangeFilter                                                                                                                                  
ColumnValueFilter                                                                                                                                  
TimestampsFilter                                                                                                                                   
FamilyFilter                                                                                                                                       
QualifierFilter                                                                                                                                    
ColumnPrefixFilter                                                                                                                                 
RowFilter                                                                                                                                          
MultipleColumnPrefixFilter                                                                                                                         
InclusiveStopFilter                                                                                                                                
PageFilter                                                                                                                                         
ValueFilter                                                                                                                                        
ColumnPaginationFilter                                                                                                                             
Took 0.0187 seconds                                                                                                                                
=> #

 

3 DML命令

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

  • append     向指定的属性值的后面追加内容  没有属性创建  有属性追加
  •  count       统计表中的行数据的条数 (行键的个数)
  •  delete      删除某行的列
  •  deleteall  删除整行 多行 行内的列
  hbase> deleteall 'ns1:t1', 'r1'
  hbase> deleteall 't1', 'r1'
  hbase> deleteall 't1', 'r1', 'c1'
  hbase> deleteall 't1', 'r1', 'c1', ts1
  hbase> deleteall 't1', 'r1', 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'}

ROWPREFIXFILTER can be used to delete row ranges
  hbase> deleteall 't1', {ROWPREFIXFILTER => 'prefix'}
  hbase> deleteall 't1', {ROWPREFIXFILTER => 'prefix'}, 'c1'        //delete certain column family in the row ranges
  hbase> deleteall 't1', {ROWPREFIXFILTER => 'prefix'}, 'c1', ts1
  hbase> deleteall 't1', {ROWPREFIXFILTER => 'prefix'}, 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'}
  •  get 获取表中的数据
  hbase> t.get 'r1'
  hbase> t.get 'r1', {TIMERANGE => [ts1, ts2]}
  hbase> t.get 'r1', {COLUMN => 'c1'}
  hbase> t.get 'r1', {COLUMN => ['c1', 'c2', 'c3']}
  hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1}
  hbase> t.get 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4}
  hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4}
  hbase> t.get 'r1', {FILTER => "ValueFilter(=, 'binary:abc')"}
  hbase> t.get 'r1', 'c1'
  hbase> t.get 'r1', 'c1', 'c2'
  hbase> t.get 'r1', ['c1', 'c2']
  hbase> t.get 'r1', {CONSISTENCY => 'TIMELINE'}
  hbase> t.get 'r1', {CONSISTENCY => 'TIMELINE', REGION_REPLICA_ID => 1}
  •  get_counter  获取计数字段的值
  •  incr  创建计数属性 并可以累计计数
base(main):218:0> incr "tb_b" , "rk001" , "cf1:cc" , 2
COUNTER VALUE = 21
hbase(main):219:0> get_counter "tb_b" , "rk001" , "cf1:cc"
COUNTER VALUE = 21
  •  get_splits获取切割点
  •  put  插入数据 插入数据的之后指定 表名 行建 列族:属性 值
  hbase> put 'ns1:t1', 'r1', 'c1', 'value'
  hbase> put 't1', 'r1', 'c1', 'value'
  hbase> put 't1', 'r1', 'c1', 'value', ts1
  hbase> put 't1', 'r1', 'c1', 'value', {ATTRIBUTES=>{'mykey'=>'myvalue'}}
  hbase> put 't1', 'r1', 'c1', 'value', ts1, {ATTRIBUTES=>{'mykey'=>'myvalue'}}
  hbase> put 't1', 'r1', 'c1', 'value', ts1, {VISIBILITY=>'PRIVATE|SECRET'}
  • scan全表检索数据
hbase(main):031:0> scan "tb_b"
ROW                                   COLUMN+CELL                                                                                                  
 rk001                                column=cf1:age, timestamp=1591778604652, value=33                                                            
 rk001                                column=cf1:cc, timestamp=1591779039918, value=\x00\x00\x00\x00\x00\x00\x00\x15                               
 rk001                                column=cf1:name, timestamp=1591778604602, value=ls                                                           
 rk001                                column=cf2:job, timestamp=1591778604687, value=teacher                                                       
 rk002                                column=cf1:name, timestamp=1591778604736, value=ls2                                                          
 rk003                                column=cf1:name, timestamp=1591778604764, value=ls3                                                          
 rk004                                column=cf1:addr, timestamp=1591778605509, value=Beijing                                                      
 rk004                                column=cf1:sal, timestamp=1591778604797, value=10000                                                         
4 row(s)

 

4 namespace命令

Group name: namespace
  Commands: alter_namespace, create_namespace, describe_namespace, drop_namespace, list_namespace, list_namespace_tables

  1.   alter_namespace,               修改名称空间的属性
  2.   create_namespace,               创建
  3.   describe_namespace,           查看名称空间的属性
  4.   drop_namespace,                  删除名称空间 drop_namespace  "doit15"
  5.   list_namespace,                  列出系统中所有的名称空间
  6.   list_namespace_tables          列出指定名称空间下所有的表

你可能感兴趣的:(大数据之hbase)