HBase-Shell-数据结构-原理
第3章 HBase Shell操作
3.1 基本操作
1.進(jìn)入HBase客戶端命令行
[lxl@hadoop102 hbase]$ bin/hbase shell?
2.查看幫助命令
hbase(main):001:0> help?
3.查看當(dāng)前數(shù)據(jù)庫中有哪些表
hbase(main):001:0> list TABLE 0 row(s) in 0.2530 seconds=> []?
3.2 表的操作
1.創(chuàng)建表
hbase(main):003:0> create 'student','info' 0 row(s) in 1.3670 seconds=> Hbase::Table - student?
2.插入數(shù)據(jù)到表
hbase(main):003:0> put 'student','1001','info:sex','male' hbase(main):004:0> put 'student','1001','info:age','18' hbase(main):005:0> put 'student','1002','info:name','Janna' hbase(main):006:0> put 'student','1002','info:sex','female' hbase(main):007:0> put 'student','1002','info:age','20'?
3.掃描查看表數(shù)據(jù)
hbase(main):010:0> scan 'student' ROW COLUMN+CELL 1001 column=info:name, timestamp=1560293882038, value=xiannv 1001 column=info:sex, timestamp=1560293938473, value=female 1002 column=info:name, timestamp=1560293958558, value=xiantong 1002 column=info:sex, timestamp=1560293983958, value=male?
hbase(main):014:0> scan 'student',{STARTROW=>'1001',STOPROW=>'1003'} ROW COLUMN+CELL 1001 column=info:name, timestamp=1560293882038, value=xiannv 1001 column=info:sex, timestamp=1560293938473, value=female 1002 column=info:name, timestamp=1560293958558, value=xiantong 1002 column=info:sex, timestamp=1560293983958, value=male?
hbase(main):015:0> scan 'student',{STARTROW => '1001'} ROW COLUMN+CELL 1001 column=info:name, timestamp=1560293882038, value=xiannv 1001 column=info:sex, timestamp=1560293938473, value=female 1002 column=info:name, timestamp=1560293958558, value=xiantong 1002 column=info:sex, timestamp=1560293983958, value=male 1003 column=info:sex, timestamp=1560294341536, value=male?
4.查看表結(jié)構(gòu)
hbase(main):016:0> describe 'student' Table student is ENABLED student COLUMN FAMILIES DESCRIPTION {NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCA CHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'}?
5.更新指定字段的數(shù)據(jù)
hbase(main):012:0> put 'student','1001','info:name','Nick' hbase(main):013:0> put 'student','1001','info:age','100'?
6.查看“指定行”或“指定列族:列”的數(shù)據(jù)
hbase(main):011:0> get 'student','1001' COLUMN CELL info:name timestamp=1560293882038, value=xiannv info:sex timestamp=1560293938473, value=female?
hbase(main):012:0> get 'student','1001','info:name' COLUMN CELL info:name timestamp=1560293882038, value=xiannv?
7.統(tǒng)計表數(shù)據(jù)行數(shù)
hbase(main):021:0> count 'student'?
8.刪除數(shù)據(jù)
刪除某rowkey的全部數(shù)據(jù):
hbase(main):016:0> deleteall 'student','1001'?
刪除某rowkey的某一列數(shù)據(jù):
hbase(main):017:0> delete 'student','1002','info:sex'?
9.清空表數(shù)據(jù)
hbase(main):018:0> truncate 'student'?
提示:清空表的操作順序為先disable,然后再truncate。
10.刪除表
首先需要先讓該表為disable狀態(tài):
hbase(main):019:0> disable 'student'?
然后才能drop這個表:
hbase(main):020:0> drop 'student'?
提示:如果直接drop表,會報錯:ERROR: Table student is enabled. Disable it first.
11.變更表信息
將info列族中的數(shù)據(jù)存放3個版本:
hbase(main):022:0> alter 'student',{NAME=>'info',VERSIONS=>3} hbase(main):022:0> get 'student','1001',{COLUMN=>'info:name',VERSIONS=>3}?
操作:
hbase(main):018:0> alter 'student', {NAME=>'info',VERSIONS=>3} Updating all regions with the new schema... 1/1 regions updated. Done. 0 row(s) in 1.9710 secondshbase(main):019:0> describe 'student' Table student is ENABLED student COLUMN FAMILIES DESCRIPTION {NAME => 'info', BLOOMFILTER => 'ROW', VERSIONS => '3', IN_MEMORY => 'false', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', COMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCA CHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'} 1 row(s) in 0.0150 secondshbase(main):020:0> put 'student','1001','info:name','guangshen' 0 row(s) in 0.0080 secondshbase(main):021:0> get 'student','1001','info:name' COLUMN CELL info:name timestamp=1560295375184, value=guangshen 1 row(s) in 0.0170 secondshbase(main):022:0> get 'student','1001',{COLUMN=>'info:name',VERSIONS=>3} COLUMN CELL info:name timestamp=1560295375184, value=guangshen info:name timestamp=1560293882038, value=xiannv 1 row(s) in 0.0480 secondshbase(main):023:0> put 'student','1001','info:name','chunlei' 0 row(s) in 0.0080 secondshbase(main):024:0> get 'student','1001',{COLUMN=>'info:name',VERSIONS=>3} COLUMN CELL info:name timestamp=1560295467589, value=chunlei info:name timestamp=1560295375184, value=guangshen info:name timestamp=1560293882038, value=xiannv 1 row(s) in 0.0090 secondshbase(main):025:0> put 'student','1001','info:name','wangjun' 0 row(s) in 0.0080 secondshbase(main):026:0> get 'student','1001',{COLUMN=>'info:name',VERSIONS=>3} COLUMN CELL info:name timestamp=1560295506023, value=wangjun info:name timestamp=1560295467589, value=chunlei info:name timestamp=1560295375184, value=guangshen 1 row(s) in 0.0080 seconds?
第4章 HBase數(shù)據(jù)結(jié)構(gòu)
4.1 RowKey
與nosql數(shù)據(jù)庫們一樣,RowKey是用來檢索記錄的主鍵。訪問HBASE table中的行,只有三種方式:
1.通過單個RowKey訪問
2.通過RowKey的range(正則)
3.全表掃描
RowKey行鍵 (RowKey)可以是任意字符串(最大長度是64KB,實際應(yīng)用中長度一般為 10-100bytes),在HBASE內(nèi)部,
RowKey保存為字節(jié)數(shù)組。存儲時,數(shù)據(jù)按照RowKey的字典序(byte order)排序存儲。設(shè)計RowKey時,要充分排序存儲這個特性,將經(jīng)常一起讀取的行存儲放到一起。(位置相關(guān)性)
4.2 Column Family
列族:HBASE表中的每個列,都?xì)w屬于某個列族。列族是表的schema的一部 分(而列不是),必須在使用表之前定義。列名都以列族作為前綴。例如 courses:history,courses:math都屬于courses 這個列族。
4.3 Cell
由{rowkey, column?Family:columu, version} 唯一確定的單元。cell中的數(shù)據(jù)是沒有類型的,全部是字節(jié)碼形式存貯。
關(guān)鍵字:無類型、字節(jié)碼
4.4 Time Stamp
HBASE 中通過rowkey和columns確定的為一個存貯單元稱為cell。每個 cell都保存 著同一份數(shù)據(jù)的多個版本。版本通過時間戳來索引。時間戳的類型是 64位整型。時間戳可以由HBASE(在數(shù)據(jù)寫入時自動 )賦值,此時時間戳是精確到毫秒 的當(dāng)前系統(tǒng)時間。時間戳也可以由客戶顯式賦值。如果應(yīng)用程序要避免數(shù)據(jù)版 本沖突,就必須自己生成具有唯一性的時間戳。每個 cell中,不同版本的數(shù)據(jù)按照時間倒序排序,即最新的數(shù)據(jù)排在最前面。
為了避免數(shù)據(jù)存在過多版本造成的的管理 (包括存貯和索引)負(fù)擔(dān),HBASE提供 了兩種數(shù)據(jù)版本回收方式。一是保存數(shù)據(jù)的最后n個版本,二是保存最近一段 時間內(nèi)的版本(比如最近七天)。用戶可以針對每個列族進(jìn)行設(shè)置。
4.5 命名空間
命名空間的結(jié)構(gòu):
?
?
1) Table:表,所有的表都是命名空間的成員,即表必屬于某個命名空間,如果沒有指定,則在default默認(rèn)的命名空間中。
2) RegionServer?group:一個命名空間包含了默認(rèn)的RegionServer?Group。
3) Permission:權(quán)限,命名空間能夠讓我們來定義訪問控制列表ACL(Access Control List)。例如,創(chuàng)建表,讀取表,刪除,更新等等操作。
4) Quota:限額,可以強(qiáng)制一個命名空間可包含的region的數(shù)量。
?
查看命名空間:
hbase(main):003:0> list_namespace?
創(chuàng)建命名空間:
hbase(main):004:0> create_namespace 'bigdata'?
將表創(chuàng)建在自定義的命名空間中:
hbase(main):006:0> create 'bigdata:student','info'?
查看自定義命名空間中的表:?
hbase(main):007:0> list TABLE bigdata:student student 2 row(s) in 0.0080 seconds=> ["bigdata:student", "student"]?
刪除命名空間:(首先你得先刪除命名空間中的表,否則會報錯!)
hbase(main):008:0> drop_namespace 'bigdata'?
?
?
?
?
?
第5章 HBase原理
5.1?讀流程
HBase讀數(shù)據(jù)流程如圖3所示
?
圖3所示 HBase讀數(shù)據(jù)流程
?
1)Client先訪問zookeeper,從meta表讀取region的位置,然后讀取meta表中的數(shù)據(jù)。meta中又存儲了用戶表的region信息;
2)根據(jù)namespace、表名和rowkey在meta表中找到對應(yīng)的region信息;
3)找到這個region對應(yīng)的regionserver;
4)查找對應(yīng)的region;
5)先從MemStore找數(shù)據(jù),如果沒有,再到BlockCache里面讀;
6)BlockCache還沒有,再到StoreFile上讀(為了讀取的效率);
7)如果是從StoreFile里面讀取的數(shù)據(jù),不是直接返回給客戶端,而是先寫入BlockCache,再返回給客戶端。
?
在端口(http://hadoop102:16010/table.jsp?name=hbase:meta)中查看meta表所在的位置:
?
?
?
鏈接zookeeper,查看meta的存儲位置:
[lxl@hadoop102 zookeeper-3.4.10]$ bin/zkCli.sh?
[zk: localhost:2181(CONNECTED) 2] get /hbase/meta-region-server ?regionserver:16020??O?[PBUFhadoop102?}????- cZxid = 0x500000030 ctime = Wed Jun 12 05:10:12 CST 2019 mZxid = 0x500000030 mtime = Wed Jun 12 05:10:12 CST 2019 pZxid = 0x500000030 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 62 numChildren = 0?
?
?
在hbase中查看meta存儲的信息:
hbase(main):009:0> scan 'hbase:meta' ROW COLUMN+CELL bigdata:student,,1560298961661.c1d09e4ba7ff05adb8388 column=info:regioninfo, timestamp=1560298962242, value={ENCODED => c1d09e4ba7ff05adb8388529a48d992c, NAME => 'bigdata:student,,1560298961661.c1d09e4ba7ff05529a48d992c. adb8388529a48d992c.', STARTKEY => '', ENDKEY => ''} bigdata:student,,1560298961661.c1d09e4ba7ff05adb8388 column=info:seqnumDuringOpen, timestamp=1560298962242, value=\x00\x00\x00\x00\x00\x00\x00\x02 529a48d992c. bigdata:student,,1560298961661.c1d09e4ba7ff05adb8388 column=info:server, timestamp=1560298962242, value=hadoop102:16020 529a48d992c. bigdata:student,,1560298961661.c1d09e4ba7ff05adb8388 column=info:serverstartcode, timestamp=1560298962242, value=1560287383451 529a48d992c. hbase:namespace,,1560287388965.d8793997d53ec389eefa4 column=info:regioninfo, timestamp=1560288011539, value={ENCODED => d8793997d53ec389eefa4efbce8b00df, NAME => 'hbase:namespace,,1560287388965.d8793997d53ec3efbce8b00df. 89eefa4efbce8b00df.', STARTKEY => '', ENDKEY => ''} hbase:namespace,,1560287388965.d8793997d53ec389eefa4 column=info:seqnumDuringOpen, timestamp=1560288011539, value=\x00\x00\x00\x00\x00\x00\x00\x0B efbce8b00df. hbase:namespace,,1560287388965.d8793997d53ec389eefa4 column=info:server, timestamp=1560288011539, value=hadoop104:16020 efbce8b00df. hbase:namespace,,1560287388965.d8793997d53ec389eefa4 column=info:serverstartcode, timestamp=1560288011539, value=1560287762200 efbce8b00df. student,,1560298560640.cff5643f1df6fb44a31c72894f6cd column=info:regioninfo, timestamp=1560298584595, value={ENCODED => cff5643f1df6fb44a31c72894f6cdeb9, NAME => 'student,,1560298560640.cff5643f1df6fb44a31c72eb9. 894f6cdeb9.', STARTKEY => '', ENDKEY => ''} student,,1560298560640.cff5643f1df6fb44a31c72894f6cd column=info:seqnumDuringOpen, timestamp=1560298584595, value=\x00\x00\x00\x00\x00\x00\x00\x02 eb9. student,,1560298560640.cff5643f1df6fb44a31c72894f6cd column=info:server, timestamp=1560298584595, value=hadoop103:16020 eb9. student,,1560298560640.cff5643f1df6fb44a31c72894f6cd column=info:serverstartcode, timestamp=1560298584595, value=1560287489969?
在端口中查看表的存儲位置:
?
?
?
?
?
?
?
?
?
?讀數(shù)據(jù)微觀流程
?
?
5.2?寫流程
Hbase寫流程如圖2所示
?
圖2 ?HBase寫數(shù)據(jù)流程
?
1)Client向HregionServer發(fā)送寫請求;
2)HregionServer將數(shù)據(jù)寫到HLog(write ahead log)。為了數(shù)據(jù)的持久化和恢復(fù);
3)HregionServer將數(shù)據(jù)寫到內(nèi)存(MemStore);
4)反饋Client寫成功。
5.3?數(shù)據(jù)Flush過程
1)當(dāng)MemStore數(shù)據(jù)達(dá)到閾值(默認(rèn)是128M,老版本是64M),將數(shù)據(jù)刷到硬盤,將內(nèi)存中的數(shù)據(jù)刪除,同時刪除HLog中的歷史數(shù)據(jù);
2)并將數(shù)據(jù)存儲到HDFS中;
3)在HLog中做標(biāo)記點。
?
hbase-default.xml:(hbase默認(rèn)配置文件)
?
?
5.4?數(shù)據(jù)合并過程
1)當(dāng)數(shù)據(jù)塊達(dá)到4塊,Hmaster觸發(fā)合并操作,Region將數(shù)據(jù)塊加載到本地,進(jìn)行合并;
2)當(dāng)合并的數(shù)據(jù)超過256M,進(jìn)行拆分,將拆分后的Region分配給不同的HregionServer管理;
3)當(dāng)HregionServer宕機(jī)后,將HregionServer上的hlog拆分,然后分配給不同的HregionServer加載,修改.META.;
4)注意:HLog會同步到HDFS。
?
hbase-default.xml:(hbase默認(rèn)配置文件)
?
?
?
?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/LXL616/p/11007322.html
總結(jié)
以上是生活随笔為你收集整理的HBase-Shell-数据结构-原理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Sublime操作
- 下一篇: golang类型断言的使用(Type A