HBase入門基本:名稱空間、建表、增刪改查
阿新 • • 發佈:2018-12-19
進入HBase資料庫
# hbase shell
注意HBase Shell 中的回格鍵沒用,要用【Ctrl+Backspace】,每個命令之後不需要分號(;)結束。
HBase幫助命令:
hbase> help 'create'
名稱空間
列出所有名稱空間
hbase> list_namespace
新建名稱空間
hbase> create_namespace 'ns1'
刪除名稱空間
hbase> drop_namespace 'ns1'
該名稱空間必須為空,否則會報錯。
修改名稱空間
hbase> alter_namespace 'ns', {METHOD => 'set', 'PROPERTY_NAME' => 'PROPERTY_VALUE'}
表
列出所有表
hbase> list
列出指定名稱空間下的所有表
hbase> list_namespace_tables 'ns1'
新建表
hbase> create 'ns1:t1', 'cf1'
新建一個以名稱空間ns1的表t1,列族為cf1。
刪除表
hbase> disable 'ns1:t1'
hbase> drop 'ns1:t1'
修改表名 HBase沒有rename命令,可以通過快照功能修改表名,詳見下述。
查看錶內容
hbase> scan 'ns1:t1' hbase> scan 'ns1:t1', {LIMIT=>5} # 檢視前5行資料
插入
hbase> put 'ns1:t1', 'r1', 'cf1:c1', 'value'
比如我們插入以下資料:
id | name | sex | age |
---|---|---|---|
1 | 孫悟空 | 男 | 18 |
2 | 安琪拉 | 女 | 19 |
3 | 狄仁傑 | 男 | 20 |
put 'gld:student','1','cf1:name','孫悟空' put 'gld:student','1','cf1:sex','男' put 'gld:student','1','cf1:age','18' put 'gld:student','2','cf1:name','安琪拉' put 'gld:student','2','cf1:sex','女' put 'gld:student','2','cf1:age','19' put 'gld:student','3','cf1:name','狄仁傑' put 'gld:student','3','cf1:sex','男' put 'gld:student','3','cf1:age','20'
hbase> scan 'gld:student'
ROW COLUMN+CELL
1 column=cf1:age, timestamp=1538030949261, value=18
1 column=cf1:name, timestamp=1538030949174, value=\xE5\xAD\x99\xE6\x82\x9F\xE7\xA9\xBA
1 column=cf1:sex, timestamp=1538030949219, value=\xE7\x94\xB7
2 column=cf1:age, timestamp=1538030949393, value=19
2 column=cf1:name, timestamp=1538030949314, value=\xE5\xAE\x89\xE7\x90\xAA\xE6\x8B\x89
2 column=cf1:sex, timestamp=1538030949350, value=\xE5\xA5\xB3
3 column=cf1:age, timestamp=1538030950752, value=20
3 column=cf1:name, timestamp=1538030949448, value=\xE7\x8B\x84\xE4\xBB\x81\xE6\x9D\xB0
3 column=cf1:sex, timestamp=1538030949487, value=\xE7\x94\xB7
3 row(s) in 0.0230 seconds
刪除
更新
檢視
hbase> get 'ns1:t1', 'r1'
快照
hbase> list_snapshots 'ns1'
SNAPSHOT TABLE + CREATION TIME
student_snapshot20180529 gld:student(Thu Mar 29 10:12:28 +0800 2018)
student_snapshot20180929 gld:student (Sat Sep 29 18:24:24 +0800 2018)
2 row(s) in 0.0280 seconds
=> ["student_snapshot20180529", "student_snapshot20180929"]
新建快照:
hbase> snapshot 'sourceTable', 'snapshotName'
從快照複製生成一個新表:
hbase> clone_snapshot 'snapshotName', 'gld:student'
分割槽
預分割槽
在建表時預分10個區:
create 'gld:student', 'cf1', {SPLITS => ['1', '2', '3', '4', '5', '6', '7', '8', '9']}
則每個分割槽的Start Key和End Key分別為:
(~, 1),[1, 2),[2, 3),[3, 4),[4, 5),[5, 6),[6, 7),[7, 8),[8, 9),[9, ~)
檢視HDFS是可以看到這個表下有10個資料夾(如果沒有預分割槽,則只有一個資料夾):
$ hdfs dfs -ls /hbase/data/gld/student
Found 12 items
drwxr-xr-x - hdfs supergroup 0 2018-10-23 14:23 /hbase/data/gld/student/.tabledesc
drwxr-xr-x - hdfs supergroup 0 2018-10-23 14:23 /hbase/data/gld/student/.tmp
drwxr-xr-x - hdfs supergroup 0 2018-10-23 14:23 /hbase/data/gld/student/0d94e23b2b2c0b22ec9024907f65b78e
drwxr-xr-x - hdfs supergroup 0 2018-10-23 14:23 /hbase/data/gld/student/1b3c094e1a1c9d9434222e96f600fc60
drwxr-xr-x - hdfs supergroup 0 2018-10-23 14:23 /hbase/data/gld/student/31a515208c2621c6c3b3a0489c938197
drwxr-xr-x - hdfs supergroup 0 2018-10-23 14:23 /hbase/data/gld/student/4410dd4230c711965c871aecd8c1ffe8
drwxr-xr-x - hdfs supergroup 0 2018-10-23 15:27 /hbase/data/gld/student/4730dab32d57159db3b9214f6b908f2d
drwxr-xr-x - hdfs supergroup 0 2018-10-23 14:25 /hbase/data/gld/student/4ff074ef3ca1a7bf56461c606c1920a2
drwxr-xr-x - hdfs supergroup 0 2018-10-23 14:23 /hbase/data/gld/student/621cf72507b7f8942af1dca15241ecf5
drwxr-xr-x - hdfs supergroup 0 2018-10-23 15:29 /hbase/data/gld/student/8432b86e8bc1367c5872d796462281d6
drwxr-xr-x - hdfs supergroup 0 2018-10-23 14:23 /hbase/data/gld/student/c68c759df2f78c1006bc07c30a8681d1
drwxr-xr-x - hdfs supergroup 0 2018-10-31 16:31 /hbase/data/gld/student/fb9fb89a569c27ba1ca626fc3ab44c09
手動分割槽
一開始沒有分割槽:
使用切分鍵’1’一分為二:
hbase> split 'tableName', 'splitKey'
hbase> split 'gld:student', '1'
繼續將第二個Region進行切分,以’2’作為切分鍵:
hbase> split 'regionName', 'splitKey'
hbase> split 'gld:student,1,1538223641947', '2'
合併分割槽
hbase> merge_region 'ENCODED_REGIONNAME', 'ENCODED_REGIONNAME'
hbase> merge_region 'eea56b5303ed9aca9aa2b4adc14798b8','e8324c9b047282a608588c8826484f5d'