Hive记录-Impala jdbc连接hive和kudu参考

1.配置环境Eclipse和JDK

2.加载hive jar包或者impala jar包

3.源代码参考

/*
 * 1.配置好hive+sentry+impala
 * 2.hive配置sentry-site.xml加入属性/值:sentry.hive.testing.mode/true
 * 3.部署客户端配置,重启组件
 * 4.新建linux用户和组:test,test,并加入组test,设置密码为test
 * 5.beeline hive用户登录,创建角色test_role
 * 6.授权查询库给角色test_role,将角色授权给test
 * 7.beeline hive连接登录测试
 * 8.impala-shell连接登录测试
 * 9.show current roles、show grant role test_role、show create table test
 * 10.测试代码,返回查询结果
 */

package com.impala;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class ImpalaJdbcTest {  
    public static Connection getConnection() throws ClassNotFoundException, SQLException{
        String driver = "org.apache.hive.jdbc.HiveDriver";
        //String driver = "com.cloudera.impala.jdbc4.Driver";
        //auth=noSasl不使用Kerberos身份验证的群集执行此操作
        //impala Daemon HiveServer2 端口-21050
        //String url = "jdbc:hive2://10.0.4.142:21050/default;auth=noSasl";
        String url = "jdbc:hive2://10.0.4.142:21050/touna_finance;auth=noSasl";
        //String url = "jdbc:impala://10.0.4.142:21050/default";
        //String url = "jdbc:hive2://10.0.4.142:21050/default";
        String username = "test";
        String password = "test";
        Connection conn = null;
        Class.forName(driver);
        conn = (Connection) DriverManager.getConnection(url,username,password);
        return conn;
    }
    public void select() throws ClassNotFoundException, SQLException{
        Connection conn = getConnection();
        String sql = "select * from tn_fms_task_record limit 2";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        int col = rs.getMetaData().getColumnCount();
        System.out.println("=====================================");
        while (rs.next()){
            for(int i=1;i<=col;i++){
                System.out.print(rs.getString(i)+"\t");
            }
            System.out.print("\n");
        }
        System.out.println("=====================================");
    }
    public static void main(String[] args) throws SQLException, ClassNotFoundException {  
        ImpalaJdbcTest hiveJdbcClient = new ImpalaJdbcTest();  
        hiveJdbcClient.select();
    }  
}  

猜你喜欢

转载自www.cnblogs.com/xinfang520/p/9354466.html