1. 程式人生 > >springboot 1.5.6 + jdbc使用kerberos認證方式連接hive

springboot 1.5.6 + jdbc使用kerberos認證方式連接hive

hive連接 cati ica trace container unable rac try manage

一、POM文件引入的依賴如下:

1、此處hadoop-common和hive-jdbc都需要排除servlet-api,否則將出現springboot啟動報錯,例如:‘unable to start embedded tomcat‘ 或者 ‘A child container failed during start‘

2、使用hive-jdbc其它版本並執行第二點中的代碼時,可能出現protocl-client過時並連接不上的問題

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-common</artifactId>
    <version>2.6.0</version>
    <exclusions>
        <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>1.1.0</version>
    <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.eclipse.jetty.orbit</groupId>
            <artifactId>*</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.eclipse.jetty.aggregate</groupId>
            <artifactId>*</artifactId>
        </exclusion>
        <exclusion>
            <groupId>tomcat</groupId>
            <artifactId>*</artifactId>
        </exclusion>
        <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.mortbay.jetty</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>

二、JDBC連接的代碼

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;

import java.sql.*;

public class Test {

    public static void main(String[] args) throws Exception {
        // 解決windows中執行可能出現讀不到krb5配置文件的問題
        System.setProperty("java.security.krb5.conf", "C:/kerberos/test/config/krb5.conf");
        
// 解決window中執行可能出現找不到HADOOP_HOME或hadoop.home.dir問題 System.setProperty("hadoop.home.dir", "C:\\Program Files\\winutils-master\\hadoop-2.8.3"); // Kerberos認證 Configuration configuration = new Configuration(); configuration.set("hadoop.security.authentication", "Kerberos"); configuration.set(
"keytab.file" , "C:/kerberos/test/config/hadoop.keytab" ); configuration.set("kerberos.principal" , "[email protected]" ); UserGroupInformation.setConfiguration(configuration); UserGroupInformation.loginUserFromKeytab("[email protected]", "C:/kerberos/test/config/hadoop.keytab"); // 創建hive連接 Connection connection = null; ResultSet rs = null; PreparedStatement ps = null; try { Class.forName("org.apache.hive.jdbc.HiveDriver"); connection = DriverManager.getConnection("jdbc:hive2://bdpnode1.domain.com:10000/;principal=hive/[email protected]COM","hadoop",""); if (null != connection) { ps = connection.prepareStatement("SELECT * FROM test"); rs = ps.executeQuery(); while (rs.next()) { System.out.println(rs.getInt(1)); } } } catch (Exception e) { e.printStackTrace(); } finally { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (connection != null) { connection.close(); } } } }

springboot 1.5.6 + jdbc使用kerberos認證方式連接hive