远程连接hive数据库,java执行建表语句 (解决方法与步骤)
下面内容仅为某些场景参考,为稳妥起见请先联系上面的专业技术工程师,具体环境具体分析。
2023-09-21 11:25 60
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class HiveTableCreation {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) {
try {
// 连接Hive
Class.forName(driverName);
Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "hive", "");
Statement stmt = con.createStatement();
// 执行建表语句
String tableName = "my_table";
String createTableQuery = "CREATE TABLE " + tableName + " (id INT, name STRING)";
stmt.execute(createTableQuery);
System.out.println("Table created successfully!");
stmt.close();
con.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
System.exit(1);
}
}
}
```
确保你在Pom.xml中添加了正确的Hive JDBC依赖:
```xml
```
如果你仍然遇到问题,请提供具体的错误信息以便我们更好地帮助你解决问题。