Getting Startedhtml
1. Introductionjava
2.Quick Start-Strandalone HBasenode
2.1 JDK版本選擇web
2.2 Get Started With HBaseshell
2.3 僞分佈式本地安裝apache
QuickStart會介紹如何安裝和運行一個單節點,standalone Hbase實例jvm
這節介紹了single-node standalone HBase的安裝。Standalone實例有全部的HBase守護運行在一個JVM。也會演示如何使用hbase shell來建立表,插入數據,執行put和scan操做,enable,disable表,關閉和啓動HBase。分佈式
HBase須要JDK,能夠查看須要的JDK版本: Javaoop
過程:下載,配置和以standalone mode啓動HBase
1.選擇一個Mirror而且下載
2.解壓下載文件,而後cd到目錄。
3.設置JAVA_HOME環境變量。能夠用傳統的方式設置,也能夠使用conf/hbase-env.sh集中管理。編輯這個文件,去掉JAVA_HOME的註釋,而且設置正確的路徑。JAVA_HOME變量要被設置到包含執行文件bin/java的目錄。不少最新的Linux操做系統提供一個機制好比/usr/bin/alternatives,用來切換java版本的執行文件。在這個例子中,你能夠設置JAVA_HOME到包含link的目錄,好比/usr。
JAVA_HOME=/usr
4.編輯conf/hbase-site.xml,hbase的配置文件。這個時候須要指定一些本地路徑。用來HBase的數據寫入和Zookeeper數據寫入。默認新的目錄建立在/tmp下。不少服務器會在重啓後會把/tmp的目錄清空。因此須要把數據放在其餘地方。下面的配置HBase的數據目錄被配置在testuser的home目錄下面。
Standalone Hbase配置例子:
<configuration>
<property>
<name>hbase.rootdir</name>
<value>file:///home/testuser/hbase</value>
</property>
<property>
<name>hbase.zookeeper.property.dataDir</name>
<value>/home/testuser/zookeeper</value>
</property>
<property>
<name>hbase.unsafe.stream.capability.enforce</name>
<value>false</value>
<description>
Controls whether HBase will check for stream capabilities (hflush/hsync).
Disable this if you intend to run on LocalFileSystem, denoted by a rootdir
with the 'file://' scheme, but be mindful of the NOTE below.
WARNING: Setting this to false blinds you to potential data loss and
inconsistent system state in the event of process and/or node failures. If
HBase is complaining of an inability to use hsync or hflush it's most
likely not a false positive.
</description>
</property>
</configuration>
不須要建立HBase的目錄,HBase會本身建立。若是建立了目錄HBase會進行合併。
若是已經存在的HDFS已經有了HBase的home,設置hbase.rootdir指向hdfs實例,好比:hdfs://namenode.example.org:8020/hbase。
1.bin/start-hbase.sh腳本是快速啓動hbase的方法。若是啓動正常成功啓動的信息就會被輸出。能夠使用jps查看HMaster是否啓動。在standalone Hbase守護進程都運行在一個jvm上。即 HMaster,一個HregionServer,Zookeeper守護進程。能夠到http://localhost:16010查看HBase Web UI。
過程:第一次使用HBase
1.鏈接到Hbase
使用bhase shell鏈接到hbase實例。
$ ./bin/hbase shell
hbase(main):001:0>
2.輸出hbase shell幫助
在命令行上輸入help回車,會顯示hbase shell的幫助。
3.建立表
使用 create命令建立新表
hbase(main):001:0> create 'test', 'cf'
0 row(s) in 0.4170 seconds
=> Hbase::Table - test
4.輸出表的信息
使用list命令確認已經存在的表
hbase(main):002:0> list 'test'
TABLE
test
1 row(s) in 0.0180 seconds
=> ["test"]
使用describe查看錶的詳細信息
hbase(main):003:0> describe 'test'
Table test is ENABLED
test
COLUMN FAMILIES DESCRIPTION
{NAME => 'cf', VERSIONS => '1', EVICT_BLOCKS_ON_CLOSE => 'false', NEW_VERSION_BEHAVIOR => 'false', KEEP_DELETED_CELLS => 'FALSE', CACHE_DATA_ON_WRITE =>
'false', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', MIN_VERSIONS => '0', REPLICATION_SCOPE => '0', BLOOMFILTER => 'ROW', CACHE_INDEX_ON_WRITE => 'f
alse', IN_MEMORY => 'false', CACHE_BLOOMS_ON_WRITE => 'false', PREFETCH_BLOCKS_ON_OPEN => 'false', COMPRESSION => 'NONE', BLOCKCACHE => 'true', BLOCKSIZE
=> '65536'}
1 row(s)
Took 0.9998 seconds
5.put數據到表
使用put命令插入數據。
hbase(main):003:0> put 'test', 'row1', 'cf:a', 'value1'
0 row(s) in 0.0850 seconds
hbase(main):004:0> put 'test', 'row2', 'cf:b', 'value2'
0 row(s) in 0.0110 seconds
hbase(main):005:0> put 'test', 'row3', 'cf:c', 'value3'
0 row(s) in 0.0100 seconds
這裏插入了3個值,在row1中插入,列cf:a,值value1。Hbase中的列由列簇好比 cf,冒號,後綴組成。
6.掃描表的全部數據
獲取hbase表的數據的方法就是scan。使用scan命令掃描表中的數據。能夠過濾。
hbase(main):006:0> scan 'test'
ROW COLUMN+CELL
row1 column=cf:a, timestamp=1421762485768, value=value1
row2 column=cf:b, timestamp=1421762491785, value=value2
row3 column=cf:c, timestamp=1421762496210, value=value3
3 row(s) in 0.0230 seconds
7.獲取一個行的數據
hbase(main):007:0> get 'test', 'row1'
COLUMN CELL
cf:a timestamp=1421762485768, value=value1
1 row(s) in 0.0350 seconds
8.disable表
若是想要刪除表或者修改表的設置,在其餘狀況下須要先disable表,使用disable命令。若是要從新enable使用enable命令。
hbase(main):008:0> disable 'test'
0 row(s) in 1.1820 seconds
hbase(main):009:0> enable 'test'
0 row(s) in 0.1770 seconds
9.刪除表
使用drop來刪除表
hbase(main):011:0> drop 'test'
0 row(s) in 0.1370 seconds
10.退出hbase shell
使用quit命令退出hbase shell,hbase的服務任然是運行的。
過程:關閉Hbase
1.和bin/start-hbase.sh同樣,使用bin/start-hbase.sh來關閉
$ ./bin/stop-hbase.sh
stopping hbase....................
$
2.執行命令後,過幾分鐘就會被關閉,能夠使用jps查看,保證HMaster和HregionServer進程被關閉。
已經安裝了standalone版本以後,能夠重現配置成僞分佈式,僞分佈式仍是在一個host下運行,可是hbase的守護進程是獨立的進程而不是在一個jvm下。默認的rootdir仍是在/tmp下。也能夠跳過直接存在本地文件系統下。
1.關閉已經在運行的hbase
2.配置hbase
編輯hbase-site.xml配置,首選設置hbase.cluster.distributed=true
<property>
<name></name> hbase.cluster.distributed
<value></value> true
</property>
而後修改hbase.rootdir從本地文件系統指向HDFS,使用hdfs:////好比hdfs的端口是8020.
<property>
<name></name> hbase.rootdir
<value></value> hdfs://localhost:8020/hbase
</property>
不須要在hdfs建立好目錄,hbase會建立。若是已經建立了目錄,那麼就會合並。
3.啓動hbase
使用bin/start-hbase.sh命令啓動hbase。若是系統配置的正確那麼jos命令會輸出HMaster和HRegionServer的進程。
4.檢查hbase在hdfs上的目錄
若是運行沒有問題,hbase會在hdfs建立目錄。按上面的配置會保存在/hbase下。能夠使用hadoop fs命令查看。
$ ./bin/hadoop fs -ls /hbase
Found 7 items
drwxr-xr-x - hbase users 0 2014-06-25 18:58 /hbase/.tmp
drwxr-xr-x - hbase users 0 2014-06-25 21:49 /hbase/WALs
drwxr-xr-x - hbase users 0 2014-06-25 18:48 /hbase/corrupt
drwxr-xr-x - hbase users 0 2014-06-25 18:58 /hbase/data
-rw-r--r-- 3 hbase users 42 2014-06-25 18:41 /hbase/hbase.id
-rw-r--r-- 3 hbase users 7 2014-06-25 18:41 /hbase/hbase.version
drwxr-xr-x - hbase users 0 2014-06-25 21:49 /hbase/oldWALs
5.建立一個表而且導入數據
能夠使用hbase shell建立一個表,而且導入數據,而後從表裏面讀取數據, shell exercises
6.啓動和關閉HBase的備份。
HMaster服務控制了hbase的集羣。能夠啓動9個HMaster備份服務,會有一個10個HMaster,包括primary。使用local-master-backup.sh來備份HMaster。對於每一個備份的master,想要啓動,須要分配端口。每一個HMaster有2個端口(默認是16000和16010)。Offset是2,因此備份要使用16002和16012.若是啓動3個服務那麼會使用16002/16012,16003/16013和16005/16015。
$ ./bin/local-master-backup.sh start 2 3 5
爲了kill backup master然不kill整個集羣,就須要找到它的PID,而後用kill -9命令去kill。PID保存在/tmp/hbase-USER-X-master.pid。如下命令是kill offset爲1的。
$ cat /tmp/hbase-testuser-1-master.pid |xargs kill -9
7.啓動和關閉額外的RegionServers
HRegionServer管理存儲文件中的數據。存儲文件由HMaster管理。一般每一個node運行一個HRegionServer。一個系統上運行多個HRegionServer用來測試僞分佈式模式。Local-regionservers.sh命令用來運行多個Regionservers。和local-master-backup.sh運行方式相似。每一個參數提供了一個端口偏移值。每一個RegionServer須要2個端口默認是16020,16030。由於HBase 1.1.0 以前HMaster不使用region server使用端口,保留了10個端口16020到16029,16030到16039用來給RegionServers使用。在運行local-regionservers.sh以前須要設置環境變量HBASE_RS_BASE_PORT 和HBASE_RS_INFO_BASE_PORT,好比16200和16300,那麼就能夠支持99個額外的RegionServers。如下命令是啓動4個額外的RegionServer,從16022/16032開始啓動(在base port上增長2)。
$ .bin/local-regionservers.sh start 2 3 4 5
位了手動關閉RegionServer,使用local-regionserver.sh來關閉。
$ .bin/local-regionservers.sh stop 3
8.關閉HBase
使用bin/stop-hbase.sh來關閉hbase。
實際狀況下,須要一個徹底的分佈式配置在實際的場景下,來測試hbase。在分佈式配置中,cluster包含不少個節點,每一個節點可能運行hbase的一個或多個守護。包括主的和備份的Master實例,多個Zookeeper節點和多個RegionServer節點。
Node Name |
Master |
ZooKeeper |
RegionServer |
node-a.example.com |
yes |
yes |
no |
node-b.example.com |
backup |
yes |
yes |
node-c.example.com |
no |
yes |
yes |
過程:配置無密碼SSH訪問
過程:準備node-a
在node-a上會運行primary master和Zookeeper進程,沒有RegionServer。根據以前的關閉node-a上的RegionServer。
1.編輯conf/regionserver,刪除localhost。增長node-b和node-c的ip地址或者主機名
若是仍是想要在node-a上運行RegionServer,也使用node-a的主機名。
2.配置hbase使用node-b做爲backup master。
建立一個文件在conf下叫backup-masters,而且增長node-b的hostname。
3.配置Zookeeper
在真是環境下Zookeeper配置不少。Zookeeper配置能夠看 zookeeper 這個配置可讓hbase在每一個集羣的node上,啓動和管理Zookeeper實例。
在node-a編輯conf/hbase-site.xml增長如下配置:
<property>
<name></name> hbase.zookeeper.quorum
<value></value> node-a.example.com,node-b.example.com,node-c.example.com
</property>
<property>
<name></name> hbase.zookeeper.property.dataDir
<value></value> /usr/local/zookeeper
</property>
4.在全部的配置中,使用node-a的主機名來替換localhost。
過程:準備node-b,node-c
Node-b會運行一個backup master和Zookeeper實例
1.下載並解壓hbase
2.把node-a的配置文件複製到node-b,node-c
每一個集羣中的node都有同樣的配置信息。
過程:啓動和測試集羣
1.保證hbase沒有在任何節點上運行。
若是忘記中止hbase,那麼就會報錯。使用jps檢查是否還在運行主要看HMaster,HregionServer,HQuorumPeer進程。若是存在就先kill。
2.啓動集羣
在node-a上,運行start-hbase.sh命令。Zookeeper會先啓動而後是master,而後是RegionServer最後是backup master。
3.驗證
在每一個node 上運行jps,查看進程是否運行。
Node-a上輸出
$ jps
20355 Jps
20071 HQuorumPeer
20137 HMaster
Node-b上
$ jps
15930 HRegionServer
16194 Jps
15838 HQuorumPeer
16010 HMaster
Node-c上
$ jps
13901 Jps
13639 HQuorumPeer
13737 HRegionServer
Zookeeper進程名
HQourumPeer是Zookeeper的實例有HBase控制。若是使用這種方式的Zookeeper每一個集羣node都會有一個節點。若是Zookeeper在hbase以外的。能夠使用QuorumPeer。更多的Zookeeper能夠看zookeeper
4.使用Web UI訪問
在hbase 0.98以後,http端口從master端口60010,RegionServer 60030修改到了16010和16030。
若是啓動正常,就能夠經過Web訪問Master
5.測試服務消失會發生什麼
當上面3節點的集羣配置完畢,還要看若是primary master或者RegionServer被kill以後會發生什麼。