Hive安装参考
1.Hive官网地址:http://hive.apache.org
2.官方文档查看地址:https://cwiki.apache.org/confluence/display/Hive/GettingStarted
3.下载地址:http://archive.apache.org/dist/hive/ (本例使用的是hive-1.2.1)
4.github地址:https://github.com/apache/hive(这里查看源码)
Hive安装部署
0.部署前提
1.必须启动hdfs和yarn
2.在HDFS上创建/tmp和/user/hive/warehouse两个目录并修改他们的同组权限可写
1.Hive安装及配置
- 把apache-hive-1.2.1-bin.tar.gz上传到linux的/opt/software目录下
- 解压apache-hive-1.2.1-bin.tar.gz到指定目录下面(/opt/module/)
- 修改apache-hive-1.2.1-bin.tar.gz的名称为hive
- 修改/opt/module/hive/conf目录下的hive-env.sh.template名称为hive-env.sh
- 配置hive-env.sh文件
a.配置HADOOP_HOME路径
b.配置HIVE_CONF_DIR路径
2.Hive基本操作
- 启动Hive
进入到hive目录,运行脚本 $ bin/hive - 查看数据库
hive>show databases; - 打开默认数据库
hive>use default; - 显示default数据库中的表
hive>show tables; - 创建一张表
hive> create table student(id int, name string) row format delimited fields terminated by "\t";
(表名) (字段 类型) (行格式) (数据导入时按照\t来分割) - 查看表的结构
hive>desc student; - 向表中插入数据
hive> insert into student values(1,"lzl"); - 查询表中数据
hive> select * from student; - 将本地文件导入Hive
数据准备:在/opt/module/datas/student.txt这个目录下准备数据
101 linzhiling
102 tongliya
103 shendianxia /*注意每行中间用制表符分割,原因看上面第5点*/
加载/opt/module/datas/student.txt 文件到student数据库表中
hive> load data local inpath '/opt/module/datas/student.txt' into table student;
(加载数据)(数据位置,不指定为HDFS中,这里指定本地)
完成插入数据后,可以执行hive>select * from student;进行数据查询,会返回插入student中的结果。 - 退出hive
hive> quit;
3.如何开启多Hive客户端
当用户已经启动了一个Hive客户端后,再打开一个客户端窗口启动hive,会产生java.sql.SQLException异常。原因是,Metastore默认存储在自带的derby数据库中,推荐使用MySQL存储Metastore;
这里介绍一种将Hive元数据配置到MySql中的方式来解决开多客户端启动hive崩溃这种情况(如果要看mysql相关配置可参考https://blog.csdn.net/liangzelei/article/details/80096099)
- 驱动拷贝
准备mysql-connector的jar包,这里提供的是针对mysql5的jar包。下载好后在目录下解压mysql-connector-java-5.1.27.tar.gz驱动包
拷贝mysql-connector-java-5.1.27目录下的mysql-connector-java-5.1.27-bin.jar到Hive的lib目录下,本例中的路径为如下:/opt/module/hive/lib/ - 配置Metastore到MySql
在/opt/module/hive/conf目录下创建一个hive-site.xml
根据官方文档配置参数,拷贝数据到hive-site.xml文件中。可参考官方文档中的说明文档进行配置,https://cwiki.apache.org/confluence/display/Hive/AdminManual+MetastoreAdmin
这里提供一个具体的参数配置案例
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://hadoop101: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>666666</value>
<description>password to use against metastore database</description>
</property>
</configuration>
例配置完毕后,如果启动hive异常,可以重新启动系统。(重启后,别忘了启动hadoop集群),开始进行多窗口Hive测试,再次打开多个窗口,分别启动hive,启动hive后,回到MySQL窗口查看数据库,显示增加了metastore数据库
Hive常用交互命令
在开启hive后,可以通过$ bin/hive -help来查看hive的基本命令和说明
usage: hive
-d,--define <key=value> Variable subsitution to apply to hive
commands. e.g. -d A=B or --define A=B
--database <databasename> Specify the database to use
-e <quoted-query-string> SQL from command line
-f <filename> SQL from files
-H,--help Print help information
--hiveconf <property=value> Use value for given property
--hivevar <key=value> Variable subsitution to apply to hive
commands. e.g. --hivevar A=B
-i <filename> Initialization SQL file
-S,--silent Silent mode in interactive shell
-v,--verbose Verbose mode (echo executed SQL to the console)
常用命令介绍:
“-e”不进入hive的交互窗口执行sql语句
$ bin/hive -e "select id from default.student;" 这里不会进入hive而是直接返回查到的数据到终端
“-f”执行脚本中sql语句
在目录下创建hivef.sql文件,内容为要执行的HQL语句,如下:select *from student;执行文件中的sql语句,本例执行语句为$ bin/hive -f /opt/module/datas/hivef.sql执行文件中的sql语句并将结果写入文件中,语句如下:
$ bin/hive -f /opt/module/datas/hivef.sql > /opt/module/datas/hive_result.txt
Hive其他命令操作
- 退出hive窗口
hive(default)>exit; 或者 hive(default)>quit; - 在hive cli命令窗口中如何查看hdfs文件系统
hive(default)>dfs -ls /; - 在hive cli命令窗口中如何查看hdfs本地系统
hive(default)>! ls /opt/module/datas; - 查看在hive中输入的所有历史命令
方法1:进入到当前用户的根目录/root或/home/用户
方法2:查看. hivehistory文件 ~$ cat .hivehistory
Hive常见属性配置
- Hive数据仓库位置配置
(1).Default数据仓库的最原始位置是在hdfs上的:/user/hive/warehouse路径下
(2).在仓库目录下,没有对默认的数据库default创建文件夹。如果某张表属于default数据库,直接在数据仓库目录下创建一个文件夹。
(3).修改default数据仓库原始位置(将hive-default.xml.template如下配置信息拷贝到hive-site.xml文件中)
(4).配置同组用户有执行权限 $ bin/hdfs dfs -chmod g+w /user/hive/warehouse<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
<description>location of default database for the warehouse</description>
</property> - 显示当前数据库,以及查询表的头信息配置
在hive-site.xml文件中添加如下配置信息,就可以实现显示当前数据库,以及查询表的头信息配置。
重新启动hive,对比配置前后差异<property>
<name>hive.cli.print.header</name>
<value>true</value>
</property>
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
</property> - Hive运行日志信息配置
Hive的log默认存放在/tmp/atguigu/hive.log目录下(当前用户名下)。
修改hive的log存放日志到/opt/module/hive/logs
(1).修改/opt/module/hive/conf/hive-log4j.properties.template文件名称为hive-log4j.properties
(2).在hive-log4j.properties文件中修改log存放位置 hive.log.dir=/opt/module/hive/logs - 参数配置方式
查看当前所有的配置信息 ,命令为:hive>set;
参数的配置三种方式及优先级
上述三种设定方式的优先级依次递增。即配置文件<命令行参数<参数声明。注意某些系统级的参数,例如log4j相关的设定,必须用前两种方式设定,因为那些参数的读取在会话建立以前已经完成了。配置文件方式 默认配置文件:hive-default.xml
用户自定义配置文件:hive-site.xml
注意:用户自定义配置会覆盖默认配置。另外,Hive也会读入Hadoop的配置,因为Hive是作为Hadoop的客户端启动的,Hive的配置会覆盖Hadoop的配置。配置文件的设定对本机启动的所有Hive进程都有效。命令行参数方式 启动Hive时,可以在命令行添加-hiveconf param=value来设定参数。
例如:
[atguigu@hadoop103 hive]$ bin/hive -hiveconf mapred.reduce.tasks=10;
注意:仅对本次hive启动有效
查看参数设置:
hive (default)> set mapred.reduce.tasks;参数声明方式 可以在HQL中使用SET关键字设定参数
例如:
hive (default)> set mapred.reduce.tasks=100;
注意:仅对本次hive启动有效。
查看参数设置
hive (default)> set mapred.reduce.tasks;