HIVE 批量插入 java
时间: 2024-01-14 11:21:44 浏览: 156
以下是使用Java实现Hive批量插入数据的示例代码:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class HiveBatchInsert {
public static void main(String[] args) {
// JDBC连接信息
String driverName = "org.apache.hive.jdbc.HiveDriver";
String url = "jdbc:hive2://localhost:10000/default";
String username = "hive";
String password = "";
// 批量插入数据
try {
Class.forName(driverName);
Connection connection = DriverManager.getConnection(url, username, password);
PreparedStatement statement = connection.prepareStatement("INSERT INTO table_name VALUES (?, ?)");
// 设置批量插入的参数
statement.setString(1, "value1");
statement.setInt(2, 100);
statement.addBatch();
statement.setString(1, "value2");
statement.setInt(2, 200);
statement.addBatch();
statement.setString(1, "value3");
statement.setInt(2, 300);
statement.addBatch();
// 执行批量插入
statement.executeBatch();
// 关闭连接
statement.close();
connection.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
```
请注意,上述代码中的`table_name`需要替换为实际的表名,同时需要根据实际情况修改JDBC连接信息。
阅读全文