2017-07-03 84 views
-3

我有一個名爲employee的表,其中有10 000條記錄。將表格數據寫入到java中的多個CSV文件中

我必須使用多線程Java將這些數據寫入多個CSV文件,每個文件有2 000條記錄。

示例代碼如下:

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 records.but一個CSV文件,我需要有2000條記錄中的每個5個CSV文件。

線程1必須先記錄2000處理成employee1.csv

線程2具有其他2000條記錄加工成employee2.csv

,,,,,等。

通過使用我的線程代碼,我該如何實現這個要求?

+2

請參閱https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question ...你的問題太廣泛了,它看起來像「請爲我做我的工作」 – GhostCat

+0

對不起。我也有一個程序。但由於某種安全原因,我不在這裏編寫代碼。我不需要完整的代碼。如果達到2000條記錄,只需創建一個更多的CSV文件即可。 –

+0

沒有人說你需要放置*生產*代碼。放個[mcve]!否則,您將收到**沒有任何**,但讚譽和關閉請求。 – GhostCat

回答

0

在你的代碼中,你可能需要一個分區器,它會給你一個來自索引並索引線程需要處理的內容。

公共類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