HBase- ddl(表操作)、dml(記錄操作)的基本語法操作
阿新 • • 發佈:2018-12-04
1. ddl 操作
主要針對表為物件進行操作,如建立表、修改表、刪除表等。
設計表 students 作為演示:
- 建立表
hbase(main):091:0> create 'students','stu_id','basic_info','school_info'
0 row(s) in 2.3350 seconds
=> Hbase::Table - students
- 查詢所有表
hbase(main):092:0> list TABLE students 1 row(s) in 0.0150 seconds => ["students"]
- 查看錶結構
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:表示該表正在使用。
- 刪除列族
刪除方法有多種,這裡列舉一種較為簡單的。
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
- 刪除表
這裡就不實際操作了。
disable 'students'
drop 'students'
- 查詢表是否存在
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
- 查看錶使用狀態
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
- 插入記錄
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'
- 查詢記錄
格式:
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
- 為某條資料增加版本
查詢的為最新的版本。
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
- 通過時間戳獲不同版本資料
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
- 全表掃描
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
- 刪除某列族的某列
可以看出,存在多個版本的列需要刪除多次,直到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
- 以行鍵為單位,查詢表的行數
hbase(main):003:0> stu.count
1 row(s) in 0.3530 seconds
=> 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 表,然後刪除表,再根據表結構建立一個相同的表。