好程序員大數據分享:Hbase精解

好程序員大數據分享:Hbase精解,爲何有hbase?hbase是什麼?Hbase的架構。java

 

  1、 爲何有hbase?mysql

 

  數據量愈來愈大,傳統的關係型數據庫不能知足存儲和查詢的需求。而hive雖然能知足存儲的要求,可是hive不能知足非結構化、半結構化數據的存儲和查詢。linux

 

  2、hbase是什麼?程序員

 

hbase是一個開源的、分佈式的、多版本的、可擴展的非關係型數據庫。hbase是bigtable的開源java版本,創建在hdfs之上,提供高可靠性的、高性能、列式存儲、可伸縮、實時讀寫的nosql數據庫系統。適用的場景如:須要對海量非結構化的數據進行存儲。sql

 

  須要隨機近實時的讀寫管理數據。shell

 

  3、hbase的架構數據庫

 

client\zookeeper\hmaster\api

 

hregionserver\hlog\hregion\memstore\storefile\hfile服務器

 

client:hbase的客戶端,包含訪問hbase的接口(linux shell 、java api)架構

 

client維護一些cache來加快訪問hbase的速度,好比region的位置信息。

 

zookeeper:監控hmaster的狀態,保證有些僅有一個active的hmaster,達到高可用。存儲全部region的尋址入口,--root表在那臺服務器上。實時監控hregionserver的狀態,將regionserver的上下線信息實時通知給hmaster。存儲hbase的全部表的信息(hbase的元數據)

 

hmaster:(hbase的老大)爲regionserver分配region(新建表等)。負責regionserver的負載均衡。負責region的從新分配(hregionserver異常、hregion裂變)。hdfs上的垃圾文件回收。處理schema的更新請求。

 

hregionserver:(hbase的小弟)hregionserver維護master分配給他的region(管理本機器上region)。處理client對這些region的IO請求,並和hdfs進行交互

 

region server負責切分在運行過程當中變大的region。

 

hlog:對hbase的操做進行記錄,使用WAL寫數據,優先寫入log,而後再寫入memstore,以防數據丟死能夠進行回滾。

 

hregion:hbase中分佈式存儲和負載均衡的最小單元,表或者表的一部分。

 

store:至關於一個列簇。

 

memstore:128M內存緩衝區,用於將數據批量刷新到hdfs上。

 

hstorefile(hfile):hbase中的數據是以hfile的形式存儲在hdfs上。

 

  各組件間的數量關係:

 

hmaster:hregionserver=1:n

 

hregionserver:hregion=1:n

 

hregionserver:hlog=1:1

 

hregion:hstore=1:n

 

store:memstore=1:1

 

store:storefile=1:n

 

storefile:hfile=1:1

 

hbase關鍵字詞:

 

rowkey:行鍵,和mysql的主鍵是同樣的,不容許重複,有順序。

 

columnfamily:列簇(列的集合)。

 

column:列。

 

timestamp:時間戳,默認顯示最新的時間戳。

 

version:版本號。

 

cell:單元格。

 

  4、hbase和hadoop的關係

 

hbase是基於hadoop:hbase的存儲依賴於hdfs。具體說hbase的特色:

 

  模式:無模式。

 

  數據類型:單一 byte[]。

 

  多版本:每一個值均可以有多個版本。

 

  列式存儲:一個列簇存儲到一個目錄。

 

  稀疏存儲:若是key-value爲null,則將不佔用存儲空間。

 

  再說hbase的安裝:

 

一、standalone模式

 

1)解壓並配置環境變量

 

tar -zxvf hbase-1.2.1-bin.tar.gz -C /usr/local

 

cd /usr/local

 

vi /etc/profile

 

source /etc/profile

 

2)測試hbase的安裝

 

hbase version

 

  配置hbase的配置文件

 

vi conf/hbase-env.sh

 

JAVA_HOME

 

  注意:

 

# Configure PermSize. Only needed in JDK7. You can safely remove it for JDK8+

 

export HBASE_MASTER_OPTS="$HBASE_MASTER_OPTS -XX:PermSize=128m -XX:MaxPermSize=128m"。

 

export HBASE_REGIONSERVER_OPTS="$HBASE_REGIONSERVER_OPTS -XX:PermSize=128m -XX:MaxPermSize=128m"。

 

vi hbase-site.xml

 

hbase.rootdir

 

file:///usr/local/hbasedata

 

hbase.zookeeper.property.dataDir

 

/usr/local/zookeeperdata

 

  啓動hbase服務:

 

bin/start-hbase/sh

 

  啓動客戶端:

 

bin/hbase shell

 

二、僞分佈式

 

三、全分佈式

 

  解壓並配置環境變量

 

  配置hbase的配置文件

 

vi conf/hbase-env.sh

 

export HBASE_MANAGES_ZK=false

 

vi regionservers

 

vi backup-masters

 

vi hbase-site.xml

 

hbase.cluster.distributed

 

true

 

hbase.rootdir

 

hdfs://qianfeng/hbase

 

hbase.zookeeper.property.dataDir

 

/usr/local/zookeeperdata

 

hbase.zookeeper.quorum

 

hadoop05:2181,hadoop06:2181,hadoop07:2181

 

  注意:

 

  若是hdfs是高可用的,要講hadoop下的core-site.xml和hdfs-site.xml copy到hbase/conf目錄下。

 

  分發:

 

scp -r hbase-1.2.1 root@hadoop06:$PWD

 

scp -r hbase-1.2.1 root@hadoop07:$PWD

 

  啓動:

 

1)啓動zk

 

2)啓動hdfs

 

3)啓動hbase

 

hbase集羣的時間必須同步。

 

hmaster:16010

 

hregionserver:16030

 

hbase的shell操做

 

help

 

help "COMMAND"

 

help "COMMAND_GROUP"

 

  列舉出當前namespace下的全部表

 

list

 

  建立表:

 

create 'test','f1', 'f2'

 

namespace:

 

hbase沒有庫的概念,可是有名稱空間或者組的概念,namespace至關於(庫)

 

hbase默認有兩個組:

 

default:

 

hbase:

 

  列舉出全部的namespcae:

 

list_namespace

 

list_namespace_tables 'hbase'

 

create_namespace 'ns1'

 

describe_namespace 'ns1'

 

alter_namespace 'ns1', {METHOD => 'set', 'NAME' => 'gjz1'}

 

alter_namespace 'ns1', {METHOD => 'unset', NAME => 'NAME'}

 

drop_namespace 'ns1' ###只能刪除一個空的namespace

 

DDL:

 

Group name: ddl

 

Commands: alter, alter_async, alter_status, create, describe, disable, disable_all, drop, drop_all, enable, enable_all, exists, get_table, is_disabled, is_enabled, list, locate_region, show_filters

 

  建立表:

 

create 'test','f1', 'f2'

 

create 'ns1:t_userinfo',{NAME=>'base_info',BLOOMFILTER => 'ROWCOL',VERSIONS => '3'}

 

create 'ns1:t1', 'f1', SPLITS => ['10', '20', '30', '40'] --事先分配好region所管轄的rowkey的範圍。

 

  修改表:(有則更新,無則新增)

 

alter 'ns1:t_userinfo',{NAME=>'extra_info',BLOOMFILTER => 'ROW',VERSIONS => '2'}

 

alter 'ns1:t_userinfo',{NAME=>'extra_info',BLOOMFILTER => 'ROWCOL',VERSIONS => '5'}

 

  刪除列簇:

 

alter 'ns1:t_userinfo', NAME => 'extra_info', METHOD => 'delete'

 

alter 'ns1:t_userinfo', 'delete' => 'base_info'

 

  刪除表:(先要禁用表)

 

disable 'ns1:t1'

 

drop 'ns1:t1'

 

DML:

 

Group name: dml

 

Commands: append, count, delete, deleteall, get, get_counter, get_splits, incr, put, scan, truncate, truncate_preserve

 

  插入數據:(不能一次性插入多列)

 

put 'ns1:test','u00001','cf1:name','zhangsan'

 

put 'ns1:t_userinfo','rk00001','base_info:name','gaoyuanyuan'

 

put 'ns1:t_userinfo','rk00001','extra_info:pic','picture'

 

  更新數據:

 

put 'ns1:t_userinfo','rk00001','base_info:name','zhouzhiruo'

 

put 'ns1:t_userinfo','rk00002','base_info:name','zhaoming'

 

  表掃描(scan)

 

scan 'ns1:t_userinfo'

 

scan 'ns1:t_userinfo',{COLUMNS => ['base_info:name','base_info:age']}

 

  設置查詢條件:(包頭不包尾)

 

scan 'ns1:t_userinfo',{COLUMNS => ['base_info:name','base_info:age'],STARTROW=>'rk000012',LIMIT=>2}

 

scan 'ns1:t_userinfo',{COLUMNS => ['base_info:name','base_info:age'],STARTROW=>'rk000012',ENDROW=>'rk00002',LIMIT=>2}

 

  查詢數據:(GET)

 

get 'ns1:t_userinfo','rk00001'

 

get 'ns1:t_userinfo','rk00001',{TIMERANGE=>[1534136591897,1534136667747]}

 

get 'ns1:t_userinfo','rk00001',{COLUMN=>['base_info:name','base_info:age'],VERSIONS =>4}

 

get 'ns1:t_userinfo','rk00001',{TIMESTAMP=>1534136580800}

 

  刪除數據:(DELETE)

 

delete 'ns1:t_userinfo','rk00002','base_info:age'

 

'ns1:t_userinfo','rk00001',{TIMERANGE=>[1534138686498,1534138738862]}

 

  刪除指定的版本:(往上刪除版本)

 

delete 'ns1:t_userinfo','rk00001','base_info:name',TIMESTAMP=>1534138686498

 

  表判斷:

 

exists 'ns1:t_userinfo'

 

disable 'ns1:t_userinfo'

 

enable 'ns1:t_userinfo'

 

desc 'ns1:t_userinfo'

 

  統計表:(統計效率較差,不建議使用)

 

count 'ns1:t_userinfo'

 

  清空表:

 

truncate 'ns1:test'

 

 

  學習大數據開發,內容包含Linux&&Hadoop生態體系、大數據計算框架體系、雲計算體系、機器學習&&深度學習。根據好程序員提供的大數據學習路線圖可讓你對學習大數據須要掌握的知識有個清晰的瞭解,並快速入門大數據開發。

相關文章
相關標籤/搜索