Java SDK接口
更新时间 2026-10-12 11:43:55
最近更新时间: 2026-10-12 11:43:55
本章节介绍Java SDK接口。
Java SDK 示例
首先向工程项目pom 文件中添加依赖:
<dependencies>
<!-- Hadoop 依赖 -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>${hadoop.version}</version>
</dependency>
<!-- Hive依赖 -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-standalone-metastore</artifactId>
<version>${hive.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-common</artifactId>
<version>${hive.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>${hive.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>${hive.version}</version>
</dependency>
</dependencies>说明
上述所有的依赖必须使用翼MR集群的包
kerberos 验证代码如下:
private static void handleKerberos(HiveConf hiveConf, String keytab, String keytabPrincipal) throws Exception {
LOG.info("CONFIG: hive.server2.authentication -> " + hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_AUTHENTICATION));
// 如果没有启用Kerberos认证则给出警告
if (!hiveConf.getBoolVar(HiveConf.ConfVars.METASTORE_USE_THRIFT_SASL)) {
LOG.warn("kerberos is not used");
return;
}
// 认证Kerberos
System.setProperty("java.security.krb5.conf", "/etc/krb5.conf");
System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
UserGroupInformation.setConfiguration(hiveConf);
LOG.info("UserGroupInformation.loginUserFromKeytab keytabPrincipal ->" + keytabPrincipal + " keytab -> " + keytab);
UserGroupInformation.loginUserFromKeytab(keytabPrincipal, keytab);
}参数说明:1、keytab:给定用户的 keytab 文件路径;2、keytabPrincipal:对应的 principal。
获取 Hive Metastore 客户端:
/**
* 获取 Hive 对象
*
* @param hiveConf Hive 配置信息
* @return Hive 对象
* @throws HiveException
*/
public static IMetaStoreClient getHiveClient(HiveConf hiveConf) throws Exception {
try {
IMetaStoreClient hmsClient = RetryingMetaStoreClient.getProxy(hiveConf, false);
return hmsClient;
} catch (Exception e) {
LOG.error("setHiveMetaStoreConf error", e);
throw e;
}
}获取所有的数据库名称:
/**
* 获取Hive数据库中所有的库名称
*
* @param hmsClient Metastore客户端
* @return 库名称列表
* @throws TException
*/
public static List<String> getDatabases(IMetaStoreClient hmsClient) throws TException {
return hmsClient.getAllDatabases();
}获取指定数据库中所有的表名称:
/**
* 获取指定数据库中所有的表名称
*
* @param hmsClient Metastore客户端
* @param dbName 数据库名称
* @return 表名称列表
* @throws TException
*/
public static List<String> getTables(IMetaStoreClient hmsClient, String dbName) throws TException {
return hmsClient.getAllTables(dbName);
}与HiveServer2 交互并进行查询的流程如下:
一个简单的示例代码如下:
public class JDBCExample {
// Hive JDBC 驱动的类名
private static final String HIVE_DRIVER = "org.apache.hive.jdbc.HiveDriver";
// 使用 SLF4J 日志记录器
private static final Logger LOG = LoggerFactory.getLogger(JDBCExample.class);
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// 获取 Hive 配置信息
// 默认会搜寻 $HIVE_HOME/conf/hive-site.xml 以及其他资源文件
HiveConf hiveConf = HiveUtils.getHiveConf();
// 处理 Kerberos 认证
try {
HiveUtils.handleKerberos(hiveConf);
} catch (Exception e) {
// 如果 Kerberos 认证失败,记录错误日志并退出
LOG.error("Kerberos authentication failed, due to {} ", e.getMessage(), e);
return;
}
// 加载 Hive JDBC 驱动类
Class.forName(HIVE_DRIVER);
Connection connection = null;
// JDBC 连接 URL,用户名和密码
String url = "jdbc:hive2://<hiveserver2_url>"; // 替换为实际的 HiveServer2 URL
String usr = "hive"; // 用户名
String password = ""; // 密码
// 尝试建立与 HiveServer2 的连接
try {
connection = DriverManager.getConnection(url, usr, password);
} catch (SQLException e) {
// 如果连接失败,记录错误日志并退出
LOG.error("Fail to connect HiveServer2: {}, due to {}", url, e.getMessage(), e);
return;
}
// 创建用于执行 SQL 查询的 Statement 对象
Statement stmt = connection.createStatement();
ResultSet resultSet = null;
// 执行 SQL 查询并捕获结果
try {
resultSet = stmt.executeQuery("SELECT * FROM example limit 10");
} catch (Exception e) {
// 如果执行查询失败,记录错误日志
LOG.error("Fail to execute, due to {}", e.getMessage(), e);
}
// 如果结果集为空,记录日志并退出
if (resultSet == null) {
LOG.info("Unable to retrieve any data");
return;
}
// 获取结果集的列数
int columns = resultSet.getMetaData().getColumnCount();
int rowIndex = 1;
// 遍历结果集的每一行
while (resultSet.next()) {
StringBuilder sb = new StringBuilder();
// 遍历每一列并将值添加到 StringBuilder
for (int i = 1; i <= columns; i++) {
String row = resultSet.getString(i);
sb.append(row);
// 在列之间添加制表符作为分隔符
if (i != columns) {
sb.append(' ');
}
}
// 记录结果集中的当前行
LOG.info("{} %{}", rowIndex++, sb);
}
}
}说明
示例中的URL、用户名等请替换为实际集群信息;日志框架使用 SLF4J,工程中需自行引入相关依赖。