大數據學習系列之五 ----- Hive整合HBase圖文詳解

引言

在上一篇 大數據學習系列之四 ----- Hadoop+Hive環境搭建圖文詳解(單機) 和以前的大數據學習系列之二 ----- HBase環境搭建(單機) 中成功搭建了Hive和HBase的環境,並進行了相應的測試。本文主要講的是如何將Hive和HBase進行整合。java

Hive和HBase的通訊意圖

Hive與HBase整合的實現是利用二者自己對外的API接口互相通訊來完成的,其具體工做交由Hive的lib目錄中的hive-hbase-handler-*.jar工具類來實現,通訊原理以下圖所示。
這裏寫圖片描述node

Hive整合HBase後的使用場景:

(一)經過Hive把數據加載到HBase中,數據源能夠是文件也能夠是Hive中的表。
(二)經過整合,讓HBase支持JOIN、GROUP等SQL查詢語法。
(三)經過整合,不只可完成HBase的數據實時查詢,也可使用Hive查詢HBase中的數據完成複雜的數據分析。mysql

1、環境選擇

1,服務器選擇

本地虛擬機
操做系統:linux CentOS 7
Cpu:2核
內存:2G
硬盤:40Glinux

2,配置選擇

JDK:1.8 (jdk-8u144-linux-x64.tar.gz)
Hadoop:2.8.2 (hadoop-2.8.2.tar.gz)
Hive: 2.1 (apache-hive-2.1.1-bin.tar.gz)
HBase:1.6.2 (hbase-1.2.6-bin.tar.gz)sql

3,下載地址

官網地址
JDK:
http://www.oracle.com/technetwork/java/javase/downloads
Hadopp:
http://www.apache.org/dyn/closer.cgi/hadoop/common
Hive
http://mirror.bit.edu.cn/apache/hive/
HBase:
http://mirror.bit.edu.cn/apache/hbase/shell

百度雲盤
連接:https://pan.baidu.com/s/1jIemIDC 密碼:uycu數據庫

2、服務器的相關配置

在配置Hadoop+Hive+HBase以前,應該先作一下配置。
作這些配置爲了方便,使用root權限。apache

1,更改主機名

首先更改主機名,目的是爲了方便管理。
輸入:vim

hostname

查看本機的名稱
而後更改主機名爲master
輸入:瀏覽器

hostnamectl set-hostname master

注:主機名稱更改以後,要重啓(reboot)纔會生效。

2,作IP和主機名的映射

修改hosts文件,作關係映射
輸入

vim /etc/hosts

添加
主機的ip 和 主機名稱

192.168.238.128 master

3,關閉防火牆

關閉防火牆,方便訪問。
CentOS 7版本如下輸入:
關閉防火牆

service   iptables stop

CentOS 7 以上的版本輸入:

systemctl stop firewalld.service

4,時間設置

查看當前時間
輸入:

date

查看服務器時間是否一致,若不一致則更改
更改時間命令

date -s ‘MMDDhhmmYYYY.ss’

5,總體的環境配置

/etc/profile 的總體配置

#Java Config
export JAVA_HOME=/opt/java/jdk1.8
export JRE_HOME=/opt/java/jdk1.8/jre
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib

# Scala Config
export SCALA_HOME=/opt/scala/scala-2.12.2


# Spark Config
export  SPARK_HOME=/opt/spark/spark1.6-hadoop2.4-hive

# Zookeeper Config
export ZK_HOME=/opt/zookeeper/zookeeper3.4

# HBase Config
export HBASE_HOME=/opt/hbase/hbase1.2

# Hadoop Config 
export HADOOP_HOME=/opt/hadoop/hadoop2.8
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
export HADOOP_OPTS="-Djava.library.path=$HADOOP_HOME/lib"

# Hive Config
export HIVE_HOME=/opt/hive/hive2.1
export HIVE_CONF_DIR=${HIVE_HOME}/conf

export PATH=.:${JAVA_HOME}/bin:${SCALA_HOME}/bin:${SPARK_HOME}/bin:${HADOOP_HOME}/bin:${HADOOP_HOME}/sbin:${ZK_HOME}/bin:${HBASE_HOME}/bin:${HIVE_HOME}/bin:$PATH

這裏寫圖片描述

注:具體的配置以本身的爲準,沒有的不用配置。

3、Hadoop的環境配置

Hadoop的具體配置在大數據學習系列之一 ----- Hadoop環境搭建(單機) 中介紹得很詳細了。因此本文就大致介紹一下。
注:具體配置以本身的爲準。

1,環境變量設置

編輯 /etc/profile 文件 :

vim /etc/profile

配置文件:

export HADOOP_HOME=/opt/hadoop/hadoop2.8
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
export HADOOP_OPTS="-Djava.library.path=$HADOOP_HOME/lib"
export PATH=.:${JAVA_HOME}/bin:${HADOOP_HOME}/bin:$PATH

2,配置文件更改

先切換到 /home/hadoop/hadoop2.8/etc/hadoop/ 目錄下

3.2.1 修改 core-site.xml

輸入:

vim core-site.xml

添加:

<configuration>
<property>
        <name>hadoop.tmp.dir</name>
        <value>/root/hadoop/tmp</value>
        <description>Abase for other temporary directories.</description>
   </property>
   <property>
        <name>fs.default.name</name>
        <value>hdfs://master:9000</value>
   </property>
</configuration>

3.2.2修改 hadoop-env.sh

輸入:

vim hadoop-env.sh

\({JAVA_HOME} 修改成本身的JDK路徑 ``` export JAVA_HOME=\){JAVA_HOME}

修改成:

export JAVA_HOME=/home/java/jdk1.8

#### 3.2.3修改 hdfs-site.xml

輸入:

vim hdfs-site.xml

在<configuration>添加:


dfs.name.dir
/root/hadoop/dfs/name
Path on the local filesystem where theNameNode stores the namespace and transactions logs persistently.


dfs.data.dir
/root/hadoop/dfs/data
Comma separated list of paths on the localfilesystem of a DataNode where it should store its blocks.


dfs.replication
2


dfs.permissions
false
need not permissions

#### 3.2.4 修改mapred-site.xml

若是沒有 mapred-site.xml 該文件,就複製mapred-site.xml.template文件並重命名爲mapred-site.xml。 
輸入:

vim mapred-site.xml

修改這個新建的mapred-site.xml文件,在<configuration>節點內加入配置:


mapred.job.tracker
master:9001


mapred.local.dir
/root/hadoop/var


mapreduce.framework.name
yarn

### 3,Hadoop啓動
啓動以前須要先格式化
切換到/home/hadoop/hadoop2.8/bin目錄下
輸入:

./hadoop namenode -format

格式化成功後,再切換到/home/hadoop/hadoop2.8/sbin目錄下
啓動hdfs和yarn
輸入:

start-dfs.sh
start-yarn.sh

啓動成功後,輸入jsp查看是否啓動成功
在瀏覽器輸入 ip+8088 和ip +50070 界面查看是否能訪問
能正確訪問則啓動成功
 
## 4、Hive的環境配置
Hive環境的具體配置在個人這篇[大數據學習系列之四 ----- Hadoop+Hive環境搭建圖文詳解(單機)](http://www.panchengming.com/2017/12/16/pancm61/) 以及介紹得很詳細了。本篇就大概介紹下。

### 修改hive-site.xml
切換到 /opt/hive/hive2.1/conf 目錄下
將hive-default.xml.template 拷貝一份,並重命名爲hive-site.xml
而後編輯hive-site.xml文件

cp hive-default.xml.template hive-site.xml
vim hive-site.xml

編輯hive-site.xml文件,在 <configuration> 中添加:


hive.metastore.warehouse.dir
/root/hive/warehouse


hive.exec.scratchdir
/root/hive



hive.metastore.uris


javax.jdo.option.ConnectionURL
jdbc:mysql://master:3306/hive?createDatabaseIfNotExist=true



javax.jdo.option.ConnectionDriverName
com.mysql.jdbc.Driver



javax.jdo.option.ConnectionUserName
root



javax.jdo.option.ConnectionPassword
123456


hive.metastore.schema.verification
false


而後將配置文件中全部的

${system:java.io.tmpdir}

更改成 /opt/hive/tmp  (若是沒有該文件則建立),
  並將此文件夾賦予讀寫權限,將

${system:user.name}
```
更改成 root

例如:
更改以前的:
這裏寫圖片描述
更改以後:
這裏寫圖片描述

配置圖:
這裏寫圖片描述

注: 因爲hive-site.xml 文件中的配置過多,能夠經過FTP將它下載下來進行編輯。也能夠直接配置本身所需的,其餘的能夠刪除。 MySQL的鏈接地址中的master是主機的別名,能夠換成ip。

修改 hive-env.sh

修改hive-env.sh 文件,沒有就複製 hive-env.sh.template ,並重命名爲hive-env.sh

在這個配置文件中添加

export  HADOOP_HOME=/opt/hadoop/hadoop2.8
export  HIVE_CONF_DIR=/opt/hive/hive2.1/conf
export  HIVE_AUX_JARS_PATH=/opt/hive/hive2.1/lib

添加 數據驅動包

因爲Hive 默認自帶的數據庫是使用mysql,因此這塊就是用mysql
將mysql 的驅動包 上傳到 /opt/hive/hive2.1/lib

5、HBase的環境配置

HBase環境的具體配置在個人這篇大數據學習系列之二 ----- HBase環境搭建(單機) 以及介紹得很詳細了。本篇就大概介紹下。

修改 hbase-env.sh

編輯 hbase-env.sh 文件,添加如下配置

export JAVA_HOME=/opt/java/jdk1.8
export HADOOP_HOME=/opt/hadoop/hadoop2.8
export HBASE_HOME=/opt/hbase/hbase1.2
export HBASE_CLASSPATH=/opt/hadoop/hadoop2.8/etc/hadoop
export HBASE_PID_DIR=/root/hbase/pids
export HBASE_MANAGES_ZK=false

說明:配置的路徑以本身的爲準。HBASE_MANAGES_ZK=false 是不啓用HBase自帶的Zookeeper集羣。

修改 hbase-site.xml

編輯hbase-site.xml 文件,在 添加以下配置

<!-- 存儲目錄 -->
<property>  
 <name>hbase.rootdir</name>  
 <value>hdfs://test1:9000/hbase</value>  
 <description>The directory shared byregion servers.</description>  
</property>  
<!-- hbase的端口 -->
<property>  
 <name>hbase.zookeeper.property.clientPort</name>  
 <value>2181</value>  
 <description>Property from ZooKeeper'sconfig zoo.cfg. The port at which the clients will connect.  
 </description>  
</property>  
<!--  超時時間 -->
<property>  
 <name>zookeeper.session.timeout</name>  
 <value>120000</value>  
</property>  
<!--  zookeeper 集羣配置。若是是集羣,則添加其它的主機地址 -->
<property>  
 <name>hbase.zookeeper.quorum</name>  
 <value>test1</value>  
</property>  
<property>  
 <name>hbase.tmp.dir</name>  
 <value>/root/hbase/tmp</value>  
</property>  
<!-- false是單機模式,true是分佈式模式  -->
<property>  
 <name>hbase.cluster.distributed</name>  
 <value>false</value>  
</property>

說明:hbase.rootdir:這個目錄是region server的共享目錄,用來持久化Hbase 。hbase.cluster.distributed :Hbase的運行模式。false是單機模式,true是分佈式模式。若爲false,Hbase和Zookeeper會運行在同一個JVM裏面。

6、Hive整合HBase的環境配置以及測試

1,環境配置

由於Hive與HBase整合的實現是利用二者自己對外的API接口互相通訊來完成的,其具體工做交由Hive的lib目錄中的hive-hbase-handler-.jar工具類來實現。因此只須要將hive的 hive-hbase-handler-.jar 複製到hbase/lib中就能夠了。
切換到hive/lib目錄下
輸入:

cp hive-hbase-handler-*.jar /opt/hbase/hbase1.2/lib

這裏寫圖片描述
注: 若是在hive整合hbase中,出現版本之類的問題,那麼以hbase的版本爲主,將hbase中的jar包覆蓋hive的jar包。

2,hive和hbase測試

在進行測試的時候,確保hadoop、hbase、hive環境已經成功搭建好,而且都成功啓動了。
打開xshell的兩個命令窗口
一個進入hive,一個進入hbase

6.2.1在hive中建立映射hbase的表

在hive中建立一個映射hbase的表,爲了方便,設置兩邊的表名都爲t_student,存儲的表也是這個。
在hive中輸入:

create table t_student(id int,name string) stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties("hbase.columns.mapping"=":key,st1:name") tblproperties("hbase.table.name"="t_student","hbase.mapred.output.outputtable" = "t_student");

說明:第一個t_student 是hive表中的名稱,第二個t_student是定義在hbase的table名稱 ,第三個t_student 是存儲數據表的名稱("hbase.mapred.output.outputtable" = "t_student"這個能夠不要,表數據就存儲在第二個表中了) 。
(id int,name string) 這個是hive表結構。若是要增長字段,就以這種格式增長。若是要增長字段的註釋,那麼在字段後面添加comment ‘你要描述的’。
例如:
create table t_student(id int comment ‘StudentId’,name string comment ‘StudentName’)
org.apache.hadoop.hive.hbase.HBaseStorageHandler 這個是指定的存儲器。
hbase.columns.mapping 是定義在hbase的列族。
例如:st1就是列族,name就是列。在hive中建立表t_student,這個表包括兩個字段(int型的id和string型的name)。 映射爲hbase中的表t_student,key對應hbase的rowkey,value對應hbase的st1:name列。

表成功建立以後
在hive、hbase分別中查看錶和表結構
hive中輸入

show tables;
describe t_student;

hbase輸入:

list
describe ‘t_student’

這裏寫圖片描述

這裏寫圖片描述
能夠看到表已經成功的建立了

6.2.2數據同步測試

進入hbase以後
在t_student中添加兩條數據 而後查詢該表

put 't_student','1001','st1:name','zhangsan'
put 't_student','1002','st1:name','lisi'
scan 't_student'

這裏寫圖片描述

而後切換到hive中
查詢該表
輸入:

select * from t_student;

這裏寫圖片描述

而後在hive中刪除該表
注:由於作測試要看結果,因此將表刪除了。若是同窗們要作測試的話,是沒有必要刪除該表的,由於在後面還會使用該表。

而後查看hive和hbase中的表是否刪除了
輸入:

drop table t_student;

這裏寫圖片描述

這裏寫圖片描述
經過這些能夠看到hive和hbase之間的數據成功同步!

6.2.3關聯查詢測試

hive外部表測試

先在hbase中建一張t_student_info表,添加兩個列族
而後查看錶結構
輸入:

create 't_student_info','st1','st2'
describe 't_student_info'

這裏寫圖片描述

而後在hive中建立外部表
說明:建立外部表要使用EXTERNAL 關鍵字
輸入:

create external table t_student_info(id int,age int,sex string) stored by 'org.apache.hadoop.hive.hbase.HBaseStorageHandler' with serdeproperties("hbase.columns.mapping"=":key,st1:age,st2:sex") tblproperties("hbase.table.name"="t_student_info");

這裏寫圖片描述

而後在t_student_info 中添加數據

put 't_student_info','1001','st2:sex','man'
put 't_student_info','1001','st1:age','20'
put 't_student_info','1002','st1:age','18'
put 't_student_info','1002','st2:sex','woman'

這裏寫圖片描述

而後在hive中查詢該表
輸入:

select * from t_student_info;

這裏寫圖片描述

查詢到數據以後,而後將t_student 和t_student_info進行關聯查詢。
輸入:

select * from t_student t join t_student ti where t.id=ti.id ;

這裏寫圖片描述
說明:經過關聯查詢,能夠得出表之間是能夠關聯查詢的。可是明顯看到hive 使用默認的mapreduce 做爲引擎是多麼的慢。。。

其餘說明:
因爲本身的虛擬機配置實在太渣,即便調大reduce內存,限制每一個reduce處理的數據量,仍是不行,最後沒辦法使用公司的測試服務進行測試。
在查詢一張表的時候,hive沒有使用引擎,所以相對比較快,若是是進行了關聯查詢之類的,就會使用引擎,因爲hive默認的引擎是mr,因此會很慢,也和配置有必定關係,hive2.x之後官方就不建議使用mr了。

本文到此結束,謝謝閱讀!
版權聲明:
做者:虛無境
博客園出處:http://www.cnblogs.com/xuwujing
CSDN出處:http://blog.csdn.net/qazwsxpcm    
我的博客出處:http://www.panchengming.com 原創不易,轉載請標明出處,謝謝!

相關文章
相關標籤/搜索