使用 Java 将多个数据库的数据高效导出到 CSV

问题描述 投票:0回答:1

我有一个独特的要求,即从两个不同的数据库中获取选定的记录和列 - 一个在 SQL Server 中,另一个在 MongoDB 中。目标是将这些数据存储到文件中,我正在寻找一种高效的基于 Java 的解决方案或参考代码来快速完成此任务。我对Java比较陌生,数据量很大,大约有300万条记录。由于这不是一项重复性工作,因此我不想使用 ETL 工具。相反,我想要一个可以多次读取大量数据并将其导出到 CSV 文件中的实用程序。

当我从 SQL Server 手动导出数据时,需要几分钟的时间,我想知道是否有一种更快的方法可以使用 Java 实现此目的,也许可以利用多线程或其他优化技术。任何指导、代码片段或参考文献将不胜感激

java batch-processing
1个回答
-1
投票

考虑到从两个不同的数据库获取数据、将其导出到 CSV 并处理大量数据的要求,我将向您提供路线图以及一些代码片段来帮助您入门:

1.设置依赖关系:

使用 Maven 或 Gradle 将 JDBC for SQL Server 和 MongoDB Java 驱动程序包含在您的项目中:

马夫:

<!-- SQL Server JDBC -->
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>LATEST_VERSION</version>
</dependency>

<!-- MongoDB Java Driver -->
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongodb-driver-sync</artifactId>
    <version>LATEST_VERSION</version>
</dependency>

<!-- Apache Commons CSV for handling CSV operations -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>LATEST_VERSION</version>
</dependency>

2.创建数据库连接实用程序:

SQL 服务器:

import java.sql.Connection;
import java.sql.DriverManager;

public class SQLServerConnection {

    public static Connection getConnection(String url, String user, String password) throws Exception {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        return DriverManager.getConnection(url, user, password);
    }
}

MongoDB:

import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;

public class MongoDBConnection {

    public static MongoDatabase getDatabase(String host, int port, String dbName) {
        MongoClient mongoClient = new MongoClient(host, port);
        return mongoClient.getDatabase(dbName);
    }
}

3.获取数据:

这是从 SQL Server 和 MongoDB 获取数据的简单方法:

SQL 服务器:

try (Connection connection = SQLServerConnection.getConnection(YOUR_URL, YOUR_USER, YOUR_PASSWORD);
     PreparedStatement preparedStatement = connection.prepareStatement(YOUR_QUERY);
     ResultSet resultSet = preparedStatement.executeQuery()) {

    // Process resultSet...
}

MongoDB:

MongoDatabase database = MongoDBConnection.getDatabase(YOUR_HOST, YOUR_PORT, YOUR_DB_NAME);
MongoCollection<Document> collection = database.getCollection(YOUR_COLLECTION_NAME);

for (Document doc : collection.find()) {
    // Process each document...
}

4.写入 CSV:

利用 Apache Commons CSV 库进行高效的 CSV 写入:

try (FileWriter writer = new FileWriter(YOUR_OUTPUT_PATH);
     CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.DEFAULT.withHeader(YOUR_HEADERS))) {
    
    // ... fetch data ...

    // SQL Server example:
    while (resultSet.next()) {
        csvPrinter.printRecord(resultSet.getString(1), resultSet.getInt(2), ...);
    }

    // MongoDB example:
    for (Document doc : collection.find()) {
        csvPrinter.printRecord(doc.getString("field1"), doc.getInteger("field2"), ...);
    }
}

5.优化:

  • 批处理:不要一次获取所有记录,而是使用批处理,尤其是对于 SQL Server。这将允许您以可管理的块的形式获取记录,这对于内存管理特别有帮助。

  • 线程:使用单独的线程同时从 SQL Server 和 MongoDB 获取数据。 Java 的

    ExecutorService
    可以很方便地实现这一点。确保同步对共享资源的访问。

  • 缓冲写入:使用缓冲写入(例如Java中的

    BufferedWriter
    )可以加快文件写入过程。

结论:

提供的代码片段提供了一种从 SQL Server 和 MongoDB 获取数据并将其写入 CSV 文件的简单方法。根据具体要求和数据结构,您可能需要进行修改。高效处理如此大量数据的关键在于有效的批处理、明智地使用内存以及优化 I/O 操作。

© www.soinside.com 2019 - 2024. All rights reserved.