jdbc批量写入impala数据库
时间: 2023-05-16 09:07:20 浏览: 155
可以使用ImpalaJDBC驱动程序来实现JDBC批量写入Impala数据库。以下是一个示例代码片段:
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class ImpalaBatchInsert {
public static void main(String[] args) throws SQLException {
String impalaUrl = "jdbc:impala://<impala-host>:<impala-port>/<database>";
String impalaUser = "<impala-user>";
String impalaPassword = "<impala-password>";
Connection conn = DriverManager.getConnection(impalaUrl, impalaUser, impalaPassword);
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO mytable (col1, col2) VALUES (?, ?)");
for (int i = 0; i < 1000; i++) {
pstmt.setInt(1, i);
pstmt.setString(2, "value" + i);
pstmt.addBatch();
}
pstmt.executeBatch();
pstmt.close();
conn.close();
}
}
```
这个示例代码使用了ImpalaJDBC驱动程序来连接Impala数据库,并使用PreparedStatement对象来执行批量插入操作。在循环中,我们设置了两个参数的值,并将它们添加到批量中。最后,我们调用executeBatch()方法来执行批量插入操作。
阅读全文