首先虚拟机上已经安装好hive。
下面是连接hive需要的操作。
一、配置。
1.查找虚拟机的ip
输入 ifconfig
2.配置文件
(1)配置hadoop目录下的core-site.xml和hdfs-site.xml
在core-site.xml中添加下面配置:
<property>
<name>hadoop.proxyuser.hadoop.hosts</name>
<value>*</value>
</property>
<property>
<name>hadoop.proxyuser.hadoop.groups</name>
<value>*</value>
</property>
在hdfs-site.xml添加以下配置:
<property>
<name>dfs.webhdfs.enabled</name>
<value>true</value>
</property>
(2)配置hive目录中的conf文件夹下的hive-site.xml文件(这是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.ConnectionURL</name>
<value>jdbc:mysql://localhost:3306/hive?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>hive</value> //连接 hive 的用户名
<description>username to use against metastore database</description>
</property>
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>hive</value> //连接 hive 的密码
<description>password to use against metastore database</description>
</property>
//以上部分是在安装hive的时候就需要进行的配置,已经配置好的不用进行修改
//----------------------------------------------------分割线--------------------------------
<!-- 这是hiveserver2 -->
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/usr/hive/warehouse</value> //(hive所在集群的IP地址)
<description>location of default database for the warehouse</description>
</property>
<property>
<name>hive.server2.thrift.port</name>
<value>10000</value>
<description>Port number of HiveServer2 Thrift interface.
Can be overridden by setting $HIVE_SERVER2_THRIFT_PORT</description>
</property>
<property>
<name>hive.server2.thrift.bind.host</name>
<value>192.168.43.66</value> //主机地址(修改为自己的主机ip)
<description>Bind host on which to run the HiveServer2 Thrift interface.
Can be overridden by setting $HIVE_SERVER2_THRIFT_BIND_HOST</description>
</property>
<property>
<name>hive.server2.long.polling.timeout</name>
<value>5000</value>
<description>Time in milliseconds that HiveServer2 will wait, before responding to asynchronous calls that use long polling</description>
</property>
</configuration>
三、使用beeline/hiveserver2连接hive
1.启动hadoop,用jps查看进程
2.输入hiveserver2,等待一会儿,打开一个新的终端(出现一个新的进程RunJar)
3.在新的终端输入beeline
4.进行连接( !connect jdbc:hive2://192.168.43.66:10000)(其中192.168.43.66为自己虚拟机的ip地址)
箭头所指的两个地方是在上述过程 一.2.(2) 中配置hive-site.xml提到的用户名和密码(修改为自己的用户名以及密码)
出现这样界面,就是连接成功。
下面是使用eclipe进行连接测试。
首先需要向新建项目导入jar包
连接mysql的jar包(mysql-connector-java-5.1.44-bin.jar);hadoop中share/hadoop/common/lib下所有的jar包;hadoop中share/hadoop/common下hadoop-common-2.7.7.jar
以及hive中lib下所有的jar包
测试代码:(因为我已经有了表data以及数据,就以查找为例)
package com.jdbc.hive.test;
import java.sql.*;
import java.sql.SQLException;
public class JDBCHive {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
Connection con = DriverManager.getConnection("jdbc:hive2://192.168.43.66:10000/hive", "hive", "hive");//后两个参数是用户名密码
if(con==null)
System.out.println("连接失败");
else {
Statement stmt = con.createStatement();
String sql = "select * from data ";
//String sql = "select id,count(*) total from data group by id order by total desc limit 10";
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
while (res.next()) {
//System.out.print(res.getString(1)+" ");
//System.out.println(res.getString("total"));
System.out.println(res.getString(1)); //输出第一列的数据
}
}
}
}
结果展示如下