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認證java
代碼以下:sql
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 認證apache
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(); } } }