Java---JDBC连接Impala(Kerberos认证)

Java----JDBC连接Impala(Kerberos认证)

  • 环境
  • 两种连接方式
    • 一、Impala的JDBC驱动连接impala
        • 1. 驱动下载
        • 2. 导入Impala的JDBC包
        • 3. 导入Maven外部依赖
        • 3. 主程序
    • 二、Hive的JDBC驱动连接impala
        • 1. 导入Maven外部依赖
        • 2. 主程序
  • 常见问题
      • java.lang.IllegalArgumentException: Can't get Kerberos realm
      • GSS initiate failed

环境

版本
JAVA 8
CDH 6.3
Impala 3.2
Hive 3.0
代码开发环境 Windows10

两种连接方式

说明
HiveJdbc 比较方便一点,直接可以从maven远程仓库下载驱动,用hive的Jdbc驱动连接Impala
ImpalaJdbc Impala自身的Jdbc驱动,但是在maven的远程仓库中没有,需要在官网下载

一、Impala的JDBC驱动连接impala

1. 驱动下载
  • 连接Impala的JDBC驱动在Maven远程仓库中没有,需要到官网下载
  • 这个下载是需要先注册账号的哦!免费注册哒

点击去官网下载:https://www.cloudera.com/search.html?q=impala%20jdbc
Java---JDBC连接Impala(Kerberos认证)_第1张图片

2. 导入Impala的JDBC包
  • 下载解压后,打开文件夹,里面有两个名字类似的文件夹,选择一个即可
    Java---JDBC连接Impala(Kerberos认证)_第2张图片
  • 打开即可看到一个jar包,将其导入到我们的环境
    Java---JDBC连接Impala(Kerberos认证)_第3张图片* 我这里是导入到了idea中
    在这里插入图片描述
3. 导入Maven外部依赖
  • 这里只需要导入一个hadoop的就可以
    <dependencies>
        <dependency>
            <groupId>org.apache.hadoopgroupId>
            <artifactId>hadoop-commonartifactId>
            <version>3.0.0version>
        dependency>
    dependencies>
3. 主程序
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import java.security.PrivilegedAction;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @Date: 2022/1/5
 * @Author: BigBig-Data
 * @Description: 注: impala的JDBC驱动maven远程仓库中没有, 需要从别的地方下载: https://www.cloudera.com/search.html?q=impala%20jdbc
 */
public class IConnectImpalaKerberos {
    // Kerberos
    public static final String KRB5_CONF = "C:/Windows/krb5.ini";//linux中叫 krb5.conf,改后缀即可
    public static final String PRINCIPAL = "[email protected]";
    public static final String KEYTAB = "C:/Windows/normtest.keytab";
    // impala jdbc url 参数可参考官方文档   https://docs.cloudera.com/documentation/other/connectors/impala-jdbc/latest/Cloudera-JDBC-Driver-for-Impala-Install-Guide.pdf
    public static String connectionUrl = "jdbc:impala://host:21050/;AuthMech=1;KrbRealm=HADOOP.COM;KrbHostFQDN=host;KrbServiceName=impala";

    // 从官网下载的jar包
    public static String jdbcDriverName = "com.cloudera.impala.jdbc.Driver";

    public static void main(String[] args) throws Exception {

        System.setProperty("java.security.krb5.conf", KRB5_CONF);
        Configuration conf = new Configuration();
        conf.set("hadoop.security.authentication", "Kerberos");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab(PRINCIPAL, KEYTAB);
        System.out.println(">> 1. Login from keytab " + KEYTAB + " Success");
        UserGroupInformation loginUser = UserGroupInformation.getLoginUser();
        int result = loginUser.doAs(new PrivilegedAction<Integer>() {
            @Override
            public Integer run() {
                int result = 0;
                //加载驱动
                try {
                    Class.forName(jdbcDriverName);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
                System.out.println(">> 2. jdbcDriver load Success");
                try (Connection con = DriverManager.getConnection(connectionUrl)) {
                    System.out.println(">> 3. Login impala Sussecs");
                    Statement stmt = con.createStatement();
                    ResultSet rs = stmt.executeQuery("SELECT count(1) FROM norm_demo");
                    while (rs.next()) {
                        result = rs.getInt(1);
                    }
                    stmt.close();
                    con.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return result;
            }
        });
        System.out.println("表行数: "+ result);
    }

}

二、Hive的JDBC驱动连接impala

1. 导入Maven外部依赖
    <dependencies>
        <dependency>
            <groupId>org.apache.hadoopgroupId>
            <artifactId>hadoop-commonartifactId>
            <version>3.0.0version>
        dependency>
        <dependency>
            <groupId>org.apache.hivegroupId>
            <artifactId>hive-jdbcartifactId>
            <version>1.2.1version>
        dependency>
    dependencies>
2. 主程序
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class HConnectImpalaKerberos {
    // Kerberos
    public static final String KRB5_CONF = "C:/Windows/krb5.ini"; //linux中krb5.conf
    public static final String PRINCIPAL = "[email protected]";
    public static final String KEYTAB = "C:/Windows/normtest.keytab";
    public static String connectionUrl = "jdbc:hive2://host:21050/;principal=impala/[email protected]";
    public static String jdbcDriverName = "org.apache.hive.jdbc.HiveDriver";

    public static void main(String[] args) throws Exception {
        System.setProperty("java.security.krb5.conf", KRB5_CONF);
        Configuration conf = new Configuration();
        conf.set("hadoop.security.authentication", "Kerberos");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab(PRINCIPAL, KEYTAB);
        System.out.println(">> 1. Login from keytab " + KEYTAB + " Success");

        //加载驱动
        Class.forName(jdbcDriverName);
        System.out.println(">> 2. jdbcDriver load Success");
        try (Connection con = DriverManager.getConnection(connectionUrl)) {
            System.out.println(">> 3. Login impala Sussecs");
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT count(1) FROM norm_demo");
            while (rs.next()) {
                System.out.println(rs.getInt(1));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

常见问题

java.lang.IllegalArgumentException: Can’t get Kerberos realm

一般是因为没有找到配置文件
如果是windows,将 krb5.ini 和 xxx.keytab 需要放在 C:\Windows\ 路径下,记得改后缀
如果是linux,将 krb5.conf 和 xxx.keytab 需要放在 /etc/ 路径下

GSS initiate failed

kerberos认证成功后,连接impala报这个错误
一般可能是因为url填写错误了,请仔细检查url中的参数等
下图为官方文档示例:
Java---JDBC连接Impala(Kerberos认证)_第4张图片

你可能感兴趣的:(java,hive,impala,jdbc,kerberos)