java clickhouse 每15分钟批量插入随机数据
时间: 2023-12-06 15:45:45 浏览: 102
java实现jdbc批量插入数据
你可以使用 Java 的 ClickHouse JDBC 驱动程序来连接 ClickHouse 数据库,并使用 Java 生成随机数据并批量插入到 ClickHouse 数据库中。以下是一个示例代码,每15分钟将1000条随机数据批量插入到 ClickHouse 中:
```
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Random;
public class ClickHouseBatchInsert {
private static final String DB_URL = "jdbc:clickhouse://localhost:8123/default";
private static final String DB_USER = "default";
private static final String DB_PASSWORD = "";
private static final int BATCH_SIZE = 1000;
public static void main(String[] args) throws SQLException, InterruptedException {
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
conn.setAutoCommit(false);
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO test (col1, col2, col3) VALUES (?, ?, ?)");
Random rand = new Random();
while (true) {
for (int i = 0; i < BATCH_SIZE; i++) {
pstmt.setInt(1, rand.nextInt(100));
pstmt.setString(2, "str" + rand.nextInt(100));
pstmt.setDouble(3, rand.nextDouble());
pstmt.addBatch();
}
pstmt.executeBatch();
conn.commit();
Thread.sleep(15 * 60 * 1000); // 15 minutes
}
}
}
```
请注意,此示例代码中的表名为 `test`,列名为 `col1`、`col2` 和 `col3`,您需要将其替换为您实际使用的表名和列名。
阅读全文