Hbase入門(二)——安裝與配置

  • 2019 年 10 月 11 日
  • 筆記

本文講述如何安裝,部署,啟停HBase集群,如何通過命令行對Hbase進行基本操作。

並介紹Hbase的配置文件。

在安裝前需要將所有先決條件安裝完成。

一、先決條件

1、JDK

和Hadoop一樣,Hbase需要JDK1.6或者更高的版本,所以請安裝好JDK並配置環境變數。

Hbase版本與JDK版本

HBase Version

JDK 7

JDK 8

JDK 9 (Non-LTS)

JDK 10 (Non-LTS)

JDK 11

2.0+

HBASE-20264

HBASE-20264

HBASE-21110

1.2+

HBASE-20264

HBASE-20264

HBASE-21110

2、Zookeeper

zookeeper是Hbase集群的協調器,負責解決HMaster的單點問題,所以必須先安裝好一個zookeeper。

3、Hadoop

集群模式下,我們需要Hadoop環境

Hadoop 版本支援

  • T = 支援
  • F = 不支援
  • N = 未測試

HBase-1.2.x, HBase-1.3.x

HBase-1.4.x

HBase-2.0.x

HBase-2.1.x

Hadoop-2.4.x

T

F

F

F

Hadoop-2.5.x

T

F

F

F

Hadoop-2.6.0

F

F

F

F

Hadoop-2.6.1+

T

F

T

F

Hadoop-2.7.0

F

F

F

F

Hadoop-2.7.1+

T

T

T

T

Hadoop-2.8.[0-1]

F

F

F

F

Hadoop-2.8.2

N

N

N

N

Hadoop-2.8.3+

N

N

T

T

Hadoop-2.9.0

F

F

F

F

Hadoop-2.9.1+

N

N

N

N

Hadoop-3.0.[0-2]

F

F

F

F

Hadoop-3.0.3+

F

F

T

T

Hadoop-3.1.0

F

F

F

F

Hadoop-3.1.1+

F

F

T

T

二、安裝與部署

Hbase有兩種運行模式,單機模式和分散式模式。

1、單機模式

下載

首先在這裡下載Hbase的一個穩定版本,https://www.apache.org/dyn/closer.lua/hbase/

建議點擊頂部鏈接,進入 HBase Releases 點擊 stable 的文件夾,然後下載將以 tar.gz 結尾的二進位文件到本地。暫時不要下載以 src.tar.gz 結尾的文件。

解壓

進入到那個要解壓的目錄

$ tar xzvf hbase-1.3.5-bin.tar.gz  $ cd hbase-1.3.5/
JAVA_HOME

在啟動 HBase 之前,您需要設置JAVA_HOME環境變數。您可以通過作業系統的常用設置來設置變數,HBase 也提供了一個中心機制 conf/hbase-env.sh。編輯此文件,取消注釋以JAVA_HOME開頭的行,並將其設置為適合您的作業系統的路徑。

JAVA_HOME=/usr
hbase-site.xml

編輯 HBase 主配置文件 conf/hbase-site.xml.

您需要在本地文件系統上指定 HBase 和 ZooKeeper 數據存儲目錄.

<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>
啟停

bin/start-hbase.sh 為啟動 HBase

bin/start-hbase.sh為停止 HBase

可以使用 jps 確認 HMaster 和 HRegionServer 進程是否開啟關閉。

2、集群模式

2.1 偽分散式模式

偽分布模式意味著 HBase 仍然在單個主機上完全運行,但是每個 HBase 守護進程(HMaster,HRegionServer 和 ZooKeeper)作為一個單獨的進程運行.

使用集群模式前,要保證HDFS可以正常運行。

hbase-site.xml

編輯 HBase 主配置文件 conf/hbase-site.xml.

需要將分散式模式開啟 指定hdfs的URI

<property>    <name>hbase.cluster.distributed</name>    <value>true</value>  </property>    <property>    <name>hbase.rootdir</name>    <value>hdfs://localhost:8020/hbase</value>  </property>

使用 bin/start-hbase.sh 啟動 HBase. 如果您的系統配置正確,該jps命令應顯示 HMaster 和 HRegionServer 進程正在運行。

啟動和停止備份 HBase 主(HMaster)伺服器

你可以啟動最多 9 個備份 HMaster 伺服器,這個伺服器總共有 10 個 HMaster 計算主伺服器。

$ ./bin/local-master-backup.sh start 2 3 5

啟動和停止其他 RegionServers

$ .bin/local-regionservers.sh start 2 3 4 5

2.2 完全分散式

實際上,您需要一個完全分散式的配置來全面測試 HBase,並將其用於實際場景中。在分散式配置中,集群包含多個節點,每個節點運行一個或多個 HBase 守護進程。這些包括主要和備份主實例,多個 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免密和防火牆許可權,並將zookeeper配置好啟動。

將Hbase下載並解壓,將配置文件同步到各個機器上。

啟動集群
$ bin/start-hbase.sh  node-c.example.com: starting zookeeper, logging to /home/hbuser/hbase-0.98.3-hadoop2/bin/../logs/hbase-hbuser-zookeeper-node-c.example.com.out  node-a.example.com: starting zookeeper, logging to /home/hbuser/hbase-0.98.3-hadoop2/bin/../logs/hbase-hbuser-zookeeper-node-a.example.com.out  node-b.example.com: starting zookeeper, logging to /home/hbuser/hbase-0.98.3-hadoop2/bin/../logs/hbase-hbuser-zookeeper-node-b.example.com.out  starting master, logging to /home/hbuser/hbase-0.98.3-hadoop2/bin/../logs/hbase-hbuser-master-node-a.example.com.out  node-c.example.com: starting regionserver, logging to /home/hbuser/hbase-0.98.3-hadoop2/bin/../logs/hbase-hbuser-regionserver-node-c.example.com.out  node-b.example.com: starting regionserver, logging to /home/hbuser/hbase-0.98.3-hadoop2/bin/../logs/hbase-hbuser-regionserver-node-b.example.com.out  node-b.example.com: starting master, logging to /home/hbuser/hbase-0.98.3-hadoop2/bin/../logs/hbase-hbuser-master-nodeb.example.com.out

3、Web UI

在 HBase 0.98.x 以上, HBase Web UI 的埠從主節點的 60010 和 RegionServer 的 60030 變化為 16010 和 16030

在Hbase啟動以後可以通過頁面查看。

三、Hbase Shell

在 HBase 安裝目錄 bin/ 目錄下使用hbase shell命令連接正在運行的 HBase 實例。

$ ./bin/hbase shell  hbase(main):001:0>
預覽 HBase Shell 的幫助文本

輸入help並回車, 可以看到 HBase Shell 的基本資訊和一些示例命令.

創建表

使用 create創建一個表 必須指定一個表名和列族名

hbase(main):001:0> create 'test', 'cf'  0 row(s) in 0.4170 seconds    => Hbase::Table - test
表資訊

使用 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
插入數據

使用 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
掃描全部數據

從 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
獲取一條數據

使用 get 命令一次獲取一條數據

hbase(main):007:0> get 'test', 'row1'  COLUMN                                   CELL   cf:a                                    timestamp=1421762485768, value=value1  1 row(s) in 0.0350 seconds
禁用表

使用 disable 命令禁用表

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

使用 enable 命令啟用表

hbase(main):010:0> disable 'test'  0 row(s) in 1.1820 seconds
刪除表
hbase(main):011:0> drop 'test'  0 row(s) in 0.1370 seconds
退出 HBase Shell

使用quit命令退出命令行並從集群斷開連接。

四、配置文件詳解

Apache HBase 使用與 Apache Hadoop 相同的配置系統。所有配置文件都位於 conf/ 目錄中,需要保持群集中每個節點的同步。

backup-masters  默認情況下不存在。這是一個純文本文件,其中列出了主伺服器應在其上啟動備份主進程的主機,每行一台主機。  hadoop-metrics2-hbase.properties  用於連接 HBase Hadoop 的 Metrics2 框架。默認情況下只包含注釋出的示例。  hbase-env.cmd and hbase-env.sh  用於 Windows 和 Linux/Unix 環境的腳本,以設置 HBase 的工作環境,包括 Java、Java 選項和其他環境變數的位置。該文件包含許多注釋示例來提供指導。  hbase-policy.xml  RPC 伺服器使用默認策略配置文件對客戶端請求進行授權決策。僅在啟用 HBase安全模式下使用。  hbase-site.xml  主要的 HBase 配置文件。該文件指定覆蓋 HBase 的默認配置的配置選項。您可以在 docs/hbase-default.xml 中查看(但不要編輯)默認配置文件。您還可以在 HBase Web UI 的 HBase 配置選項卡中查看群集的整個有效配置(默認和覆蓋)。  log4j.properties  通過log4j進行 HBase 日誌記錄的配置文件。  regionservers  包含應該在 HBase 集群中運行 RegionServer 的主機列表的純文本文件。默認情況下,這個文件包含單個條目localhostt。它應該包含主機名或 IP 地址列表,每行一個,如果集群中的每個節點將在其localhost介面上運行 RegionServer 的話,則只應包含localhost

更多實時計算,Hbase,Flink,Kafka等相關技術博文,歡迎關注實時流式計算