java批量生成海量测试数据及用ChatGPT提示语一键生成的方法
在做大数据开发时,为了测试性能等,需要上千万,甚至TB或PB级别的,在测试环境可能没有那么多数据,这时可以考虑进行造测试数据。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Random;
public class TestDataGenerator {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/your_database";
String username = "your_username";
String password = "your_password";
int batchSize = 1000; // 每批次插入的数据量
int totalRecords = 1000000; // 总共要生成的数据量
try {
Connection connection = DriverManager.getConnection(url, username, password);
connection.setAutoCommit(false);
String insertQuery = "INSERT INTO test (id, callid, type, ...其他列...) VALUES (?, ?, ?, ...其他值...)";
PreparedStatement preparedStatement = connection.prepareStatement(insertQuery);
Random random = new Random();
for (int i = 1; i <= totalRecords; i++) {
// 设置每个字段的值,根据表结构设置对应的数据生成逻辑
preparedStatement.setLong(1, i);
preparedStatement.setString(2, "CallSheet" + i);
preparedStatement.setString(3, "Type" + (random.nextInt(5) + 1));
// 设置其他字段的值...
preparedStatement.addBatch();
if (i % batchSize == 0) {
preparedStatement.executeBatch();
connection.commit();
}
}
preparedStatement.executeBatch();
connection.commit();
preparedStatement.close();
connection.close();
System.out.println("测试数据生成完成!");
} catch (SQLException e) {
e.printStackTrace();
}
}
请将上述示例中的数据库连接信息和插入逻辑根据您的数据库设置和表结构进行相应的修改。此程序将会在数据库中插入海量测试数据。
更方便的方法是在ChatGPT等大模型,输入下面提示语:
根据下面的表结构,生成100万的测试数据,给出详细的java实现代码或存储过程代码:【表结构】
亲测在New Bing是可以生成可以运行的代码。