java连接hive代码
时间: 2023-10-24 09:21:05 浏览: 91
以下是Java连接Hive的示例代码:
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class HiveJdbcClient {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws Exception {
// Register driver and create connection
Class.forName(driverName);
Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", "");
// Execute query
Statement stmt = con.createStatement();
String query = "SELECT * FROM my_table";
ResultSet rs = stmt.executeQuery(query);
// Process result set
while (rs.next()) {
System.out.println(rs.getString("column1") + "\t" + rs.getString("column2"));
}
// Close connection
rs.close();
stmt.close();
con.close();
}
}
```
请注意,您需要将`driverName`设置为正确的Hive JDBC驱动程序名称,并将连接字符串中的主机名和端口号更改为您的Hive服务器的实际值。您还需要替换查询字符串中的`my_table`与您要查询的表名。
阅读全文