JDBC 连接Hive 简单样例(开启Kerberos)
- 运用 Ambari 搭建的HDP 集群,由于开启了kerberos ,对外提供Hive数据时统一用JDBC 的方式,所以写了下面这么一个简单样例供第三方数据接入参考。
代码如下所示:
package com.bmsoft.hive.impl;
import org.apache.hadoop.security.UserGroupInformation;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* 简单的jdbc连接hive实例(已开启kerberos服务)
*/
public class HiveSimple2 {
/**
* 用于连接Hive所需的一些参数设置 driverName:用于连接hive的JDBC驱动名 When connecting to
* HiveServer2 with Kerberos authentication, the URL format is:
* jdbc:hive2://<host>:<port>/<db>;principal=
* <Server_Principal_of_HiveServer2>
*/
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
// 注意:这里的principal是固定不变的,其指的hive服务所对应的principal,而不是用户所对应的principal
private static String url = "jdbc:hive2://bigdata40:10000/admin;principal=hive/bigdata40@BIGDATA.COM";
private static String sql = "";
private static ResultSet res;
public static Connection get_conn() throws SQLException, ClassNotFoundException {
/** 使用Hadoop安全登录 **/
org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration();
conf.set("hadoop.security.authentication", "Kerberos");
if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
// 默认:这里不设置的话,win默认会到 C盘下读取krb5.init
System.setProperty("java.security.krb5.conf", "C:/Windows/krbconf/bms/krb5.ini");
} // linux 会默认到 /etc/krb5.conf 中读取krb5.conf,本文笔者已将该文件放到/etc/目录下,因而这里便不用再设置了
try {
UserGroupInformation.setConfiguration(conf);
UserGroupInformation.loginUserFromKeytab("test2/hdp39@BMSOFT.COM", "./conf/test2.keytab");
} catch (IOException e1) {
e1.printStackTrace();
}
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url);
return conn;
}
/**
* 查看数据库下所有的表
*
* @param statement
* @return
*/
public static boolean show_tables(Statement statement) {
sql = "SHOW TABLES";
System.out.println("Running:" + sql);
try {
ResultSet res = statement.executeQuery(sql);
System.out.println("执行“+sql+运行结果:");
while (res.next()) {
System.out.println(res.getString(1));
}
return true;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 获取表的描述信息
*
* @param statement
* @param tableName
* @return
*/
public static boolean describ_table(Statement statement, String tableName) {
sql = "DESCRIBE " + tableName;
try {
res = statement.executeQuery(sql);
System.out.print(tableName + "描述信息:");
while (res.next()) {
System.out.println(res.getString(1) + "\t" + res.getString(2));
}
return true;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 删除表
*
* @param statement
* @param tableName
* @return
*/
public static boolean drop_table(Statement statement, String tableName) {
sql = "DROP TABLE IF EXISTS " + tableName;
System.out.println("Running:" + sql);
try {
statement.execute(sql);
System.out.println(tableName + "删除成功");
return true;
} catch (SQLException e) {
System.out.println(tableName + "删除失败");
e.printStackTrace();
}
return false;
}
/**
* 查看表数据
*
* @param statement
* @return
*/
public static boolean queryData(Statement statement, String tableName) {
sql = "SELECT * FROM " + tableName + " LIMIT 20";
System.out.println("Running:" + sql);
try {
res = statement.executeQuery(sql);
System.out.println("执行“+sql+运行结果:");
while (res.next()) {
System.out.println(res.getString(1) + "," + res.getString(2) + "," + res.getString(3));
}
return true;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* 创建表
*
* @param statement
* @return
*/
public static boolean createTable(Statement statement, String tableName) {
sql = "CREATE TABLE test_1m_test2 AS SELECT * FROM test_1m_test"; // 为了方便直接复制另一张表数据来创建表
System.out.println("Running:" + sql);
try {
boolean execute = statement.execute(sql);
System.out.println("执行结果 :" + execute);
return true;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args) {
try {
Connection conn = get_conn();
Statement stmt = conn.createStatement();
// 创建的表名
String tableName = "test_100m";
show_tables(stmt);
// describ_table(stmt, tableName);
/** 删除表 **/
// drop_table(stmt, tableName);
// show_tables(stmt);
// queryData(stmt, tableName);
createTable(stmt, tableName);
conn.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("!!!!!!END!!!!!!!!");
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
pom.xml 文件如下所示:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-metastore -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-metastore</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-common -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-common</artifactId>
<version>1.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hive/hive-service -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-service</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.3</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
参考文档:
https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients
文档其中比较值得注意的一点是:
JDBC Client Setup for a Secure Cluster
When connecting to HiveServer2 with Kerberos authentication, the URL format is:
jdbc:hive2://<host>:<port>/<db>;principal=<Server_Principal_of_HiveServer2>
- 1
- 2
- 3
- 这里的principal是固定不变的,其指的hive服务所对应的principal,而不是用户所对应的principal; 对于这里的可以为不存在的数据库,但是如果这么做那么在查询表的时候则需要指出其所在的库(如db.tablename),否则默认会查询所对应的表。