java 通过 jdbc 操作 kerberos 环境下 impala

文章 青牛 ⋅ 于 2021-05-29 18:03:57 ⋅ 654 阅读

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hainiu</groupId>
    <artifactId>ImpalaJDBC</artifactId>
    <version>1.0</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-server</artifactId>
            <version>2.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.phoenix</groupId>
            <artifactId>phoenix-core</artifactId>
            <version>5.0.0-HBase-2.0</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

    </dependencies>
</project>

kerberos工具类

package Utils;

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

import java.io.IOException;

/**
 * @author : xiniu
 * @date : 2021/5/29
 * @describe :kerberos认证工具类
 */
public class KerberosAuthenticator {
    /**
     * kerberos认证
     * @param configuration hadoop配置文件
     * @param krb5Path kerberos配置文件(krb5.conf)路径
     * @param principal 认证主体名
     * @param keytabPath keytab文件路径
     */
    public void kerberosAuth(Configuration configuration, String krb5Path, String principal, String keytabPath){
        System.setProperty("java.security.krb5.conf",krb5Path);
        configuration.set("hadoop.security.authentication","Kerberos");
        UserGroupInformation.setConfiguration(configuration);
        try {
            UserGroupInformation.loginUserFromKeytab(principal,keytabPath);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

配置文件

准备krb5配置文件、keytab配置文件、hadoop配置文件如下

file

操作代码

import Utils.KerberosAuthenticator;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.log4j.Logger;

import java.security.PrivilegedAction;
import java.sql.*;

/**
 * @author : xiniu
 * @date : 2021/5/29
 * @describe :kerberos环境下通过jdbc操作impala
 */
public class ImpalaJDBC {
    public static void main(String[] args) {
        Logger logger = Logger.getLogger(ImpalaJDBC.class);

        //kerberos conf
        Configuration configuration = new Configuration();
        String krb5path = "src/main/conf/krb5.conf";
        String keytabpath = "src/main/conf/impala.keytab";
        String principal = "impala@HAINIU.COM";

        //JDBC
        String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
        String JDBC_URL = "jdbc:impala://worker-1:21050/default;AuthMech=1;KrbHostFQDN=worker-1;KrbServiceName=impala";

        //kerberos authentication
        KerberosAuthenticator kerberosAuthenticator = new KerberosAuthenticator();
        kerberosAuthenticator.kerberosAuth(configuration,krb5path,principal,keytabpath);

        //connect to impala by kerberos authentication
        try {
            UserGroupInformation loginUser = UserGroupInformation.getLoginUser();
            loginUser.doAs(new PrivilegedAction<Object>() {
                @Override
                public Object run() {
                    Connection connection = null;
                    ResultSet rs = null;
                    PreparedStatement ps = null;
                    try {
                        Class.forName(JDBC_DRIVER);
                        connection = DriverManager.getConnection(JDBC_URL);
                        ps = connection.prepareStatement("select count(1) from xinniu.hainiu1");
                        rs = ps.executeQuery();
                        while (rs.next()){
                            int anInt = rs.getInt(1);
                            System.out.println("anInt = " + anInt);
                        }
                    } catch (Exception e) {
                        logger.error(e);
                    }finally {
                        if (ps != null){
                            try {
                                ps.close();
                            } catch (Exception e) {
                                logger.error(e);
                            }
                        }
                        if (connection != null){
                            try {
                                connection.close();
                            } catch (Exception e) {
                                logger.error(e);
                            }
                        }
                    }
                    return null;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
版权声明:原创作品,允许转载,转载时务必以超链接的形式表明出处和作者信息。否则将追究法律责任。来自海牛部落-青牛,http://hainiubl.com/topics/75546
点赞
成为第一个点赞的人吧 :bowtie:
回复数量: 0
    暂无评论~~
    • 请注意单词拼写,以及中英文排版,参考此页
    • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
    • 支持表情,可用Emoji的自动补全, 在输入的时候只需要 ":" 就可以自动提示了 :metal: :point_right: 表情列表 :star: :sparkles:
    • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif,教程
    • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
    Ctrl+Enter