【问题标题】:Writing table data in to multiple CSV files in java在java中将表数据写入多个CSV文件
【发布时间】:2017-07-03 13:58:04
【问题描述】:

我有一个名为employee 的表,有10 000 条记录。

我必须使用多线程 Java 将这些数据写入多个 CSV 文件,每个文件有 2000 条记录。

示例代码如下:

    public class PrintbyThreads {

    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            Runnable worker = new PrintFiles("" + i);
            executor.execute(worker);
          }
        executor.shutdown();
        while (!executor.isTerminated()) {
        }
        System.out.println("Finished all threads");
    }
}

public class PrintFiles extends Thread implements Runnable {
    private String command;

    PrintFiles() {
        super("my extending thread");
        System.out.println("my thread created" + this);
        start();
    }

    public PrintFiles(String command) {
        this.command = command;
    }

    public void run() {
        System.out.println(Thread.currentThread().getName()
                + " Start. Command = " + command);
        dataBaseExecution();
        System.out.println(Thread.currentThread().getName() + " End. Command ="
                + command);
    }

    public synchronized void dataBaseExecution() {
        String tableName = "Employee";
        String filename = "D:/db2csv/";
        int recordsAtTime = 2000;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection(
                    "URL", "Uname", "Password");
            Statement stmt = con.createStatement();
            stmt.setFetchSize(recordsAtTime);
            ResultSet rs = stmt.executeQuery("select empid,empname,managerid from Employee");
            int columnCount = rs.getMetaData().getColumnCount();
            FileWriter fw = new FileWriter(filename + "" + tableName + ".csv");
            for (int i = 1; i <= columnCount; i++) {
                fw.append(rs.getMetaData().getColumnName(i));
                fw.append(",");

            }
            fw.append(System.getProperty("line.separator"));
            while (rs.next()) {
                for (int i = 1; i <= columnCount; i++) {
                    if (rs.getObject(i) != null) {
                        String data = rs.getObject(i).toString();
                        fw.append(data);
                        fw.append(",");
                    } else {
                        String data = "null";
                        fw.append(data);
                        fw.append(",");
                    }

                }
                // new line entered after each row
                fw.append(System.getProperty("line.separator"));
            }

            fw.flush();
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return this.command;
    }
}

我的代码给出了包含所有 10000 条记录的单个 CSV 文件。但我需要 5 个 CSV 文件,每个文件有 2000 条记录。

Thread1 必须将前 2000 条记录处理到 employee1.csv

线程 2 必须将另外 2000 条记录处理到 employee2.csv

,,,,,等等

通过使用我的线程代码,我怎样才能达到这个要求?

【问题讨论】:

  • 请参阅meta.stackoverflow.com/questions/284236/… ...您的问题太宽泛了;它读起来像“请为我做我的工作”
  • 对不起。我也有一个程序。但由于某些安全原因,我不在这里编写代码。我不需要完整的代码。如果达到 2000 条记录,只需很少的逻辑再创建一个 CSV 文件..
  • 没有人说你需要放置你的生产代码。发一个minimal reproducible example!否则,您将收到什么都没有,只会收到反对票和关闭请求。
  • 你应该在你的问题中加入一个问题。

标签: java multithreading oracle


【解决方案1】:

在您的代码中,您可能需要一个分区器,该分区器将为您提供一个来自索引并为线程需要处理的索引。

公共类 MyPartitioner {

public Map partition(int gridSize,int range) {

    Map partitionMap = new HashMap();
    int startingIndex = 0;
    int endingIndex = range;

    for(int i=0; i< gridSize; i++){
      Map threadMap = new HashMap();

        threadMap.putInt("startingIndex",startingIndex);
        threadMap.putInt("endingIndex", endingIndex);

        startingIndex = endingIndex+1;
        endingIndex += range; 

        partitionMap.put("Thread:-"+i, threadMap);
    }

    return partitionMap;
}

}

执行器框架使用这个从索引和索引工作线程来处理分配的行而不是所有行。 在您的选择查询中使用过滤条件

where id >= :fromId and id <= :toId

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多