H B A S E 基 本 操 作
阿新 • • 發佈:2018-12-11
瞭解:
-建立表: create '表名稱', '列名稱1','列名稱2','列名稱N' -新增記錄: put '表名稱', '行名稱', '列名稱:', '值' -檢視記錄: get '表名稱', '行名稱' -查看錶中的記錄總數: count '表名稱' -刪除記錄: delete '表名' ,'行名稱' , '列名稱' -刪除一張表: 先要遮蔽該表,才能對該表進行刪除,第一步 disable '表名稱' 第二步 drop '表名稱' -檢視所有記錄: scan "表名稱" -檢視某個表某個列中所有資料: scan "表名稱" , ['列名稱:']
1.namespace 名稱空間 相當於庫一個概念 庫
檢視名稱空間的幫助:
help 'namespace'
建立一個名稱空間;
create_namespace 'data'
描述檢視:
describe_namespace ''data'
檢視當前habse有哪些名稱空間:
list_namespace
檢視名稱空間的表:
list_namespace_tables ''data'
2.create建立表: 至少包含一個列族
help 'create' 建立帶版本的表 create 'ns1:t1', {NAME => 'f1', VERSIONS => 5} 建立兩個列族的表 create 'hadoop3_1709:stu','info','other' 在hadoop3_1708名稱空間下建立一個表stu表: create 'hadoop3_1709:stu','stu_info','stu_other' 或者 create 'stu','stu_info','stu_other' 檢視建立的表: list_namespace_tables 'hadoop3_1709' 查看錶描述 describe "hadoop3_1709:stu"
3. put新增資料:
help 'put' stu表列族對應列名稱: info name sex other address put 'hadoop3_1709:stu','20171122','info:name','laozhang' put 'hadoop3_1709:stu','20171122','info:sex','男' put 'hadoop3_1709:stu','20171122','other:addr','fangshan' put 'hadoop3_1709:stu','20171020','info:name','laozhang' put 'hadoop3_1709:stu','20171020','info:sex','nan' put 'hadoop3_1709:stu','20171020','other:address','fangshan' scan 'hadoop3_1709:stu' put 'hadoop3_1709:stu','20171020laowang','info:name','laowang' put 'hadoop3_1709:stu','20171020laowang','info:sex','nan' put 'hadoop3_1709:stu','20171020laowang','other:address','fangshan' put 'hadoop3_1709:stu','20171020laosong','info:name','laosong' put 'hadoop3_1709:stu','20171020laosong','info:sex','nan' put 'hadoop3_1709:stu','20171020laosong','other:address','fangshan' put 'hadoop3_1709:stu','20171020laoliu','info:name','laoliu' put 'hadoop3_1709:stu','20171020laoliu','info:sex','nv' put 'hadoop3_1709:stu','20171020laoliu','other:address','liangxiang' put 'hadoop3_1709:stu','20171021laofu','info:name','laofu' put 'hadoop3_1709:stu','20171021laofu','info:sex','nv' put 'hadoop3_1709:stu','20171021laofu','other:address','liangxiang' put 'hadoop3_1709:stu','20171021laoda','info:name','laoda' put 'hadoop3_1709:stu','20171021laoda','info:sex','nan' put 'hadoop3_1709:stu','20171021laoda','other:address','beicai' 新增資料: put 'hadoop3_1709:stu','rk2017092710001','stu_info:name','zhangyanpeng' put 'hadoop3_1709:stu','rk2017092710001','stu_info:sex','nan' put 'hadoop3_1709:stu','rk2017092710001','stu_other:address','beijing-fangshan' put 'hadoop3_1709:stu','rk2017092710009','stu_info:name','xiaowu' put 'hadoop3_1709:stu','rk2017092710009','stu_info:sex','nan' put 'hadoop3_1709:stu','rk2017092710009','stu_other:address','beijing-erzhi' put 'hadoop3_1709:stu','rk2017092710089','stu_info:name','ziaozhang' put 'hadoop3_1709:stu','rk2017092710089','stu_info:sex','nv' put 'hadoop3_1709:stu','rk2017092710089','stu_other:address','beijing-daxing'
4.檢視所有資料:scan
help 'scan'
檢視整張表的資料:
scan 'hadoop3_1709:stu'
區別於:scan 'stu'
檢視某一個列族的資料:
scan 'hadoop3_1709:stu', {COLUMNS => ['stu_info']}
scan 'hadoop3_1709:stu', {COLUMNS => ['stu_info','stu_other']}
scan 'hadoop3_1709:stu',{COLUMNS =>['info']}
scan 'hadoop3_1709:stu',{COLUMNS =>['info:name']}
scan 'hadoop3_1709:stu',{COLUMNS => ['info:sex','other']}
scan 'hadoop3_1709:stu','info' --error
5.get 檢視某個值
help 'get'
get 'hadoop3_1709:stu','20171020laowang'
get 'hadoop3_1709:stu','20171020laowang',{COLUMNS =>['info']}
get 'hadoop3_1709:stu','20171020laowang',{COLUMNS =>['info:name']}
get 'hadoop3_1709:stu', 'rk2017092710089', {COLUMN => 'stu_info'}
檢視一行的多個列族:
get 'hadoop3_1709:stu', 'rk2017092710089','stu_info','stu_other'
檢視某一個單元資料值:
get 'hadoop3_1709:stu', 'rk2017092710089','stu_info:name','stu_other:address'
6. count 統計:
統計表的行數:
count 'hadoop3_1709:stu'
7.delete 刪除表: ???歷史版本被刪除!???
delete 'ns1:t1', 'r1', 'c1', ts1
delete 'hadoop3_1709:stu','20171020laowang','other:address',1507731195828
8.disable 禁用表;
help 'disable'
Start disable of named table:
hbase> disable 't1'
hbase> disable 'ns1:t1'
disable 'hadoop3_1709:stu'
9.drop
help 'drop'
Drop the named table. Table must first be disabled:
hbase> drop 't1'
hbase> drop 'ns1:t1'
刪除表:
drop 'hadoop3_1709:stu'
查看錶是否啟用
is_enabled "hadoop3_1709:stu"
enable "hadoop3_1709:stu"
10.刪除名稱空間:
drop_namespace 'hadoop3_1709'
如果名稱空間有表,會報錯
11.查看錶結構
describe 'hadoop3_1709:stu'
desc 'hadoop3_1709:stu'
#修改表結構
修改表結構必須先disable
語法:alter 't1', {NAME => 'f1'}, {NAME => 'f2', METHOD => 'delete'}
例如:修改表test1的cf的TTL為180天
disable 'test1'
alter 'test1',{NAME=>'body',TTL=>'15552000'},{NAME=>'meta', TTL=>'15552000'}
enable 'test1'
語法:scan <table>, {COLUMNS => [ <family:column>,.... ], LIMIT => num}
另外,還可以新增STARTROW、TIMERANGE和FITLER等高階功能
scan 't1',{LIMIT=>5}
12.刪除表中的所有資料
語法: truncate <table>
其具體過程是:disable table -> drop table -> create table
例如:刪除表t1的所有資料
hbase(main)> truncate 't1'
13. HBase建表是可以新增列族,
alter 't1', {NAME => 'f1', VERSIONS => 5}
把表disable 後 alter,然後 enable
## 14.許可權管理
1)分配許可權
語法 : grant <user> <permissions> <table> <column family> <column qualifier> 引數後面用逗號分隔
許可權用五個字母表示: "RWXCA".
READ('R'), WRITE('W'), EXEC('X'), CREATE('C'), ADMIN('A')
例如,給使用者‘test'分配對錶t1有讀寫的許可權,
hbase(main)> grant 'test','RW','t1'
2)檢視許可權
語法:user_permission <table>
例如,查看錶t1的許可權列表
hbase(main)> user_permission 't1'
3)收回許可權
與分配許可權類似,語法:revoke <user> <table> <column family><column qualifier>
例如,收回test使用者在表t1上的許可權
hbase(main)> revoke 'test','t1'.