服务器安装与配置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 相关的设定,必须用前两种方式设定,因为那些参数的读取在会话建立以前已经完成了。

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