HBase- ddl(表操作)、dml(记录操作)的基本语法操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38038143/article/details/84191110

1. ddl 操作

主要针对表为对象进行操作,如创建表、修改表、删除表等。
设计表 students 作为演示:

students

在这里插入图片描述

  1. 创建表
hbase(main):091:0> create 'students','stu_id','basic_info','school_info'
0 row(s) in 2.3350 seconds

=> Hbase::Table - students
  1. 查询所有表
hbase(main):092:0> list
TABLE                                                                                                 
students                                                                                              
1 row(s) in 0.0150 seconds

=> ["students"]
  1. 查看表结构
hbase(main):093:0> describe 'students'
Table students is ENABLED                                                                             
students                                                                                              
COLUMN FAMILIES DESCRIPTION                                                                           
{NAME => 'basic_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'}                             
{NAME => 'school_info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELL
S => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => 
'0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                            
{NAME => 'stu_id', 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.0300 seconds

Table students is ENABLED:表示该表正在使用。

  1. 删除列族
    删除方法有多种,这里列举一种较为简单的。
hbase(main):094:0> alter 'students', 'delete' => 'stu_id'
Updating all regions with the new schema...
0/1 regions updated.
1/1 regions updated.
Done.
0 row(s) in 3.9590 seconds

如果出现错误,可能由于版本问题,先将表的使用状况设置为disabled:

disable 'students'
enable 'students'

查看删除 stu_id 是否成功:

hbase(main):095:0> describe 'students'
Table students is ENABLED                                                                             
students                                                                                              
COLUMN FAMILIES DESCRIPTION                                                                           
{NAME => 'basic_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'}                             
{NAME => 'school_info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELL
S => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => 
'0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}                            
2 row(s) in 0.0530 seconds
  1. 删除表
    这里就不实际操作了。
disable 'students'
drop 'students'
  1. 查询表是否存在
hbase(main):096:0> exists 'students'
Table students does exist                                                                             
0 row(s) in 0.0110 seconds

hbase(main):097:0> exists 'teacher'
Table teacher does not exist                                                                          
0 row(s) in 0.0120 seconds
  1. 查看表使用状态
hbase(main):099:0> is_disabled 'students'
false                                                                                                 
0 row(s) in 0.0140 seconds

hbase(main):100:0> is_enabled 'students'
true                                                                                                  
0 row(s) in 0.0150 seconds

2. dml 操作

主要针对表的记录的操作,如插入记录、查询记录等。
首先获得表的“引用”,简化操作:

hbase(main):101:0> stu = get_table 'students'
0 row(s) in 0.0060 seconds

=> Hbase::Table - students
  1. 插入记录
stu.put '2018111701', 'basic_info:name','gyt'
stu.put '2018111701', 'basic_info:gender', 'male'
stu.put '2018111701', 'basic_info:birthday','2018-10-01'
stu.put '2018111701', 'basic_info:connect','15802801111'
stu.put '2018111701', 'basic_info:address','SiChuan-Chengdu'
stu.put '2018111701', 'school_info:college','Neusoft'
stu.put '2018111701', 'school_info:class','class 10 grade 3'
stu.put '2018111701', 'school_info:object','Computer Science and Technology'

在这里插入图片描述

  1. 查询记录
    格式:
get '表名' '行键'[, '列族[:列]']
  • 以行键查询
hbase(main):112:0* stu.get '2018111701'
COLUMN                     CELL                                                                       
 basic_info:address        timestamp=1542447284756, value=SiChuan-Chengdu                             
 basic_info:birthday       timestamp=1542447284707, value=2018-10-01                                  
 basic_info:connect        timestamp=1542447284735, value=15802801111                                 
 basic_info:gender         timestamp=1542447284671, value=male                                        
 basic_info:name           timestamp=1542447284632, value=gyt                                         
 school_info:class         timestamp=1542447284841, value=class 10 grade 3                            
 school_info:college       timestamp=1542447284783, value=Neusoft                                     
 school_info:object        timestamp=1542447289312, value=Computer Science and Technology             
1 row(s) in 0.0330 seconds
  • 以列族查询
hbase(main):114:0* stu.get '2018111701','basic_info'
COLUMN                     CELL                                                                       
 basic_info:address        timestamp=1542447284756, value=SiChuan-Chengdu                             
 basic_info:birthday       timestamp=1542447284707, value=2018-10-01                                  
 basic_info:connect        timestamp=1542447284735, value=15802801111                                 
 basic_info:gender         timestamp=1542447284671, value=male                                        
 basic_info:name           timestamp=1542447284632, value=gyt                                         
1 row(s) in 0.0220 seconds
  • 以列查询
hbase(main):115:0> stu.get '2018111701','basic_info:name'
COLUMN                     CELL                                                                       
 basic_info:name           timestamp=1542447284632, value=gyt                                         
1 row(s) in 0.0130 seconds
  1. 为某条数据增加版本
    查询的为最新的版本。
hbase(main):118:0* stu.put '2018111701', 'basic_info:connect', '15802802222'
0 row(s) in 0.0100 seconds
hbase(main):119:0> stu.get '2018111701','basic_info:connect'
COLUMN                     CELL                                                                       
 basic_info:connect        timestamp=1542447813631, value=15802802222                                 
1 row(s) in 0.0060 seconds
  1. 通过时间戳获不同版本数据
hbase(main):120:0> stu.get '2018111701', {COLUMN=>'basic_info:connect', TIMESTAMP=>1542447813631}
COLUMN                     CELL                                                                       
 basic_info:connect        timestamp=1542447813631, value=15802802222                                 
1 row(s) in 0.0160 seconds
hbase(main):121:0> stu.get '2018111701', {COLUMN=>'basic_info:connect', TIMESTAMP=>1542447284735}
COLUMN                     CELL                                                                       
 basic_info:connect        timestamp=1542447284735, value=15802801111                                 
1 row(s) in 0.0110 seconds
  1. 全表扫描
hbase(main):123:0* stu.scan
ROW                        COLUMN+CELL                                                                
 2018111701                column=basic_info:address, timestamp=1542447284756, value=SiChuan-Chengdu  
 2018111701                column=basic_info:birthday, timestamp=1542447284707, value=2018-10-01      
 2018111701                column=basic_info:connect, timestamp=1542447813631, value=15802802222      
 2018111701                column=basic_info:gender, timestamp=1542447284671, value=male              
 2018111701                column=basic_info:name, timestamp=1542447284632, value=gyt                 
 2018111701                column=school_info:class, timestamp=1542447284841, value=class 10 grade 3  
 2018111701                column=school_info:college, timestamp=1542447284783, value=Neusoft         
 2018111701                column=school_info:object, timestamp=1542447289312, value=Computer Science 
                           and Technology                                                             
1 row(s) in 0.0180 seconds
  1. 删除某列族的某列
    可以看出,存在多个版本的列需要删除多次,直到0个版本。
hbase(main):124:0> stu.delete '2018111701', 'basic_info:connect'
0 row(s) in 0.0110 seconds
hbase(main):125:0> stu.get '2018111701', 'basic_info'
COLUMN                     CELL                                                                       
 basic_info:address        timestamp=1542447284756, value=SiChuan-Chengdu                             
 basic_info:birthday       timestamp=1542447284707, value=2018-10-01                                  
 basic_info:connect        timestamp=1542447284735, value=15802801111                                 
 basic_info:gender         timestamp=1542447284671, value=male                                        
 basic_info:name           timestamp=1542447284632, value=gyt                                         
1 row(s) in 0.0070 seconds

hbase(main):126:0> stu.delete '2018111701', 'basic_info:connect'
0 row(s) in 0.0200 seconds
hbase(main):127:0> stu.get '2018111701', 'basic_info'
COLUMN                     CELL                                                                       
 basic_info:address        timestamp=1542447284756, value=SiChuan-Chengdu                             
 basic_info:birthday       timestamp=1542447284707, value=2018-10-01                                  
 basic_info:gender         timestamp=1542447284671, value=male                                        
 basic_info:name           timestamp=1542447284632, value=gyt                                         
1 row(s) in 0.0090 seconds
  1. 以行键为单位,查询表的行数
hbase(main):003:0> stu.count
1 row(s) in 0.3530 seconds
=> 1
  1. 清空整张表
hbase(main):005:0> truncate 'students'
Truncating 'students' table (it may take a while):
 - Disabling table...
 - Truncating table...
0 row(s) in 7.7470 seconds

hbase(main):006:0> stu.get '2018111701'
COLUMN                     CELL                                                                       
0 row(s) in 0.3510 seconds

步骤为:先disabled 表,然后删除表,再根据表结构创建一个相同的表。

完!

猜你喜欢

转载自blog.csdn.net/qq_38038143/article/details/84191110