服務器安裝與配置Hive

記錄服務器上部署與配置Hive組件

機器環境

  • Centos7
  • HDFS(啓動好)
  • mysql(保證服務可用–可選)
  • jdk

準備

  • 1.Hive 官網地址

http://hive.apache.org/

  • 2.文檔查看地址

https://cwiki.apache.org/confluence/display/Hive/GettingStarted

  • 3.下載地址

http://archive.apache.org/dist/hive/

Hive 安裝及配置

  • (1)把 apache-hive-1.2.1-bin.tar.gz 上傳到 linux 的/home/mym/apps (隨服務組件安裝自定義規範去選擇目錄即可)目錄下
  • (2)解壓 apache-hive-1.2.1-bin.tar.gz 到/home/mym/apps/目錄下面
[root@mym apps]# tar -zxvf 
apache-hive-1.2.1-bin.tar.gz 
  • (3)修改 apache-hive-1.2.1-bin.tar.gz 的名稱爲 hive
[root@mym conf]# mv apache-hive-1.2.1-bin/ hive
  • (4)修改/home/mym/apps/hive/conf 目錄下的 hive-env.sh.template 名稱爲 hive-env.sh
[root@mym conf]# mv hive-env.sh.template hive-env.sh
  • (5)配置 hive-env.sh 文件
    • (a)配置 HADOOP_HOME 路徑
    • (b)配置 HIVE_CONF_DIR 路徑
export HADOOP_HOME=/home/mym/apps/hadoop-2.4.1
export HIVE_CONF_DIR=/home/mym/apps/hive/conf
  • (6)配置hive-site.xml 文件(可選)

使用mysql來存儲源信息。若不配置默認是derby數據庫

vi hive-site.xml 配置元信息存儲

<configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost:3306/hive?createDatabaseIfNotExist=true</value>
<description>JDBC connect string for a JDBC metastore</description>
</property>

<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
<description>Driver class name for a JDBC metastore</description>
</property>

<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
<description>username to use against metastore database</description>
</property>

<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>123456</value>
<description>password to use against metastore database</description>
</property>
</configuration>

把mysql驅動拷貝到hive的lib目錄下

把mysql服務器對應的驅動版本jar:mysql-connector-java-5.1.6.jar 複製到 /home/mym/apps/hive/lib

當Hive服務啓動之後就會在mysql中看到一張hive表

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hive               |
| mysql              |
| performance_schema |
| qintong_test       |
+--------------------+
5 rows in set (0.00 sec)

Hive 基本操作

  • (1)啓動 hive
[root@mym hive]# bin/hive
  • (2)查看數據庫
hive> show databases;
  • (3)打開默認數據庫
hive> use default;
  • (4)顯示 default 數據庫中的表
hive> show tables;
  • (5)創建一張表
hive> create table student(id int, name string);
  • (6)顯示數據庫中有幾張表
hive> show tables;
  • (7)查看錶的結構
hive> desc student;
  • (8)向表中插入數據
hive> insert into student values(1000,"ss");
  • (9)查詢表中數據
hive> select * from student;
  • (10)退出 hive
hive> quit;
[root@mym hive]# bin/hive

Logging initialized using configuration in jar:file:/home/mym/apps/hive/lib/hive-common-1.2.1.jar!/hive-log4j.properties
Java HotSpot(TM) 64-Bit Server VM warning: You have loaded library /home/mym/apps/hadoop-2.4.1/lib/native/libhadoop.so.1.0.0 which might have disabled stack guard. The VM will try to fix the stack guard now.
It's highly recommended that you fix the library with 'execstack -c <libfile>', or link it with '-z noexecstack'.
hive> show databases;
OK
default
Time taken: 5.219 seconds, Fetched: 1 row(s)
hive> use default;
OK
Time taken: 0.363 seconds
hive> show tables;
OK
t_mym_test_01
Time taken: 0.186 seconds, Fetched: 1 row(s)
hive> create table student_test(id int, name string);
OK
Time taken: 2.54 seconds
hive> show tables;
OK
student_test
t_mym_test_01
Time taken: 0.114 seconds, Fetched: 2 row(s)
hive> desc student_test;
OK
id                      int
name                    string
Time taken: 0.412 seconds, Fetched: 2 row(s)
hive> insert into student_test values(1000, "ss");
Query ID = root_20191021120122_8c1e32c5-0a33-4c11-8827-87716f742fab
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_1571630964739_0001, Tracking URL = http://mym:8088/proxy/application_1571630964739_0001/
Kill Command = /home/mym/apps/hadoop-2.4.1/bin/hadoop job  -kill job_1571630964739_0001
Hadoop job information for Stage-1: number of mappers: 1; number of reducers: 0
2019-10-21 12:11:58,442 Stage-1 map = 0%,  reduce = 0%
2019-10-21 12:12:12,288 Stage-1 map = 100%,  reduce = 0%, Cumulative CPU 2.72 sec
MapReduce Total cumulative CPU time: 2 seconds 720 msec
Ended Job = job_1571630964739_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: hdfs://mym:9000/user/hive/warehouse/student_test/.hive-staging_hive_2019-10-21_12-01-22_858_5082688653628747608-1/-ext-10000
Loading data to table default.student_test
Table default.student_test stats: [numFiles=1, numRows=1, totalSize=8, rawDataSize=7]
MapReduce Jobs Launched:
Stage-Stage-1: Map: 1   Cumulative CPU: 2.72 sec   HDFS Read: 3546 HDFS Write: 84 SUCCESS
Total MapReduce CPU Time Spent: 2 seconds 720 msec
OK
Time taken: 652.252 seconds
hive> select * from student_test;
OK
1000    ss
Time taken: 0.113 seconds, Fetched: 1 row(s)
hive>

hive 1.x版本底層是mapreduce,所以hadoop集羣需要啓動yarn才能執行insert那個操作哦,否則會卡住。

Hive常見屬性配置

Hive 數據倉庫位置配置

  • 1)Default 數據倉庫的最原始位置是在 hdfs 上的:/user/hive/warehouse 路徑下。
[root@mym apps]# hdfs dfs -ls /user/hive/warehouse
Java HotSpot(TM) 64-Bit Server VM warning: You have loaded library /home/mym/apps/hadoop-2.4.1/lib/native/libhadoop.so.1.0.0 which might have disabled stack guard. The VM will try to fix the stack guard now.
It's highly recommended that you fix the library with 'execstack -c <libfile>', or link it with '-z noexecstack'.
19/10/21 12:51:38 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Found 2 items
drwxr-xr-x   - root supergroup          0 2019-10-21 12:12 /user/hive/warehouse/student_test
drwxr-xr-x   - mym  supergroup          0 2018-08-07 02:02 /user/hive/warehouse/t_mym_test_01
  • 2)在倉庫目錄下,沒有對默認的數據庫 default 創建文件夾。如果某張表屬於 default
    數據庫,直接在數據倉庫目錄下創建一個文件夾。
  • 3)修改 default 數據倉庫原始位置(將 hive-default.xml.template 如下配置信息拷貝到
    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>

配置同組用戶有執行權限

bin/hdfs dfs -chmod g+w /user/hive/warehouse

查詢後信息顯示配置

  • 1)在 hive-site.xml 文件中添加如下配置信息,就可以實現顯示當前數據庫,以及查詢表的頭信息配置。
<property>
    <name>hive.cli.print.header</name>
    <value>true</value>
</property>
<property>
    <name>hive.cli.print.current.db</name>
    <value>true</value>
</property>
  • 2)重新進入client。 對比配置前後差異
hive (default)> show tables;
OK
tab_name
student_test
t_mym_test_01

Hive 運行日誌信息配置

  • 1.Hive 的 log 默認存放在/tmp/<用戶名>/hive.log 目錄下(當前用戶名下)
[root@mym conf]# ll /tmp/root
total 448
drwx------. 2 root root      6 Oct 21 12:07 5ee5fd51-8a28-4e67-847f-1fc6cedd81b0
-rw-r--r--. 1 root root      0 Oct 21 12:06 5ee5fd51-8a28-4e67-847f-1fc6cedd81b06521240771716159995.pipeout
drwx------. 2 root root      6 Oct 21 12:58 7b749f26-d852-4ed7-870a-b918f6937fad
-rw-r--r--. 1 root root      0 Oct 21 12:57 7b749f26-d852-4ed7-870a-b918f6937fad1947350991899854945.pipeout
-rw-r--r--. 1 root root 210975 Oct 21 12:58 hive.log
  • 2.修改 hive 的 log 存放日誌到$HIVE_HOME/logs
    • (1)複製$HIVE_HOME/conf/hive-log4j.properties.template 文件名稱爲hive-log4j.properties
[root@mym conf]# cp hive-log4j.properties.template hive-log4j.properties
[root@mym conf]# vi hive-log4j.properties
  • (2)在 hive-log4j.properties 文件中修改 log 存放位置
#hive.log.dir=${java.io.tmpdir}/${user.name}
hive.log.dir=/home/mym/apps/hive/logs

當重新啓動hive客戶端時,日誌就可以在配置的路徑下看到

[root@mym logs]# pwd
/home/mym/apps/hive/logs
[root@mym logs]# ll
total 12
-rw-r--r--. 1 root root 9350 Oct 21 13:08 hive.log

一般參數配置方式

  • 1.查看當前所有的配置信息

會打印一大波

hive (default)> set;
  • 2.參數的配置三種方式
    • (1)配置文件方式

默認配置文件:hive-default.xml

用戶自定義配置文件:hive-site.xml

注意:用戶自定義配置會覆蓋默認配置。

另外,Hive 也會讀入 Hadoop 的配置,因
爲 Hive 是作爲 Hadoop 的客戶端啓動的,Hive 的配置會覆蓋 Hadoop 的配置。配置文件
的設定對本機啓動的所有 Hive 進程都有效。

  • (2)命令行參數方式
    啓動 Hive 時,可以在命令行添加-hiveconf param=value 來設定參數。

例如:

[root@mym hive]# bin/hive -hiveconf mapred.reduce.tasks=10;

Logging initialized using configuration in file:/home/mym/apps/hive/conf/hive-log4j.properties
Java HotSpot(TM) 64-Bit Server VM warning: You have loaded library /home/mym/apps/hadoop-2.4.1/lib/native/libhadoop.so.1.0.0 which might have disabled stack guard. The VM will try to fix the stack guard now.
It's highly recommended that you fix the library with 'execstack -c <libfile>', or link it with '-z noexecstack'.
hive (default)> set mapred.reduce.tasks;
mapred.reduce.tasks=10

注意:僅對本次 hive 啓動有效

  • (3)參數聲明方式
    可以在 HQL 中使用 SET 關鍵字設定參數
    例如:
hive (default)> set mapred.reduce.tasks=100;
hive (default)> set mapred.reduce.tasks;
mapred.reduce.tasks=100

注意:僅對本次 hive 啓動有效。

上述三種設定方式的優先級依次遞增。
配置文件<命令行參數<參數聲明

注意某些系統級的參數,例如 log4j 相關的設定,必須用前兩種方式設定,因爲那些參數的讀取在會話建立以前已經完成了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章