Hive

Hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张表,并提供类SQL查询功能。
本质是:将HQL转化成MapReduce程序
1)Hive处理的数据存储在HDFS
2)Hive分析数据底层的实现是MapReduce
3)执行程序运行在Yarn上

1. Hive安装

1.1 Hive安装地址

  1. Hive官网地址
    https://hive.apache.org/
  2. 文档查看地址
    https://cwiki.apache.org/confluence/display/Hive/GettingStarted
  3. 下载地址
    http://archive.apache.org/dist/hive/
  4. github地址
    https://github.com/apache/hive

1.2 hive2.3.6安装

  1. 解压
$ tar -zxvf apache-hive-2.3.6-bin.tar.gz -C /opt/module/
  1. 修改 apache-hive-2.3.6-bin 名称为 hive2.3.6
  2. 将 Mysql 的 mysql-connector-java-5.1.27-bin.jar 拷贝到/opt/module/hive2.3.6/lib/
  3. 在/opt/module/hive2.3.6/conf 路径上, 创建 hive-site.xml 文件,添加如下内容:
    使用MySQL存储Metastore
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
	<property>
		<name>javax.jdo.option.ConnectionURL</name>
		<value>jdbc:mysql://hadoop116:3306/metastore?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>000000</value>
		<description>password to use against metastore database</description>
	</property>
	<property>
		<name>hive.metastore.warehouse.dir</name>
		<value>/user/hive/warehouse</value>
		<description>location of default database for the warehouse</description>
	</property>
	<property>
		<name>hive.cli.print.header</name>
		<value>true</value>
	</property>
	<property>
		<name>hive.cli.print.current.db</name>
		<value>true</value>
	</property>
	<property>
		<name>hive.metastore.schema.verification</name>
		<value>false</value>
	</property>
	<property>
		<name>datanucleus.schema.autoCreateAll</name>
		<value>true</value>
	</property>
	
	<property>
		<name>hive.metastore.uris</name>
		<value>thrift://hadoop116:9083</value>
	</property>
</configuration>

注意:hive安装在哪个服务器节点,thrift://hadoop116:9083中的主机名就要更换为相应的主机。

1.3 启动服务

  1. 需要先启动两个服务
    一个hive元数据服务metastore ,一个远程连接服务hiveserver2
[lhcz@host-192-168-240-116 hive2.3.6]$ nohup bin/hive --service metastore &
[lhcz@host-192-168-240-116 hive2.3.6]$ nohup bin/hive --service hiveserver2 &
  1. 服务启动后启动hive
[lhcz@host-192-168-240-116 hive2.3.6]$ bin/hive

1.4 将本地文件导入hive

现有一个文件 /opt/data/student.txt,文件内容如下:

1001 jam
1002 kate
1003 john

将这个文件导入hive

  1. 创建student表, 并声明文件分隔符’ ’
hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
  1. 加载文件到数据库表student中
load data local inpath '/opt/data/student.txt' into table student;

2. Hive集成Tez

官网

  1. 拷贝 apache-tez-0.9.1-bin.tar.gz 到 hadoop116 的/opt/software 目录
  2. 将 apache-tez-0.9.1-bin.tar.gz 上传到hdfs的/tez目录
$ hadoop fs -mkdir /tez
$ hadoop fs -put /opt/software/apache-tez-0.9.1-bin.tar.gz/ /tez
  1. 解压缩到/opt/module目录,并修改名称
$ tar -zxvf apache-tez-0.9.1-bin.tar.gz -C /opt/module
$ mv apache-tez-0.9.1-bin/ tez-0.9.1

2.1 集成tez

  1. 在 Hive 的/opt/module/hive/conf 下面创建一个 tez-site.xml 文件,添加以下内容
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
	<property>
		<name>tez.lib.uris</name>
		<value>${fs.defaultFS}/tez/apache-tez-0.9.1-bin.tar.gz</value>
	</property>
	<property>
		<name>tez.use.cluster.hadoop-libs</name>
		<value>true</value>
	</property>
	<property>
		<name>tez.history.logging.service.class</name>
		<value>org.apache.tez.dag.history.logging.ats.ATSHistoryLoggingService</value>
	</property>
</configuration>
  1. 在 hive-env.sh 文件中添加 tez 环境变量配置和依赖包环境变量配置
$ mv hive-env.sh.template hive-env.sh
$ vim hive-env.sh
  1. 添加以下内容
export TEZ_HOME=/opt/module/tez-0.9.1 #是你的 tez 的解压目录
export TEZ_JARS=""
for jar in `ls $TEZ_HOME |grep jar`; do
	export TEZ_JARS=$TEZ_JARS:$TEZ_HOME/$jar
done
for jar in `ls $TEZ_HOME/lib`; do
	export TEZ_JARS=$TEZ_JARS:$TEZ_HOME/lib/$jar
done
export HIVE_AUX_JARS_PATH=/opt/module/hadoop-2.7.2/share/hadoop/common/hadoop-lzo-0.4.20.jar$TEZ_JARS
  1. 在 hive-site.xml 文件中添加如下配置, 更改 hive 计算引擎
<property>
	<name>hive.execution.engine</name>
	<value>tez</value>
</property>

2.2 注意事项

  1. 运行 Tez 时检查到用过多内存而被 NodeManager 杀死进程问题
  2. 解决办法
    (1)关掉虚拟内存检查, 修改 yarn-site.xml
<property>
	<name>yarn.nodemanager.vmem-check-enabled</name>
	<value>false</value>
</property>
  1. (2) 修改后一定要分发, 并重新启动 hadoop 集群
$ xsync yarn-site.xml

3. Hive命令

3.1 hive常用交互命令

$ bin/hive -help
  1. “-e” 不进入hive的交互窗口执行sql语句
$ bin/hive -e select id from student;
  1. “-f” 执行脚本中的sql
    在/opt/data目录下,有文件student.sql,文件内容为:select * from student;
    (1) 执行文件中的sql语句
$ bin/hive -f /opt/data/student.sql

(2) 执行文件中的sql语句并将结果保存至输出文件中

$ bin/hive -f /opt/data/student.sql > /opt/data/out.txt

3.2 hive其他命令操作

  1. 在hive cli命令窗口中如何查看hdfs文件系统
hive(default)>dfs -ls /;
  1. 在hive cli命令窗口中如何查看本地文件系统
    配置的hive文件存放位置为:/user/hive/warehouse
hive(default)>! ls /opt/module/datas;
  1. 查看在hive中输入的所有历史命令
    进入到当前用户的根目录/root或/home/lhcz,查看. hivehistory文件。cat . hivehistory

4. hive常见属性配置