Hive JDBC连接hive(2.1.1)(java)
1.Maven 依赖
<dependencies>
<!--hive jdbc依赖 此包有签名需要排除他们-->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.1.1-cdh6.3.0</version>
</dependency>
</dependencies>
2.kerberos 或者LDAP认证
LDAP认证
代码如下:
public class HIveJDBC {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
private static String HIVE_URL = "jdbc:hive2://cdh-master03:25005/zyh;ssl=true;sslTrustStore=D:/cdh/hive/src/main/ssl/cm-auto-global_truststore.jks;";
private static String user = "gree1";
private static String password = "000000";
public static void main(String[] args) throws Exception {
Class.forName(driverName);
Connection conn = DriverManager.getConnection(HIVE_URL, user, password);
Statement st = conn.createStatement();
ResultSet resultSet = st.executeQuery("select * from zhchivetable");
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
}
}
kerberos 认证
import hivekerberosauth.KuduKerberosAuth;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**
* 用于通过hive jdbc 连接hive 测试
*/
public class HIveJDBC {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
private static String CONNECTION_URL ="jdbc:hive2://cdh-master03:25005/;ssl=true;sslTrustStore=D:/cdh/hive/src/main/ssl/cm-auto-global_truststore.jks;principal=hive/cdh-master03@GREE.IO";
public static void main(String[] args) throws Exception {
KuduKerberosAuth.initKerberosENV(false);
Class.forName(driverName);
Connection conn = DriverManager.getConnection(CONNECTION_URL);
Statement st = conn.createStatement();
ResultSet resultSet = st.executeQuery("select * from zyh.zhchivetable");
while (resultSet.next()) {
System.out.println(resultSet.getString(1));
}
}
}
import org.apache.hadoop.security.UserGroupInformation;
public class KuduKerberosAuth {
/**
* 初始化访问Kerberos访问
* @param debug 是否启用Kerberos的Debug模式
*/
public static void initKerberosENV(Boolean debug) {
try {
System.setProperty("java.security.krb5.conf","D:\cdh\hive\src\main\kerberos\krb5.conf");
// System.setProperty("java.security.krb5.conf","/lvm/data3/zhc/krb5.conf");
System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
if (debug){
System.setProperty("sun.security.krb5.debug", "true");
}
UserGroupInformation.loginUserFromKeytab("gree1@GREE.IO", "D:\cdh\hive\src\main\kerberos\gree1.keytab");
// UserGroupInformation.loginUserFromKeytab("gree1@GREE.IO", "/lvm/data3/zhc/gree1.keytab");
System.out.println(UserGroupInformation.getCurrentUser());
} catch(Exception e) {
e.printStackTrace();
}
}
}