温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。
1.文档编写目的
访问Impala的方式很多(如:impala-shell、ODBC、JDBC、Beeline),也可以通过Hue的来访问。关于Beeline方式连接Impala可以参考前面的《如何使用Beeline连接Impala》,本篇文章主要讲述如何使用JAVA代码通过JDBC的方式连接Kerberos和非Kerberos环境下的Impala。
- 内容概述
1.环境准备
2.非Kerberos及Kerberos环境连接示例
- 测试环境
1.Kerberos集群CDH5.11.2,OS为Redhat7.2
2.非Kerberos集群CDH5.13,OS为CentOS6.5
3.Impala JDBC驱动版本2.5.41.1061
- 前置条件
1.Kerberos和非Kerberos集群Impala服务正常
2.环境准备
1.下载Impala JDBC驱动包
代码语言:txt复制https://downloads.cloudera.com/connectors/impala_jdbc_2.5.41.1061.zip
2.创建Java工程jdbcdemo
创建工程时注意加入Hadoop的依赖包
代码语言:txt复制<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.6.5</version>
</dependency>
将下载的Impala驱动包添加到jdbcdemo工程lib目录下,并加载到环境变量
3.非Kerberos环境
1.示例代码
代码语言:txt复制private static String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
private static String CONNECTION_URL ="jdbc:impala://54.255.237.128: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);
}
}
2.示例代码运行
4.Kerberos环境
在Kerberos环境下的Impala需要准备Kerberos的krb5.conf文件及keytab文件。
注意:Fayson的测试环境是AWS,所以将kdc和Impala Daemon服务的IP地址修改为外网地址。JDBC URL后Kerberos信息是连接的Impala Daemon服务对应Prinical信息。
- krb5.conf配置,直接将集群的krb5.conf文件拷贝至本地开发环境下
# Configuration snippets may be placed in this directory as well
includedir /etc/krb5.conf.d/
[logging]
default = FILE:/var/log/krb5libs.log
kdc = FILE:/var/log/krb5kdc.log
admin_server = FILE:/var/log/kadmind.log
[libdefaults]
dns_lookup_realm = false
ticket_lifetime = 24h
renew_lifetime = 7d
forwardable = true
rdns = false
default_realm = CLOUDERA.COM
#default_ccache_name = KEYRING:persistent:%{uid}
[realms]
CLOUDERA.COM = {
kdc = 54.179.148.25
admin_server = 54.179.148.25
}
keytab文件生成
代码语言:txt复制[ec2-user@ip-172-31-22-86 ~]$ sudo kadmin.local
Authenticating as principal hive/admin@CLOUDERA.COM with password.
kadmin.local: xst -norandkey -k fayson.keytab fayson@CLOUDERA.COM
1.示例代码
代码语言:txt复制private static String JDBC_DRIVER = "com.cloudera.impala.jdbc41.Driver";
private static String CONNECTION_URL = "jdbc:impala://13.229.60.149:21050/default;AuthMech=1;KrbRealm=CLOUDERA.COM;KrbHostFQDN=ip-172-31-26-102.ap-southeast-1.compute.internal;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", "/Volumes/Transcend/keytab/krb5.conf");
Configuration configuration = new Configuration();
configuration.set("hadoop.security.authentication" , "Kerberos");
UserGroupInformation. setConfiguration(configuration);
UserGroupInformation.loginUserFromKeytab("fayson@CLOUDERA.COM", "/Volumes/Transcend/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();
}
}
具体JDBC的URL参数说明参考:
http://www.cloudera.com/documentation/other/connectors/impala-jdbc/latest/Cloudera-JDBC-Driver-for-Impala-Install-Guide.pdf
2.示例代码运行
GitHub源码地址:
https://github.com/javaxsky/cdhproject
为天地立心,为生民立命,为往圣继绝学,为万世开太平。
温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。