Java中调用CDH集群的hive的示例代码
时间: 2024-09-15 11:05:39 浏览: 61
在Java中,通过Hive JDBC驱动程序连接Apache Hadoop CDH集群上的Hive服务,你可以编写类似这样的示例代码:
```java
import java.sql.*;
public class HiveJdbcExample {
public static void main(String[] args) {
String url = "jdbc:hive2://<cluster_host>:<port>/default;principal=<principal>;auth=<authentication_type>"; // 请替换实际的主机、端口、用户名和权限类型
String username = "<username>";
String password = "<password>";
try {
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("Connected to Hive successfully");
Statement statement = connection.createStatement();
String query = "SELECT * FROM <table_name>"; // 请替换实际表名
ResultSet resultSet = statement.executeQuery(query);
while (resultSet.next()) {
// 处理查询结果
String col1 = resultSet.getString("column1");
int col2 = resultSet.getInt("column2");
System.out.println("Column 1: " + col1 + ", Column 2: " + col2);
}
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
在这个例子中,你需要将`<cluster_host>`、`<port>`、`<principal>`、`<authentication_type>`、`<username>`和`<password>`替换为你CDH集群的实际配置。`<table_name>`也需要替换为你想要查询的Hive表名。
阅读全文
相关推荐

















