hive的JDBC模式 : HIVE版本: apache-hive-0.14.0-bin
JAVA API交互执行方式 hive 远程服务 (端口号10000) 启动方式
hive --service hiveserver2
驱动:
org.apache.hive.jdbc.HiveDriver
在java代码中调用hive的JDBC建立连接
1、配置hive-site.xml, 设置mysql数据库连接
javax.jdo.option.ConnectionURL
jdbc:mysql://192.168.1.225:3307/hive?createDatabaseIfNotExist=true
javax.jdo.option.ConnectionDriverName
com.mysql.jdbc.Driver
javax.jdo.option.ConnectionUserName
root
javax.jdo.option.ConnectionPassword
root
hive.querylog.location
/opt/apache-hive-0.14.0-bin/tmp
hive.exec.local.scratchdir
/opt/apache-hive-0.14.0-bin/tmp
hive.downloaded.resources.dir
/opt/apache-hive-0.14.0-bin/tmp
2、启动hive远程服务
hive --service hiveserver2 >/dev/null 2>/dev/null &
3、java连接hive
package com.chb.hiveJdbcUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class HiveJdbcUtils {
public static void main(String[] args) {
Connection conn = getConnection();
try {
PreparedStatement ps = conn.prepareStatement("show tables");
ResultSet rs = ps.executeQuery();
while(rs.next()) {
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = null;
String ip = "192.168.1.225";
System.out.println("开始连接。。。");
try {
//Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");早期是使用驱动
Class.forName("org.apache.hive.jdbc.HiveDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
//conn = DriverManager.getConnection("jdbc:hive://"+ip+":10000/default","","");//早期使用jdbc:hive
conn = DriverManager.getConnection(
"jdbc:hive2://"+ip+":10000/default",//查询hive中的数据库
"",
"");
System.out.println("连接成功!");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}