多线程的方式的将100万条数据,从一个数据库迁移到另一个数据库

以下是使用Java多线程方式将100万条数据从一个数据库迁移到另一个数据库的示例代码:

import java.sql.*;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;

import java.util.concurrent.TimeUnit;

public class DataMigration {

public static void main(String[] args) {

int batchSize = 10000; // 每个批次的数据量

int totalCount = 1000000; // 要迁移的总数据量

int threadCount = 4; // 线程数

String sourceUrl = "jdbc:mysql://localhost:3306/source_db";

String targetUrl = "jdbc:mysql://localhost:3306/target_db";

String sourceUsername = "root";

String sourcePassword = "password";

String targetUsername = "root";

String targetPassword = "password";

ExecutorService executor = Executors.newFixedThreadPool(threadCount);

try (

Connection sourceConn = DriverManager.getConnection(sourceUrl, sourceUsername, sourcePassword);

Connection targetConn = DriverManager.getConnection(targetUrl, targetUsername, targetPassword);

Statement sourceStmt = sourceConn.createStatement();

PreparedStatement targetStmt = targetConn.prepareStatement("INSERT INTO target_table VALUES (?, ?, ?)")

) {

// 设置源数据库查询的起始位置和每个批次的数据量

int offset = 0;

sourceStmt.setFetchSize(batchSize);

while (offset < totalCount) {

for (int i = 0; i < threadCount; i++) {

// 创建一个线程来处理每个批次的数据

executor.submit(new DataMigrationThread(offset, batchSize, sourceStmt, targetStmt));

offset += batchSize;

}

}

} catch (SQLException e) {

e.printStackTrace();

}

executor.shutdown();

try {

executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

private static class DataMigrationThread implements Runnable {

private final int offset;

private final int batchSize;

private final Statement sourceStmt;

private final PreparedStatement targetStmt;

public DataMigrationThread(int offset, int batchSize, Statement sourceStmt, PreparedStatement targetStmt) {

this.offset = offset;

this.batchSize = batchSize;

this.sourceStmt = sourceStmt;

this.targetStmt = targetStmt;

}

@Override

public void run() {

try {

// 查询数据并将其添加到批处理中

ResultSet rs = sourceStmt.executeQuery("SELECT * FROM source_table LIMIT " + batchSize + " OFFSET " + offset);

while (rs.next()) {

int column1 = rs.getInt("column1");

String column2 = rs.getString("column2");

Date column3 = rs.getDate("column3");

targetStmt.setInt(1, column1);

targetStmt.setString(2, column2);

targetStmt.setDate(3, column3);

targetStmt.addBatch();

}

// 执行批处理并提交事务

targetStmt.executeBatch();

targetStmt.getConnection().commit();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

在这个示例中,我们使用了Java中的线程池来创建多个线程来同时处理数据。我们将要迁移的数据分成多个批次,然后为每个批次创建一个线程来处理。在每个线程中,我们从源数据库中查询数据,然后将其添加到批处理中。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值