Apache Hive 是一个数据仓库基础架构,它提供了数据汇总,查询和分析的能力。在 Hive 中,大部分的工作是通过 SQL 语句来完成的,但 Hive 也提供了 Java API,使得开发者可以在 Java 程序中与 Hive 交互。
下面是一些常用的 Java 代码片段,用于通过 Hive 的 JDBC 接口与 Hive 交互:
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;
public class HiveJdbcClient {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
//replace "hive" here with the name of the user the queries should run as
Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "hive", "");
Statement stmt = con.createStatement();
String tableName = "testHiveTable";
stmt.execute("drop table if exists " + tableName);
stmt.execute("create table " + tableName + " (key int, value string)");
// show tables
String sql = "show tables '" + tableName + "'";
System.out.println("Running: " + sql);
ResultSet res = stmt.executeQuery(sql);
if (res.next()) {
System.out.println(res.getString(1));
}
// describe table
sql = "describe " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1) + "\t" + res.getString(2));
}
// load data into table
// NOTE: file is in HDFS, NOT local file system.
// NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
String filepath = "/tmp/a.txt";
sql = "load data local inpath '" + filepath + "' into table " + tableName;
System.out.println("Running: " + sql);
stmt.execute(sql);
// select * query
sql = "select * from " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
}
// regular hive query
sql = "select count(1) from " + tableName;
System.out.println("Running: " + sql);
res = stmt.executeQuery(sql);
while (res.next()) {
System.out.println(res.getString(1));
}
}
}
这个 Java 示例展示了如何使用 Hive 的 JDBC 驱动来连接 Hive,执行 SQL 语句,并处理结果。你需要根据你的环境调整 Hive 的地址、端口、用户和密码等信息。
注意:这个示例假设 Hive 服务正在运行,并且可以通过 JDBC 连接。此外,这个示例中的代码没有包含任何错误处理或资源管理,这在实际的生产环境中是必需的。例如,你应该在 finally 块中关闭你的 Statement 和 Connection,或者使用 try-with-resources 语句来自动关闭它们。你还应该捕获并适当处理可能抛出的任何 SQLException。
在使用此代码之前,确保已经在项目中包含了 Hive 的 JDBC 驱动依赖。如果你的项目是 Maven 项目,可以在 pom.xml 文件中添加如下依赖:
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>3.1.2</version> <!-- 请根据你的 Hive 版本调整这个值 -->
</dependency>