hbase查看表结构_HBase语法「建议收藏」

2022-06-13 08:37:58 浏览数 (1)

大家好,我是架构君,一个会写代码吟诗的架构师。今天说一说hbase查看表结构_HBase语法「建议收藏」,希望能够帮助大家进步!!!

一、HBase的DDL语法


1. help

①help :查看所有的hbase的shell命令

②help 'cmd' :寻求指定命令的使用方法

e.g.

help 'create_namespace'

代码语言:javascript复制
hbase(main):012:0> help 'create_namespace'Create namespace; pass namespace name,and optionally a dictionary of namespace configuration.Examples:  hbase> create_namespace 'ns1'  hbase> create_namespace 'ns1', {
    
    'PROPERTY_NAME'=>'PROPERTY_VALUE'}

只听到从架构师办公室传来架构君的声音:

二子乘舟,泛泛其景。有谁来对上联或下联?

③namespace :命名空间,理解为Java中的包

hbase中的表看成是Java中的类

换句话说,namespace就是保存表的一个逻辑上的路径

2. namespace

2.1 namespace的常用操作命令

alter_namespace

修改命名空间的属性

create_namespace

创建命名空间

describe_namespace

查看命名空间的结构

drop_namespace

删除命名空间

list_namespace

查看HBase中所有的命名空间

list_namespace_tables

查看指定的命名空间中的所有表

2.2 list_namespace

代码语言:javascript复制
hbase(main):001:0> list_namespaceNAMESPACE                                                                                default                                                                                  hbase                                                                                    2 row(s) in 0.1800 seconds

若不指定命名空间,所有的表都创建在default下面

代码语言:javascript复制
hbase(main):002:0> help 'list_namespace'List all namespaces in hbase. Optional regular expression parameter couldbe used to filter the output. Examples:  hbase> list_namespace  hbase> list_namespace 'abc.*' hbase(main):003:0> list_namespace 'hbase'NAMESPACE                                                                                hbase                                                                                    1 row(s) in 0.0170 secondshbase(main):004:0> list_namespace 'h.*'NAMESPACE                                                                                hbase                                                                                    1 row(s) in 0.0090 secondshbase(main):005:0> list_namespace '^d'NAMESPACE                                                                                default                                                                                  1 row(s) in 0.0140 seconds

2.3 create_namespace

代码语言:javascript复制
hbase(main):006:0> create_namespace 'ns1'0 row(s) in 0.8890 secondshbase(main):007:0> list_namespaceNAMESPACE                                                                                default                                                                                  hbase                                                                                    ns1                                                                                      3 row(s) in 0.0140 seconds

2.4 describe_namespace

代码语言:javascript复制
hbase(main):008:0> describe_namespace 'ns1'DESCRIPTION                                                                              {
    
    NAME => 'ns1'}                                                                          1 row(s) in 0.0130 secondshbase(main):009:0> create_namespace 'ns2',{
    
    'name'=>'rock'}0 row(s) in 0.8690 secondshbase(main):010:0> describe_namespace 'ns2'DESCRIPTION                                                                              {
    
    NAME => 'ns2', name => 'rock'}                                                          1 row(s) in 0.0030 seconds

2.5 alter_namespace

修改namespace的属性

代码语言:javascript复制
hbase(main):011:0> help 'alter_namespace'Alter namespace properties.To add/modify a property:  hbase> alter_namespace 'ns1', {
    
    METHOD => 'set', 'PROPERTY_NAME' => 'PROPERTY_VALUE'}To delete a property:  hbase> alter_namespace 'ns1', {
    
    METHOD => 'unset', NAME=>'PROPERTY_NAME'}  e.g.# 为'ns2'设置属性hbase(main):012:0> alter_namespace 'ns2',{
    
    METHOD => 'set','name'=>'xiaoliu'}0 row(s) in 0.6270 secondshbase(main):013:0> describe_namespace 'ns2'DESCRIPTION                                                                              {
    
    NAME => 'ns2', name => 'xiaoliu'}                                                       1 row(s) in 0.0030 secondshbase(main):003:0> alter_namespace 'ns2',{
    
    METHOD=>'set','sex'=>'younggirlgirl'}0 row(s) in 0.6170 secondshbase(main):004:0> describe_namespace 'ns2'DESCRIPTION                                                                              {
    
    NAME => 'ns2', name => 'xiaoliu', sex => 'younggirlgirl'}                               1 row(s) in 0.0100 seconds# 将ns2中sex的属性撤销hbase(main):005:0> alter_namespace 'ns2',{
    
    METHOD=>'unset',NAME=>'sex'}0 row(s) in 0.6130 secondshbase(main):006:0> describe_namespace 'ns2'DESCRIPTION                                                                              {
    
    NAME => 'ns2', name => 'xiaoliu'}                                                       1 row(s) in 0.0060 seconds

2.6 list_namespace_tables

代码语言:javascript复制
hbase(main):010:0> create 'ns1:t1',{NAME=>'f1',VERSIONS=>5}0 row(s) in 2.3830 seconds=> Hbase::Table - ns1:t1hbase(main):011:0> listTABLE                                                                                    ns1:t1                                                                                   1 row(s) in 0.0100 seconds=> ["ns1:t1"]hbase(main):012:0> list_namespace_tables 'ns1'TABLE                                                                                    t1                                                                                       1 row(s) in 0.0200 secondshbase(main):013:0> list_namespace_tables 'hbase'TABLE                                                                                    meta                                                                                     namespace                                                                                2 row(s) in 0.0130 secondshbase(main):014:0> list_namespace_tables 'default'TABLE                                                                                    0 row(s) in 0.0050 seconds

2.7 drop_namespace

代码语言:javascript复制
hbase(main):015:0> help 'drop_namespace'Drop the named namespace. The namespace must be empty.hbase(main):016:0> drop_namespace 'ns2'0 row(s) in 0.8850 secondshbase(main):017:0> list_namespaceNAMESPACE                                                                                default                                                                                  hbase                                                                                    ns1                                                                                      3 row(s) in 0.0150 seconds# 这个命令只能删除为空的namespacehbase(main):019:0> drop_namespace 'ns1'ERROR: org.apache.hadoop.hbase.constraint.ConstraintException: Only empty namespaces can be removed. Namespace ns1 has 1 tablesCaused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.hbase.constraint.ConstraintException): Only empty namespaces can be removed. Namespace ns1 has 1 tablesHere is some help for this command:Drop the named namespace. The namespace must be empty.

3. table

3.1 关于表的常见命令

create

建表

alter

修改表

describe/desc

查看表结构

disable/disable_all

令表失效,在HBase中,只有失效的表才能删除/所有表失效

enable/enable_all

使表生效

drop/drop_all

删除表

exists

判断表是否存在

is_disabled/is_enabled

是否失效/生效

list

查询HBase所有的表

3.2 create

代码语言:javascript复制
hbase(main):010:0> create 'ns1:t1',{
    
    NAME=>'f1',VERSIONS=>5}# 创建一个namespace是ns1,表是t1,表有一个列簇叫f1,有5个版本hbase(main):026:0> create 't1',{
    
    NAME=>'f1'},{
    
    NAME=>'f2'},{
    
    NAME=>'f3'}hbase(main):026:0> create 't1','f1','f2','f3'# 上面的两句话表达的意思完全相同。这个t1表被创建在default的namespace中hbase(main):026:0> create 't1',{
    
    NAME => 'f1', VERSION => 1, TTL => 259200, BLOCKCACHE => true}# 在default的namespace下创建了表t1。指定列簇f1的属性的配置:VERSIONS,TTL,BLOCKCACHE

①未进行分片存储:

②伪分布式下的分片存储:

代码语言:javascript复制
hbase(main):032:0> create 'ns1:t2','f1',SPLITS => ['10','20','30']

每10行数据进行一次分片(0-9),(10-19),(20-29),第30行之后的分片一次(30- )

③高可用下的分片存储

代码语言:javascript复制
hbase(main):004:0> create 'ns1:t2','f1',SPLITS => ['10','20','30']

3.3 list

代码语言:javascript复制
#查询ns1下所有的表hbase(main):002:0> list 'ns1:.*'TABLE                                                                                    ns1:t1                                                                                   ns1:t2                                                                                   2 row(s) in 0.0070 seconds=> ["ns1:t1", "ns1:t2"]#查询所有命名空间的所有表hbase(main):003:0> listTABLE                                                                                    ns1:t1                                                                                   ns1:t2                                                                                   t1                                                                                       3 row(s) in 0.0130 seconds=> ["ns1:t1", "ns1:t2", "t1"]

3.4 describe、desc

代码语言:javascript复制
# 查看表结构hbase(main):004:0> desc 'ns1:t1'Table ns1:t1 is ENABLED                                                                  ns1:t1                                                                                   COLUMN FAMILIES DESCRIPTION                                                              {NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '5', 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.0880 seconds

3.5 alter

代码语言:javascript复制
# 修改添加删除列簇的信息,也可以修改添加删除表的属性# 1.修改指定表的指定列簇的属性hbase(main):005:0> alter 'ns1:t1',NAME =>'f1',VERSIONS =>10Updating all regions with the new schema...1/1 regions updated.Done.hbase(main):006:0> desc 'ns1:t1'Table ns1:t1 is ENABLED                                                                  ns1:t1                                                                                   COLUMN FAMILIES DESCRIPTION                                                              {
    
    NAME => 'f1', BLOOMFILTER => 'ROW', VERSIONS => '10', 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'}# 2.修改一张表中的多个列簇的属性hbase(main):010:0> alter 't1','f1',{NAME=>'f2',IN_MEMORY=>true},{NAME=>'f3',VERSIONS=>5}Updating all regions with the new schema...1/1 regions updated.Done.hbase(main):011:0> desc 'default:t1'Table default:t1 is ENABLED                                                              default: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 => 'true', 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 => '5', 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.删除一张表的列簇hbase(main):012:0> alter 't1','delete'=>'f1'Updating all regions with the new schema...1/1 regions updated.Done.hbase(main):013:0> desc 't1'Table t1 is ENABLED                                                                      t1                                                                                       COLUMN FAMILIES DESCRIPTION                                                              {
    
    NAME => 'f2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'true', 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 => '5', 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.6 exists

代码语言:javascript复制
hbase(main):014:0> exists 't1'Table t1 does exist

3.7 drop/enable/disable

代码语言:javascript复制
hbase(main):015: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):016:0> disable 't1'hbase(main):017:0> drop 't1'hbase(main):018:0> listTABLE                                                                                    ns1:t1                                                                                   ns1:t2 

二、HBase的DML语法


1. put命令:只能插入单行

代码语言:javascript复制
hbase(main):019:0> put 'ns1:t1','001','f1:id','1'- 'ns1:t1' :表示namespace:tablename- '001' :表示rowkey- 'f1:id' :f1表示 列簇:列名- '1' :表示列值hbase(main):020:0> put 'ns1:t1','002','f1:id','2',111111- 111111 :表示的是时间戳hbase(main):024:0> put 'ns1:t1','001','f1:name','xiaoliu'

2. scan命令:多行查询数据

代码语言:javascript复制
# 查询指定命名空间中的指定表的所有数据hbase(main):025:0> scan 'ns1:t1'ROW                     COLUMN CELL                                                       001                    column=f1:id, timestamp=1601194522018, value=1                    001                    column=f1:name, timestamp=1601195398366, value=xiaoliu            002                    column=f1:id, timestamp=111111, value=2  # 查询指定命名空间中的 指定表中的 指定列簇中的 指定列 hbase(main):026:0> scan 'ns1:t1',{COLUMNS => 'f1:id'}ROW                     COLUMN CELL                                                       001                    column=f1:id, timestamp=1601194522018, value=1                    002                    column=f1:id, timestamp=111111, value=2  # 分页查询 :# 和传统的关系型数据库不一样,# 关系型数据库的列可以是很多不同的类型,但是HBase中只能存储byte[]hbase(main):024:0> put 'ns1:t1','003','f1:school','hbau'hbase(main):030:0> scan 'ns1:t1',{COLUMNS => ['f1'], LIMIT => 2, STARTROW => '001'}ROW                     COLUMN CELL                                                       001                    column=f1:id, timestamp=1601194522018, value=1                    001                    column=f1:name, timestamp=1601195398366, value=xiaoliu            002                    column=f1:id, timestamp=111111, value=2# 按时间范围查询数据:时间左闭右开hbase(main):032:0> scan 'ns1:t1',{COLUMNS => 'f1',TIMERANGE => [111111,1601194522018]}ROW                     COLUMN CELL                                                       002                    column=f1:id, timestamp=111111, value=2                          hbase(main):033:0> scan 'ns1:t1',{COLUMNS => 'f1',TIMERANGE => [111111,1601194522019]}ROW                     COLUMN CELL                                                       001                    column=f1:id, timestamp=1601194522018, value=1                    002                    column=f1:id, timestamp=111111, value=2   

按版本查询数据

先进后出,栈来存储

代码语言:javascript复制
hbase(main):024:0> put 'ns1:t1','001','f1:name','liujinhong'hbase(main):001:0> scan 'ns1:t1'ROW                     COLUMN CELL                                                      001                    column=f1:id, timestamp=1601194522018, value=1                   001                    column=f1:name, timestamp=1601198548241, value=liujinhong        002                    column=f1:id, timestamp=111111, value=2                          003                    column=f1:school, timestamp=1601198178186, value=hbau  hbase(main):002:0> scan 'ns1:t1',{RAW => true,VERSIONS => 1}hbase(main):003:0> scan 'ns1:t1',{RAW => true,VERSIONS => 2} 

3. get命令:单行查询

代码语言:javascript复制
# 查询指定表中的行# 会将行中所有的列簇都查出来hbase(main):004:0> get 'ns1:t1','001'COLUMN                 CELL                                                             f1:id                 timestamp=1601194522018, value=1                                 f1:name               timestamp=1601198548241, value=liujinhong # 按照时间范围查询hbase(main):005:0> get 'ns1:t1','001',{COLUMNS => 'f1',TIMERANGE => [111111,1601194522019]}COLUMN                 CELL                                                             f1:id                 timestamp=1601194522018, value=1# 查询指定列簇的信息hbase(main):006:0> get 'ns1:t1','001',{COLUMNS => 'f1'}COLUMN                 CELL                                                             f1:id                 timestamp=1601194522018, value=1                                 f1:name               timestamp=1601198548241, value=liujinhong # 按版本查询hbase(main):007:0> get 'ns1:t1','001',{COLUMNS => 'f1',VERSIONS => 1}COLUMN                 CELL                                                             f1:id                 timestamp=1601194522018, value=1                                 f1:name               timestamp=1601198548241, value=liujinhong hbase(main):008:0> get 'ns1:t1','001',{COLUMNS => 'f1',VERSIONS => 2}COLUMN                 CELL                                                             f1:id                 timestamp=1601194522018, value=1                                 f1:name               timestamp=1601198548241, value=liujinhong                        f1:name               timestamp=1601195398366, value=xiaoliu 

4. 修改数据

代码语言:txt复制
   没有专门的修改数据的命令,我们使用put命令来修改数据。只要表名、列簇、列名 相同,那么后put的数据会覆盖先put的数据

5. 删除数据

代码语言:javascript复制
hbase(main):010:0> delete 'ns1:t1','002','f1:id'0 row(s) in 0.0350 secondshbase(main):011:0> scan 'ns1:t1'ROW                    COLUMN CELL                                                      001                   column=f1:id, timestamp=1601194522018, value=1                   001                   column=f1:name, timestamp=1601198548241, value=liujinhong        003                   column=f1:school, timestamp=1601198178186, value=hbau

6. count :统计表中的行键数

代码语言:javascript复制
hbase(main):012:0> count 'ns1:t1'2 row(s) in 0.0160 seconds=> 2

0 人点赞