如何使用java代码通过JDBC连接Hive(附github源码)

2018-04-01 19:35:54 浏览数 (1)

温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。

1.文档编写目的


前面我们讲过《如何使用java代码通过JDBC连接Impala(附Github源码)》,本篇文章主要讲述如何使用Java代码通过JDBC的方式连接Hive。

  • 内容概述

1.环境准备

2.非Kerberos及Kerberos环境连接示例

  • 测试环境

1.Kerberos集群CDH5.11.2,OS为Redhat7.2

2.非Kerberos集群CDH5.13,OS为CentOS6.5

  • 前置条件

1.集群已安装HiveServer2服务正常

2.Kerberos和非Kerberos集群HiveServer2服务正常

2.环境准备


1.创建Java工程jdbcdemo

2.添加Maven依赖

代码语言:txt复制
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>
    <version>2.6.5</version>
</dependency>
<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>1.1.0</version>
</dependency>

3.非Kerberos环境


1.示例代码

代码语言:txt复制
private static String JDBC_DRIVER = "org.apache.hive.jdbc.HiveDriver";
private static String CONNECTION_URL ="jdbc:hive2://54.251.129.76:10000/";

static {
    try {
        Class.forName(JDBC_DRIVER);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {
    System.out.println("通过JDBC连接非Kerberos环境下的HiveServer2");
    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环境下的Hive需要准备Kerberos的krb5.conf文件及keytab文件。

注意:Fayson的测试环境是AWS,所以将kdc和HiveServer2服务的IP地址修改为相应的外网地址,JDBC的URL后Kerberos信息是连接的HiveServer2服务对应Prinical信息。

  • krb5.conf配置,直接将集群的krb5.conf文件拷贝至本地开发环境下
代码语言:txt复制
# 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 = "org.apache.hive.jdbc.HiveDriver";
private static String CONNECTION_URL ="jdbc:hive2://54.179.148.25:10000/;principal=hive/ip-172-31-22-86.ap-southeast-1.compute.internal@CLOUDERA.COM";

static {
    try {
        Class.forName(JDBC_DRIVER);

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) throws Exception {
    //登录Kerberos账号
    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");

    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));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        JDBCUtils.disconnect(connection, rs, ps);
    }
}

2.示例代码运行

GitHub源码地址:

https://github.com/javaxsky/cdhproject

为天地立心,为生民立命,为往圣继绝学,为万世开太平。

温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。

0 人点赞