Hbase的基本操作(CURD)
Hbase
僅供參考學(xué)習(xí)
一、Hbase是什么?
Hbase是一個分布式的
、面向列
的開源數(shù)據(jù)庫,且Hbase不同于一般的關(guān)系數(shù)據(jù)庫,它是一個適用于非結(jié)構(gòu)化數(shù)據(jù)
存儲的數(shù)據(jù)庫,且是基于列
的模式。它利用Hadoop HDFS作為其文件存儲系統(tǒng),利用Hadoop MapReduce來處理HBase中的海量數(shù)據(jù),利用Zookeeper作為協(xié)同服務(wù)
表的基本結(jié)構(gòu)
二、與傳統(tǒng)數(shù)據(jù)庫的差異
MySQL、Oracle | Hbase |
---|---|
行儲存 | 列儲存 |
適用于OLTP業(yè)務(wù) | 平衡了OLTP和OLAP |
強一致性 | 強一致性 |
支持二級索引 | 不支持二級索引 |
支持全文索引 | 不支持全文索引 |
強一致性
:例如MySQL的全同步復(fù)制模式,在該模式下用戶與MySQL交互,主庫和備庫同binlog時,主庫只有在收到備庫的成功響應(yīng)之后,才能夠向客戶端反饋提交成功。因此在用戶獲得響應(yīng)時,主庫和備庫的數(shù)據(jù)副本已經(jīng)達(dá)到了一致,所以后續(xù)的讀操作肯定不會出現(xiàn)問題,這種模式稱為強一致性。但是該模式具有很嚴(yán)重的弊端:
性能差: 主庫必須等到所有備庫均返回成功后,才能向用戶反饋提交成功。
可用性降低: 在全同步復(fù)制模式下,集群中的節(jié)點被串聯(lián)在一起,集群整體的可用性就降低了,且集群越大可用性問題越嚴(yán)重。
正因為這兩個問題,實現(xiàn)強一致性需要的代價太大,所以大部分產(chǎn)品選擇弱一致性
二級索引
:葉子節(jié)點中存儲主鍵值,每次查找數(shù)據(jù)時,根據(jù)索引找到葉子節(jié)點中的主鍵值,根據(jù)主鍵值再到聚簇索引中得到完整的一行記錄。全文索引
:通過關(guān)鍵字的匹配來進(jìn)行查詢過濾,基于相似度的查詢
OLTP和OLAP
三、基本操作
1.表操作
1.1 創(chuàng)建表
hbase> create ‘tablename’,‘column_family01’,‘column_family02’
create 'student','info','grade'
tablename: 表名
column_family01,column_family02:列族名
注意:
不用創(chuàng)建列,列名是后期插入數(shù)據(jù)時才定義的
1.2 刪除表
表創(chuàng)建成功后,默認(rèn)狀態(tài)是enable,即“使用中”的狀態(tài),刪除表之前需先設(shè)置表為“關(guān)閉中”。
disable 'student'
再使用關(guān)鍵字drop刪除表
drop 'student'
1.3 增加列族
hbase> alter ‘tablename’,‘column_famaily03’
alter 'student','sci'
1.4 刪除列族名
hbase> alter ‘ table_name ’, ‘delete’ => ‘ column family ’
hbase> alter 'table_name ',{NAME=>‘column family’,METHOD=>‘delete’}
alter 'student','delete'=>'sci'
alter 'student',{NAME=>'grade',METHOD=>'delete'}
1.5查看表結(jié)構(gòu)
hbase> describe ‘table_name’
describe 'student'
2.對數(shù)據(jù)的操作
2.1插入(更新)數(shù)據(jù)
put ‘table_name’,‘row_key1’,‘column_family:column’,‘value’
注:
row_key: 行鍵
column: 列名
只能一列一列的加,并不能一次性插入多條列數(shù)據(jù)(當(dāng)插入的列已經(jīng)有數(shù)據(jù)時就會覆蓋原有值)
put 'student','student_01','grade:math','20'
put 'student','student_01','grade:english','96'
put 'student','student_01','info:name','Jack'
put 'student','student_01','info:class','1101'
2.2 查看數(shù)據(jù)(get|scan)
get ‘table_name’ ,‘row_key’
scan ‘table_name’
get: 只查看某個行鍵的數(shù)據(jù)
scan:查看表的所有數(shù)據(jù)
get 'student','student_01'
scan 'student'
2.3 刪除一條行數(shù)據(jù)中的列值
hbase> deleteall ‘table_name’,‘row_key’,‘column_family:column’
hbase>delete ‘table_name’,‘row_key’,‘column_family:column’
deleteall'student','student_01','info:class'
2.4 刪除行數(shù)據(jù)(delete|deleteall)
deleteall ‘table_name’,‘row_key’
deleteall:刪除具體到哪一行中的某個列族下的某一列數(shù)據(jù)文章來源:http://www.zghlxwxcb.cn/news/detail-400442.html
deleteall 'student','student_01'
文章多處借鑒,代碼親測,若有侵權(quán),還忘告知:
文章來源地址http://www.zghlxwxcb.cn/news/detail-400442.html
到了這里,關(guān)于Hbase的基本操作(CURD)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!