Hive安装,以及一些问题处理
1.Hive安装部署
1.Hive安装及配置
(1)把apache-hive-1.2.1-bin.tar.gz上传到linux的/opt/software目录下
(2)解压apache-hive-1.2.1-bin.tar.gz到/opt/module/目录下面
[ software]$ tar -zxvf apache-hive-1.2.1-bin.tar.gz -C /opt/module/
(3)修改apache-hive-1.2.1-bin.tar.gz的名称为hive
[ module]$ mv apache-hive-1.2.1-bin/ hive
(4)修改/opt/module/hive/conf目录下的hive-env.sh.template名称为hive-env.sh
[ conf]$ mv hive-env.sh.template hive-env.sh
(5)配置hive-env.sh文件
(a)配置HADOOP_HOME路径
export HADOOP_HOME=/opt/module/hadoop-2.7.2
(b)配置HIVE_CONF_DIR路径
export HIVE_CONF_DIR=/opt/module/hive/conf
2.Hadoop集群配置
(1)必须启动hdfs和yarn
[ hadoop-2.7.2]$ sbin/start-dfs.sh
[ hadoop-2.7.2]$ sbin/start-yarn.sh
(2)在HDFS上创建/tmp和/user/hive/warehouse两个目录并修改他们的同组权限可写
[ hadoop-2.7.2]$ bin/hadoop fs -mkdir /tmp
[ hadoop-2.7.2]$ bin/hadoop fs -mkdir -p /user/hive/warehouse
[ hadoop-2.7.2]$ bin/hadoop fs -chmod g+w /tmp
[ hadoop-2.7.2]$ bin/hadoop fs -chmod g+w /user/hive/warehouse
3.Hive基本操作
(1)启动hive
[ 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;
2.将本地文件导入Hive案例
需求
将本地/opt/module/datas/student.txt这个目录下的数据导入到hive的student(id int, name string)表中。
1.数据准备
在/opt/module/datas这个目录下准备数据
(1)在/opt/module/目录下创建datas
[ module]$ mkdir datas
(2)在/opt/module/datas/目录下创建student.txt文件并添加数据
[ datas]$ touch student.txt
[ datas]$ vi student.txt
1001 zhangshan
1002 lishi
1003 zhaoliu
注意以tab键间隔。
2.Hive实际操作
(1)启动hive
[ hive]$ bin/hive
(2)显示数据库
hive> show databases;
(3)使用default数据库
hive> use default;
(4)显示default数据库中的表
hive> show tables;
(5)删除已创建的student表
hive> drop table student;
(6)创建student表, 并声明文件分隔符’\t’
hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED
BY ‘\t‘;
(7)加载/opt/module/datas/student.txt 文件到student数据库表中。
hive> load data local inpath ‘/opt/module/datas/student.txt‘ into table student;
(8)Hive查询结果
hive> select * from student;
OK
1001 zhangshan
1002 lishi
1003 zhaoliu
Time taken: 0.266 seconds, Fetched: 3 row(s)
3.遇到的问题
再打开一个客户端窗口启动hive,会产生java.sql.SQLException异常。
Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException:
Unable to instantiate
org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:522)
at org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:677)
at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:621)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
at org.apache.hadoop.hive.metastore.MetaStoreUtils.newInstance(MetaStoreUtils.java:1523)
at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.<init>(RetryingMetaStoreClient.java:86)
at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.getProxy(RetryingMetaStoreClient.java:132)
at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.getProxy(RetryingMetaStoreClient.java:104)
at org.apache.hadoop.hive.ql.metadata.Hive.createMetaStoreClient(Hive.java:3005)
at org.apache.hadoop.hive.ql.metadata.Hive.getMSC(Hive.java:3024)
at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:503)
... 8 more
原因是,Metastore默认存储在自带的derby数据库中,推荐使用MySQL存储Metastore;
3. MySql安装
3.1 安装包准备
1.查看mysql是否安装,如果安装了,卸载mysql
(1)查看
[ 桌面]# rpm -qa|grep mysql
mysql-libs-5.1.73-7.el6.x86_64
(2)卸载
[ 桌面]# rpm -e --nodeps mysql-libs-5.1.73-7.el6.x86_64
2.解压mysql-libs.zip文件到当前目录
[ software]# unzip mysql-libs.zip
[ software]# ls
mysql-libs.zip
mysql-libs
3.进入到mysql-libs文件夹下
[ mysql-libs]# ll
总用量 76048
-rw-r--r--. 1 root root 18509960 3月 26 2015 MySQL-client-5.6.24-1.el6.x86_64.rpm
-rw-r--r--. 1 root root 3575135 12月 1 2013 mysql-connector-java-5.1.27.tar.gz
-rw-r--r--. 1 root root 55782196 3月 26 2015 MySQL-server-5.6.24-1.el6.x86_64.rpm
3.2 安装MySql服务器
1.安装mysql服务端
[ mysql-libs]# yum -y install autoconf(需要的依赖perl)
[ mysql-libs]# yum remove mariadb-libs-5.5.56-2.el7(依赖冲突删除)
[ mysql-libs]# rpm -ivh MySQL-server-5.6.24-1.el6.x86_64.rpm
2.查看产生的随机密码
[ mysql-libs]# cat /root/.mysql_secret
OEXaQuS8IWkG19Xs
3.查看mysql状态
[ mysql-libs]# service mysql status
4.启动mysql
[ mysql-libs]# service mysql start
3.3 安装MySql客户端
1.安装mysql客户端
[ mysql-libs]# rpm -ivh MySQL-client-5.6.24-1.el6.x86_64.rpm
2.链接mysql
[ mysql-libs]# mysql -uroot -pOEXaQuS8IWkG19Xs
3.修改密码
mysql>SET PASSWORD=PASSWORD(‘000000‘);
4.退出mysql
mysql>exit
3.4 MySql中user表中主机配置
配置只要是root用户+密码,在任何主机上都能登录MySQL数据库。
1.进入mysql
[ mysql-libs]# mysql -uroot -proot
2.显示数据库
mysql>show databases;
3.使用mysql数据库
mysql>use mysql;
4.展示mysql数据库中的所有表
mysql>show tables;
5.展示user表的结构
mysql>desc user;
6.查询user表
mysql>select User, Host, Password from user;
7.修改user表,把Host表内容修改为%
mysql>update user set host=‘%‘ where host=‘localhost‘;
8.删除root用户的其他host
mysql>delete from user where Host=‘hadoop102‘;
mysql>delete from user where Host=‘127.0.0.1‘;
mysql>delete from user where Host=‘::1‘;
如果启动hive时显示连接不上mysql,这需要添加以下信息到user表中
mysql>grant all privileges on *.* to ‘%‘ identified by ‘root‘
mysql>grant all privileges on *.* to ‘hadoop111‘ identified by ‘root‘
9.刷新
mysql>flush privileges;
10.退出
mysql>quit;
4 Hive元数据配置到MySql
4.1 驱动拷贝
1.在/opt/software/mysql-libs目录下解压mysql-connector-java-5.1.27.tar.gz驱动包
[ mysql-libs]# tar -zxvf mysql-connector-java-5.1.27.tar.gz
2.拷贝/opt/software/mysql-libs/mysql-connector-java-5.1.27目录下的mysql-connector-java-5.1.27-bin.jar到/opt/module/hive/lib/
[ mysql-connector-java-5.1.27]# cp mysql-connector-java-5.1.27-bin.jar
/opt/module/hive/lib/
4.2 配置Metastore到MySql
1.在/opt/module/hive/conf目录下创建一个hive-site.xml
[ conf]$ touch hive-site.xml
[ conf]$ vi hive-site.xml
2.根据官方文档配置参数,拷贝数据到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://hadoop102: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>
</configuration>
3.配置完毕后,如果启动hive异常,可以重新启动虚拟机。(重启后,别忘了启动hadoop集群)
4.如果进入hive执行命令时报错:hive Caused by: MetaException(message:Version information not found in metastore. )解决方法
元数据是hive中存的,应该还是与hive相关的配置有关。经查,把hive-site.xml中如下配置项改为false可归避此问题。
<property>
<name>hive.metastore.schema.verification</name>
<value>true</value>
</property>
5.MetaException(message:Hive metastore database is not initialized. Please use schematool 解决方法:
需要执行以下指令(在hive的bin目录下执行)
./schematool -initSchema -dbType mysql
最后一个参数为hive存放元数据所使用的数据库,我用的是mysql。如果使用derby,将最后的参数改为derby就可以了。
4.3 多窗口启动Hive测试
1.先启动MySQL
[ mysql-libs]$ mysql -uroot -p000000
查看有几个数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
2.再次打开多个窗口,分别启动hive
[ hive]$ bin/hive
3.启动hive后,回到MySQL窗口查看数据库,显示增加了metastore数据库
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| metastore |
| mysql |
| performance_schema |
| test |
+--------------------+
4.如果启动时报错连接不上数据库,通过一下命令添加到user表中连接信息
grant all privileges on *.* to ‘%‘ identified by ‘root‘
grant all privileges on *.* to ‘hadoop111‘ identified by ‘root‘