2014-04-29 75 views
2

我有以下代碼從oracle數據庫讀取數據並寫入csv文件。我使用OpenCSV jar來編寫。編寫1MB數據需要230秒。有沒有其他方法可以提高性能?如何提高將數據寫入CSV的性能

springJdbcTemplate.query(query,new ResultSetExtractor<ResultSet>(){ 

      @Override 
      public ResultSet extractData(ResultSet rs) throws SQLException, 
        DataAccessException { 
       try { 
        CSVWriter writer = new CSVWriter(new FileWriter("C:/csv/Sample.csv"), ';'); 
        writer.writeAll(rs, true); 
       } catch (Exception e) { 
        System.out.println("Exception -> "+e); 
       } 

       return rs; 
      }}); 
+0

你可以分享csvwriter的代碼? – Hirak

+1

提取數據的可能性更大。提取所有數據需要多長時間,而不需要編寫它? –

+0

@Hirak CSVWriter是OpenCSV jar中的一個類。這不是我定製的課程。 – Samurai

回答

1

它用7秒鐘不寫。

我不能想像爲什麼CSVWriter是如此緩慢,除非它需要緩衝。

你可以嘗試

CSVWriter writer = new CSVWriter(
        new BufferedWriter(new FileWriter("C:/csv/Sample.csv")), ';'); 

,並添加

writer.close(); 

或使用Java 7+

try(CSVWriter writer = ...) { 

試試這個

import java.io.*; 

public class DumbCSVWriter { 
    private final Writer writer; 
    private final String sep; 

    public DumbCSVWriter(Writer writer, String sep) { 
     this.sep = sep; 
     this.writer = writer instanceof BufferedWriter ? writer : new BufferedWriter(writer); 
    } 

    public void addRow(Object... values) throws IOException { 
     for (int i = 0; i < values.length - 1; i++) { 
      print(values[i]); 
      writer.write(sep); 
     } 
     if (values.length > 0) 
      print(values[values.length - 1]); 
     writer.write("\n"); 
    } 

    private void print(Object value) throws IOException { 
     if (value == null) return; 
     String str = value.toString(); 
     if (str.contains(sep) || str.contains("\"") || str.contains("\n")) { 
      str = '"' + str.replaceAll("\"", "\"\""); 
     } 
     writer.write(str); 
    } 

    public static void main(String[] args) throws IOException { 
     long start = System.nanoTime(); 
     File file = new File("/tmp/deleteme"); 
     DumbCSVWriter writer = new DumbCSVWriter(new FileWriter(file), ";"); 
     String[] words = "hello,,has a;semi-colon,has a \"quote".split(","); 
     for (int i = 0; file.length() < 1024 * 1024; i++) { 
      writer.addRow(words); 
     } 
     writer.close(); 
     long time = System.nanoTime() - start; 
     System.out.printf("Time tow rite 1 MB %.3f%n", time/1e9); 
    } 

    private void close() throws IOException { 
     writer.close(); 
    } 
} 

打印

時間寫1 MB 0.307

+0

BufferedWriter減少了10秒。仍然需要更多時間。 – Samurai

+0

@武士你可以表演簡介吧。我無法想象爲什麼要花1秒鐘才能寫出1 MB的CSV數據。 –

+0

問題是從數據庫讀取數據。對於簡單的while(rs.next()){},try內部需要247秒的1200KB數據。 – Samurai