在做大数据开发时,为了测试性能等,需要上千万,甚至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 = connect