转:
http://blog.csdn.net/reesun/article/details/8556078
http://blog.csdn.net/zhongguozhichuang/article/details/52702476
http://blog.csdn.net/huoyuyan/article/details/50769104
一、Hive 运行模式
与 Hadoop 类似,Hive 也有 3 种运行模式:
1. 内嵌模式
将元数据保存在本地内嵌的 Derby 数据库中,这是使用 Hive 最简单的方式。但是这种方式缺点也比较明显,因为一个内嵌的 Derby 数据库每次只能访问一个数据文件,这也就意味着它不支持多会话连接。
2. 本地模式
这种模式是将元数据保存在本地独立的数据库中(一般是 MySQL),这用就可以支持多会话和多用户连接了。
3. 远程模式
此模式应用于 Hive 客户端较多的情况。把 MySQL 数据库独立出来,将元数据保存在远端独立的 MySQL 服务中,避免了在每个客户端都安装 MySQL 服务从而造成冗余浪费的情况。
二、下载安装 Hive
http://hive.apache.org/downloads.html
tar -xzvf apache-hive-2.1.1-bin.tar.gz ##解压
三、配置系统环境变量
修改 /etc/profile 文件 vi /etc/profile 来修改(root用户操作):
- 设置 Hive环境变量
- # Hive environment
- export HIVE_HOME=/home/hadoop/cloud/apache-hive-2.1.1-bin
- export PATH=$HIVE_HOME/bin:$HIVE_HOME/conf:$PATH
- 使环境变量生效:
- source /etc/profile
四、内嵌模式
(1)修改 Hive 配置文件
$HIVE_HOME/conf 对应的是 Hive 的配置文件路径,类似于之前学习的Hbase, 该路径下的 hive-site.xml 是 Hive 工程的配置文件。默认情况下,该文件并不存在,我们需要拷贝它的模版来实现:
- cp hive-default.xml.template hive-site.xml
hive-site.xml 的主要配置有:
同时我们还要修改 Hive 目录下 /conf/hive-env.sh 文件(请根据自己的实际路径修改),该文件默认也不存在,同样是拷贝它的模版来修改:
cp hive-env.sh.template hive-env.sh
- # Set HADOOP_HOME to point to a specific hadoop install directory
- HADOOP_HOME=/home/hadoop/cloud/hadoop-2.7.3
-
- # Hive Configuration Directory can be controlled by:
- export HIVE_CONF_DIR=/home/hadoop/cloud/apache-hive-2.1.1-bin/conf
-
- # Folder containing extra ibraries required for hive compilation/execution can be controlled by:
- export HIVE_AUX_JARS_PATH=/home/hadoop/cloud/apache-hive-2.1.1-bin/lib
(2)创建必要目录
前面我们看到 hive-site.xml 文件中有两个重要的路径,切换到 hadoop 用户下查看 HDFS 是否有这些路径:
没有发现上面提到的路径,因此我们需要自己新建这些目录,并且给它们赋予用户写(W)权限。
- $HADOOP_HOME/bin/hadoop fs -mkdir -p /user/hive/warehouse
- $HADOOP_HOME/bin/hadoop fs -mkdir -p /tmp/hive/
- hadoop fs -chmod 777 /user/hive/warehouse
- hadoop fs -chmod 777 /tmp/hive
检查是否新建成功 hadoop fs -ls / 以及 hadoop fs -ls /user/hive/ :
(3)修改 io.tmpdir/user.name
同时,要修改 hive-site.xml 中所有包含 ${system:java.io.tmpdir} 字段的 value 即路径(vim下 / 表示搜索,后面跟你的关键词,比如搜索 hello,则为 /hello , 再回车即可),你可以自己新建一个目录来替换它,例如 /home/hadoop/cloud/apache-hive-2.1.1-bin/iotmp
- mkdir /home/hadoop/cloud/apache-hive-2.1.1-bin/iotmp
- chmod 777 /home/hadoop/cloud/apache-hive-2.1.1-bin/iotmp
- 把hive-site.xml 中所有包含 ${system:Java.io.tmpdir}替换成/home/hadoop/cloud/apache-hive-2.1.1-bin/iotmp
全局替换命令 先按Esc键 再同时按shift+:把以下替换命令粘贴按回车即可全局替换
- %s#${system:java.io.tmpdir}#/home/hadoop/cloud/apache-hive-2.1.1-bin/iotmp#g
-
%s#${system:user.name}#delin#g
(4)运行 Hive
报错
![hive2.1.1安装部署_第1张图片](http://img.e-com-net.com/image/info8/441ad6b1dea24f12bc241776c24b9894.png)
解决办法:./bin/schematool -initSchema -dbType derby
报错
![hive2.1.1安装部署_第2张图片](http://img.e-com-net.com/image/info8/e93246e286bd49728865e683a9c3d81d.png)
解决方法:删除/home/hadoop/cloud/apache-hive-2.1.1-bin目录下 rm -rf metastore_db/ 在初始化:
./bin/schematool -initSchema -dbType derby
重新运行
./bin/hive
报错
![](http://img.e-com-net.com/image/info8/4b992e3f025a40528663aa4bfd182f86.jpg)
/tem/hive 没写的权限
Hive本身自带一个数据库,但是有弊端,hive本身数据库,每次只允许一个用户登录
mysql安装:http://blog.csdn.net/u014695188/article/details/51532410
设置mysql关联hive
修改配置文件
### 创建hive-site.xml文件
在hive/conf/目录下创建hive-site.xml文件
- xml version="1.0" encoding="UTF-8" standalone="no"?>
- xml-stylesheet type="text/xsl" href="configuration.xsl"?>
- <configuration>
- <property>
- <name>javax.jdo.option.ConnectionURLname>
- <value>jdbc:mysql://localhost:3306/hive?useSSL=false&;createDatabaseIfNotExist=truevalue>
- property>
- <property>
- <name>javax.jdo.option.ConnectionDriverNamename>
- <value>com.mysql.jdbc.Drivervalue>
- property>
- <property>
- <name>javax.jdo.option.ConnectionUserNamename>
- <value>rootvalue>
- property>
- <property>
- <name>javax.jdo.option.ConnectionPasswordname>
- <value>123456value>
- property>
- <property>
- <name>hive.metastore.schema.verificationname>
- <value>falsevalue>
- <description>
- Enforce metastore schema version consistency.
- True: Verify that version information stored in metastore matches with one from Hive jars. Also disable automatic
- schema migration attempt. Users are required to manully migrate schema after Hive upgrade which ensures
- proper metastore schema migration. (Default)
- False: Warn if the version information stored in metastore doesn't match with one from in Hive jars.
- description>
- property>
- configuration>
报错:Caused by: MetaException(message:Version information not found in metastore. )
解决:hive-site.xml加入
- <property>
- <name>hive.metastore.schema.verificationname>
- <value>falsevalue>
- <description>
- Enforce metastore schema version consistency.
- True: Verify that version information stored in metastore matches with one from Hive jars. Also disable automatic
- schema migration attempt. Users are required to manully migrate schema after Hive upgrade which ensures
- proper metastore schema migration. (Default)
- False: Warn if the version information stored in metastore doesn't match with one from in Hive jars.
- description>
- property>
报错:缺少mysql jar包
解决:将其(如mysql-connector-java-5.1.15-bin.jar)拷贝到$HIVE_HOME/lib下即可。
报错:
- Exception in thread "main" java.lang.RuntimeException: Hive metastore database is not initialized.
- Please use schematool (e.g. ./schematool -initSchema -dbType ...) to create the schema. If needed,
- don't forget to include the option to auto-create the underlying database in your JDBC connection string (e.g. ?createDatabaseIfNotExist=true for mysql)
解决:
- #数据库的初始化。
- bin/schematool -initSchema -dbType mysql
报错:[ERROR] Terminal initialization failed; falling back to unsupported
java.lang.IncompatibleClassChangeError: Found class jline.Terminal, but interface was expected
at jline.TerminalFactory.create(TerminalFactory.java:101)
解决:http://www.68idc.cn/help/buildlang/ask/20151006570946.html
${HADOOP_HOME}/share/hadoop/yarn/lib 目录下存在老版本jline
解决:把${HIVE_HOME}/lib/下的 jline-x.xx拷贝得到${HADOOP_HOME}/share/hadoop/yarn/lib
cp /hive/apache-hive-1.1.0-bin/lib/jline-2.12.jar /hadoop-2.5.2/share/hadoop/yarn/lib
启动:
启动后mysql 多了hive 数据库
![hive2.1.1安装部署_第3张图片](http://img.e-com-net.com/image/info8/162a7d93bf284dd1aa6897f618418164.png)
测试
创建数据库
create database db_hive_test;
创建测试表
use db_hive_test;
create table student(id int,name string) row format delimited fields terminated by '\t';
![](http://img.e-com-net.com/image/info8/7cb8fe7f85bd4285b153dabb730d1e59.png)
![hive2.1.1安装部署_第4张图片](http://img.e-com-net.com/image/info8/c700567ec7d24dacb4396c2f4f750c05.png)
加载数据到表中
新建student.txt 文件写入数据(id,name 按tab键分隔)
vi student.txt
- 1001 zhangsan
- 1002 lisi
- 1003 wangwu
- 1004 zhaoli
load data local inpath '/home/hadoop/student.txt' into table db_hive_test.student
![hive2.1.1安装部署_第5张图片](http://img.e-com-net.com/image/info8/65ee77685d2f4c53bac58570628dd28b.png)
查询表信息
select * from student;
查看表的详细信息
desc formatted student;
通过ui页面查看创建的数据位置
http://192.168.169.132:50070/explorer.html#/user/hive/warehouse/db_hive_test.db
![hive2.1.1安装部署_第6张图片](http://img.e-com-net.com/image/info8/c7b82ddf796f4a5088c00639df5c2040.png)
通过Mysql查看创建的表
![](http://img.e-com-net.com/image/info8/4667654a41434535af55ae1a941c8de6.jpg)
查看hive的函数
show functions;
查看函数详细信息
desc function sum;
desc function extended
![hive2.1.1安装部署_第7张图片](http://img.e-com-net.com/image/info8/53847b6aa182480e88e45f12f986841b.png)