原文鏈接:https://juejin.im/post/59c3f8f75188255be81f91d9#heading-17
Apache Hive-2.3.0 快速搭建與使用
Hive 簡介
Hive 是一個基於 hadoop 的開源數據倉庫工具,用於存儲和處理海量結構化數據。它把海量數據存儲於 hadoop 文件系統,而不是數據庫,但提供了一套類數據庫的數據存儲和處理機制,並采用 HQL (類 SQL )語言對這些數據進行自動化管理和處理。我們可以把 Hive 中海量結構化數據看成一個個的表,而實際上這些數據是分布式存儲在 HDFS 中的。 Hive 經過對語句進行解析和轉換,最終生成一系列基於 hadoop 的 map/reduce 任務,通過執行這些任務完成數據處理。
Hive 誕生於 facebook 的日志分析需求,面對海量的結構化數據, Hive 以較低的成本完成了以往需要大規模數據庫才能完成的任務,並且學習門檻相對較低,應用開發靈活而高效。
Hive 自 2009.4.29 發布第一個官方穩定版 0.3.0 至今,不過一年的時間,正在慢慢完善,網上能找到的相關資料相當少,尤其中文資料更少,本文結合業務對 Hive 的應用做了一些探索,並把這些經驗做一個總結,所謂前車之鑒,希望讀者能少走一些彎路。
准備工作
環境
JDK:1.8
Hadoop Release:2.7.4
centos:7.3
node1(master) 主機: 192.168.252.121
node2(slave1) 從機: 192.168.252.122
node3(slave2) 從機: 192.168.252.123
node4(mysql) 從機: 192.168.252.124
復制代碼
依賴環境
安裝**Apache Hive**前提是要先安裝hadoop集群,並且hive只需要在hadoop的namenode節點集群里安裝即可(需要在有的namenode上安裝),可以不在datanode節點的機器上安裝。還需要說明的是,雖然修改配置文件並不需要把hadoop運行起來,但是本文中用到了hadoop的hdfs命令,在執行這些命令時你必須確保hadoop是正在運行着的,而且啟動hive的前提也需要hadoop在正常運行着,所以建議先把hadoop集群啟動起來。
安裝**MySQL** 用於存儲 Hive 的元數據(也可以用 Hive 自帶的嵌入式數據庫 Derby,但是 Hive 的生產環境一般不用 Derby),這里只需要安裝 MySQL 單機版即可,如果想保證高可用的化,也可以部署 MySQL 主從模式;
Hadoop
MySQL 隨意任選其一
CentOs7.3 安裝 MySQL 5.7.19 二進制版本
搭建 MySQL 5.7.19 主從復制,以及復制實現細節分析
安裝
下載解壓
su hadoop
cd /home/hadoop/ wget https://mirrors.tuna.tsinghua.edu.cn/apache/hive/hive-2.3.0/apache-hive-2.3.0-bin.tar.gz tar -zxvf apache-hive-2.3.0-bin.tar.gz mv apache-hive-2.3.0-bin hive-2.3.0 復制代碼
環境變量
如果是對所有的用戶都生效就修改vi /etc/profile 文件 如果只針對當前用戶生效就修改 vi ~/.bahsrc 文件
sudo vi /etc/profile
復制代碼
#hive export PATH=${HIVE_HOME}/bin:$PATH export HIVE_HOME=/home/hadoop/hive-2.3.0/ 復制代碼
使環境變量生效,運行 source /etc/profile使/etc/profile文件生效
Hive 配置 Hadoop HDFS
復制 hive-site.xml
cd /home/hadoop/hive-2.3.0/conf cp hive-default.xml.template hive-site.xml 復制代碼
新建 hdfs 目錄
使用 hadoop 新建 hdfs 目錄,因為在 hive-site.xml 中有默認如下配置:
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<description>location of default database for the warehouse</description> </property> <property> 復制代碼
進入 hadoop 安裝目錄 執行hadoop命令新建/user/hive/warehouse目錄,並授權,用於存儲文件
cd /home/hadoop/hadoop-2.7.4 bin/hadoop fs -mkdir -p /user/hive/warehouse bin/hadoop fs -mkdir -p /user/hive/tmp bin/hadoop fs -mkdir -p /user/hive/log bin/hadoop fs -chmod -R 777 /user/hive/warehouse bin/hadoop fs -chmod -R 777 /user/hive/tmp bin/hadoop fs -chmod -R 777 /user/hive/log 復制代碼
用以下命令檢查目錄是否創建成功
bin/hadoop fs -ls /user/hive
復制代碼
修改 hive-site.xml
搜索hive.exec.scratchdir,將該name對應的value修改為/user/hive/tmp
<property> <name>hive.exec.scratchdir</name> <value>/user/hive/tmp</value> </property> 復制代碼
搜索hive.querylog.location,將該name對應的value修改為/user/hive/log/hadoop
<property> <name>hive.querylog.location</name> <value>/user/hive/log/hadoop</value> <description>Location of Hive run time structured log file</description> </property> 復制代碼
搜索javax.jdo.option.connectionURL,將該name對應的value修改為MySQL的地址
<property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://192.168.252.124:3306/hive?createDatabaseIfNotExist=true</value> <description> JDBC connect string for a JDBC metastore. To use SSL to encrypt/authenticate the connection, provide database-specific SSL flag in the connection URL. For example, jdbc:postgresql://myhost/db?ssl=true for postgres database. </description> </property> 復制代碼
搜索javax.jdo.option.ConnectionDriverName,將該name對應的value修改為MySQL驅動類路徑
<property> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.jdbc.Driver</value> <description>Driver class name for a JDBC metastore</description> </property> 復制代碼
搜索javax.jdo.option.ConnectionUserName,將對應的value修改為MySQL數據庫登錄名
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
<description>Username to use against metastore database</description>
</property>
復制代碼
搜索javax.jdo.option.ConnectionPassword,將對應的value修改為MySQL數據庫的登錄密碼
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>mima</value>
<description>password to use against metastore database</description>
</property>
復制代碼
創建 tmp 文件
mkdir /home/hadoop/hive-2.3.0/tmp
復制代碼
並在 hive-site.xml 中修改
把{system:java.io.tmpdir} 改成 /home/hadoop/hive-2.3.0/tmp
把 {system:user.name} 改成 {user.name}
新建 hive-env.sh
cp hive-env.sh.template hive-env.sh
vi hive-env.sh
HADOOP_HOME=/home/hadoop/hadoop-2.7.4/
export HIVE_CONF_DIR=/home/hadoop/hive-2.3.0/conf export HIVE_AUX_JARS_PATH=/home/hadoop/hive-2.3.0/lib 復制代碼
下載 mysql 驅動包
cd /home/hadoop/hive-2.3.0/lib wget http://central.maven.org/maven2/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar 復制代碼
初始化 mysql
MySQL數據庫進行初始化
首先確保 mysql 中已經創建 hive 庫
cd /home/hadoop/hive-2.3.0/bin ./schematool -initSchema -dbType mysql 復制代碼
如果看到如下,表示初始化成功
Starting metastore schema initialization to 2.3.0
Initialization script hive-schema-2.3.0.mysql.sql
Initialization script completed
schemaTool completed
復制代碼
查看 mysql 數據庫
/usr/local/mysql/bin/mysql -uroot -p 復制代碼
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hive |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec) 復制代碼
mysql> use hive;
Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +---------------------------+ | Tables_in_hive | +---------------------------+ | AUX_TABLE | | BUCKETING_COLS | | CDS | | COLUMNS_V2 | | COMPACTION_QUEUE | | COMPLETED_COMPACTIONS | | COMPLETED_TXN_COMPONENTS | | DATABASE_PARAMS | | DBS | | DB_PRIVS | | DELEGATION_TOKENS | | FUNCS | | FUNC_RU | | GLOBAL_PRIVS | | HIVE_LOCKS | | IDXS | | INDEX_PARAMS | | KEY_CONSTRAINTS | | MASTER_KEYS | | NEXT_COMPACTION_QUEUE_ID | | NEXT_LOCK_ID | | NEXT_TXN_ID | | NOTIFICATION_LOG | | NOTIFICATION_SEQUENCE | | NUCLEUS_TABLES | | PARTITIONS | | PARTITION_EVENTS | | PARTITION_KEYS | | PARTITION_KEY_VALS | | PARTITION_PARAMS | | PART_COL_PRIVS | | PART_COL_STATS | | PART_PRIVS | | ROLES | | ROLE_MAP | | SDS | | SD_PARAMS | | SEQUENCE_TABLE | | SERDES | | SERDE_PARAMS | | SKEWED_COL_NAMES | | SKEWED_COL_VALUE_LOC_MAP | | SKEWED_STRING_LIST | | SKEWED_STRING_LIST_VALUES | | SKEWED_VALUES | | SORT_COLS | | TABLE_PARAMS | | TAB_COL_STATS | | TBLS | | TBL_COL_PRIVS | | TBL_PRIVS | | TXNS | | TXN_COMPONENTS | | TYPES | | TYPE_FIELDS | | VERSION | | WRITE_SET | +---------------------------+ 57 rows in set (0.00 sec) 復制代碼
啟動 Hive
簡單測試
啟動Hive
cd /home/hadoop/hive-2.3.0/bin ./hive 復制代碼
創建 hive 庫
hive> create database ymq;
OK
Time taken: 0.742 seconds
復制代碼
選擇庫
hive> use ymq;
OK
Time taken: 0.036 seconds
復制代碼
創建表
hive> create table test (mykey string,myval string); OK Time taken: 0.569 seconds 復制代碼
插入數據
hive> insert into test values("1","www.ymq.io"); WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases. Query ID = hadoop_20170922011126_abadfa44-8ebe-4ffc-9615-4241707b3c03 Total jobs = 3 Launching Job 1 out of 3 Number of reduce tasks is set to 0 since there's no reduce operator Starting Job = job_1506006892375_0001, Tracking URL = http://node1:8088/proxy/application_1506006892375_0001/ Kill Command = /home/hadoop/hadoop-2.7.4//bin/hadoop job -kill job_1506006892375_0001 Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0 2017-09-22 01:12:12,763 Stage-1 map = 0%, reduce = 0% 2017-09-22 01:12:20,751 Stage-1 map = 100%, reduce = 0%, Cumulative CPU 1.24 sec MapReduce Total cumulative CPU time: 1 seconds 240 msec Ended Job = job_1506006892375_0001 Stage-4 is selected by condition resolver. Stage-3 is filtered out by condition resolver. Stage-5 is filtered out by condition resolver. Moving data to directory hdfs://node1:9000/user/hive/warehouse/ymq.db/test/.hive-staging_hive_2017-09-22_01-11-26_242_8022847052615616955-1/-ext-10000 Loading data to table ymq.test MapReduce Jobs Launched: Stage-Stage-1: Map: 1 Cumulative CPU: 1.24 sec HDFS Read: 4056 HDFS Write: 77 SUCCESS Total MapReduce CPU Time Spent: 1 seconds 240 msec OK Time taken: 56.642 seconds 復制代碼
查詢數據
hive> select * from test; OK 1 www.ymq.io Time taken: 0.253 seconds, Fetched: 1 row(s) 復制代碼
頁面數據
在界面上查看剛剛寫入的hdfs數據
