美文网首页大数据
Impala--通过jdbc连接(非kerberos/kerbe

Impala--通过jdbc连接(非kerberos/kerbe

作者: 撸码小丑 | 来源:发表于2018-12-03 16:58 被阅读54次

    前期准备

    1.下载Impala JDBC驱动包

    https://downloads.cloudera.com/connectors/impala_jdbc_2.5.41.1061.zip

    2.创建maven工程
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.6.5</version>
    </dependency>
    

    将下载的Impala驱动包添加到工程lib目录下,并加载到环境变量

    非kerberos环境

    private static String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
    private static String CONNECTION_URL ="jdbc:impala://hadoop1:21050/";
    
    static {
        try {
            Class.forName(JDBC_DRIVER);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        Connection connection = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        try {
            connection = DriverManager.getConnection(CONNECTION_URL);
            ps = connection.prepareStatement("select * from test_table");
            rs = ps.executeQuery();
            while (rs.next()) {
                System.out.println(rs.getInt(1) + "-------" + rs.getString(2));
            }
    
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.disconnect(connection, rs, ps);
        }
    }
    

    Kerberos

    在Kerberos环境下的Impala需要准备Kerberos的krb5.conf文件及keytab文件。

    private static String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
    private static String CONNECTION_URL = "jdbc:impala://haodop1:21050/default;AuthMech=1;KrbRealm=HADOOP.COM;KrbHostFQDN=hadoop1;KrbServiceName=impala";
    
    static {
        try {
            Class.forName(JDBC_DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        //登录Kerberos账号
        try {
            System.setProperty("java.security.krb5.conf", "/home/xiet/keytab/krb5.conf");
            Configuration configuration = new Configuration();
            configuration.set("hadoop.security.authentication" , "Kerberos");
            UserGroupInformation. setConfiguration(configuration);
            UserGroupInformation.loginUserFromKeytab("xiet@HADOOP.COM", "/home/xiet/keytab/fayson.keytab");
            System.out.println(UserGroupInformation.getCurrentUser() + "------" + UserGroupInformation.getLoginUser());
    
            UserGroupInformation loginUser = UserGroupInformation.getLoginUser();
    
            loginUser.doAs(new PrivilegedAction<Object>(){
    
                public Object run() {
                    Connection connection = null;
                    ResultSet rs = null;
                    PreparedStatement ps = null;
                    try {
                        Class.forName(JDBC_DRIVER);
                        connection = DriverManager.getConnection(CONNECTION_URL);
                        ps = connection.prepareStatement("select * from test_table");
                        rs = ps.executeQuery();
                        rs = ps.executeQuery();
                        while (rs.next()) {
                            System.out.println(rs.getInt(1));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        JDBCUtils.disconnect(connection, rs, ps);
                    }
                    return null;
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

    Sentry

     private static String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
    private static String CONNECTION_URL =" jdbc:impala://hadoop1:21050/default;AuthMech=3;UID=hive;PWD=;UseSasl=0";
        static {
            try {
                Class.forName(JDBC_DRIVER);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            System.out.println("集群中只启用了Sentry服务通过JDBC访问Impala");
            Connection connection = null;
            ResultSet rs = null;
            PreparedStatement ps = null;
            try {
                connection = DriverManager.getConnection(CONNECTION_URL);
                ps = connection.prepareStatement("show databases");
                rs = ps.executeQuery();
                while (rs.next()) {
                    System.out.println(rs.getString(1));
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                JDBCUtils.disconnect(connection, rs, ps);
            }
        }
    

    LDAP和Sentry

    private static String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
        private static String CONNECTION_URL ="jdbc:impala://hadoop1:21050/;AuthMech=3;UID=hive;PWD=123456";
        static {
            try {
                Class.forName(JDBC_DRIVER);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        public static void main(String[] args) {
            System.out.println("通过JDBC连接LDAP和Sentry环境下的Impala");
            Connection connection = null;
            ResultSet rs = null;
            PreparedStatement ps = null;
            try {
                connection = DriverManager.getConnection(CONNECTION_URL);
                ps = connection.prepareStatement("show databases");
                rs = ps.executeQuery();
                while (rs.next()) {
                    System.out.println(rs.getString(1));
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                JDBCUtils.disconnect(connection, rs, ps);
            }
        }
    

    相关文章

      网友评论

        本文标题:Impala--通过jdbc连接(非kerberos/kerbe

        本文链接:https://www.haomeiwen.com/subject/xxcafqtx.html