1.配置环境Eclipse和JDK
2.加载hive jar包或者impala jar包
备注:从CDH集群里面拷贝出来
下载地址:https://www.cloudera.com/downloads/connectors/impala/jdbc/2-5-5.html
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.jdbc41.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"; //jdbc:impala://localhost:21050;AuthMech=3;UID=UserName;PWD=Password //AuthMech:0-不需要密码,1-krb验证,2-用户名验证,3-用户名和密码验证 //SSL:0-不连接SSL协议,1-连接SSL协议 UseSasl=0 //String url = "jdbc:impala://10.0.4.142:21050/touna_finance;AuthMech=2;SSL=0;UID=test;PWD=test"; //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); 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(); } }