Hive 学习笔记(1)— Hive的配置
系统环境
OS: centos6.5 64bit
metastore: MySQL
Hive version: 1.2.1
hadoop version 2.6.5
安装配置
1. 官网下载hive的二进制包
链接地址:https://mirrors.tuna.tsinghua.edu.cn/apache/hive/
根据需求,下载对应版本,笔者下载的是1.2.1的。
2. 解压
笔者的软件安装在/opt目录下,可按照自己情况进行修改下面的命令
tar -zxvf apache-hive-1.2.1-bin.tar.gz -C /opt
mv hive-1.2.1 hive
3. 设置系统变量
export HIVE_HOME=/opt/hive
export PATH=$PATH:$HIVE_HOME/bin
export CLASS_PATH=$CLASSPATH:$HIVE_HOME/lib
4. 修改配置文件
cd /opt/hive/conf
cp hive-default.xml.template hive-site.xml
vim hive-site.xml
在其中加入如下内容:
<configuration>
<property>
<name>hive.server2.thrift.bind.host</name>
<value>master1</value>
<description>Bind host on which to run the HiveServer2 Thrift service.</description>
</property>
<property>
<name>datanucleus.fixedDatastore</name>
<value>false</value>
</property>
<property>
<name>hive.metastore.execute.setugi</name>
<value>true</value>
</property>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://master1:3306/hive</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>macan1015</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>
其中需要安装自身要求修改的地方:
1. 将mstaer1 修改成存放元数据的服务器地址或者主机名
2. 将root 修改为MySQL的用户名
3. 将123456 修改成用户名密码(数据库的当前用户密码,非Linux用户密码)
5. HDFS添加文件夹
hadoop fs -mkidr /tmp
hadoop fs -mkidr /user/hive/warehouse
hadoop fs -chmod g+w /tmp
hadoop fs -chmod g+w /user/hive/warehouse
6. MySQL 创建表
create database hive;
7. 上传jdbc 驱动包
找一个MySQL的jdbc包上传到hive/lib下
mysql-connector-java-5.1.22-bin.jar
8. 启动Hive
启动元数据存储
hive --service metastore &
启动hiveserver2
hive -service hiveserver2 &
三、连接Hive
1. beeline 客户端连接Hive
在集群中的任意一台服务器上敲beeline回车
在beeline服务中敲:
!connect jdbc:hive2://master1:10000
2. 使用jdbc连接Hive
java 代码如下:
package canma.dmml.hiveConn;
import java.sql.*;
/**
* Created by macan on 2017/1/30.
*/
public class HiveConn {
public static void main(String[] args) throws SQLException {
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = DriverManager.getConnection("jdbc:hive2://master1:10000/default", "username", "password");
try {
Statement st = conn.createStatement();
ResultSet rst = st.executeQuery("select * from pokes");
if (rst.next()) {
System.out.println(rst.getString(1));
}
}catch (Exception e){
System.out.println(e);
}finally {
conn.close();
}
}
}