2009-06-30 15 views
54

मुझे टेक्स्ट [csv] फ़ाइल में विशाल डेटा लिखना है। मैंने डेटा लिखने के लिए BufferedWriter का उपयोग किया और 174 एमबी डेटा लिखने में लगभग 40 सेकंड लग गए। क्या यह सबसे तेज गति जावा पेशकश कर सकता है?टेक्स्ट फ़ाइल में विशाल डेटा लिखने का सबसे तेज़ तरीका जावा

bufferedWriter = new BufferedWriter (new FileWriter ("fileName.csv")); 

नोट: ये 40 सेकेंड पुनरावृत्ति और resultset से रिकॉर्ड के रूप में अच्छी तरह से प्राप्त करने में कठिनाई के समय में शामिल हैं। :)। परिणाम में 4004 पंक्तियों के लिए 174 एमबी है।

+3

आप जहाँ आप इस कोड को चलाने मशीन पर सक्रिय एंटी-वायरस के लिए नहीं होगा? –

उत्तर

87

आप BufferedWriter को हटाने और सीधे फ़ाइलवाइटर का उपयोग करने का प्रयास कर सकते हैं। एक आधुनिक प्रणाली पर एक अच्छा मौका है कि आप अभी भी ड्राइव की कैश मेमोरी पर लिख रहे हैं।

यह मुझे 175 एमबी (4 मिलियन तार) लिखने के लिए 4-5 सेकेंड की सीमा में ले जाता है - यह एक डिजिटल कोर 2.4 गीगाहर्ट्ज डेल पर 80 जीबी, 7200-आरपीएम हिताची डिस्क के साथ विंडोज एक्सपी चल रहा है।

क्या आप अलग कर सकते हैं कि रिकॉर्ड पुनर्प्राप्ति कितनी बार है और फ़ाइल लेखन कितना है?

import java.io.BufferedWriter; 
import java.io.File; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.Writer; 
import java.util.ArrayList; 
import java.util.List; 

public class FileWritingPerfTest { 


private static final int ITERATIONS = 5; 
private static final double MEG = (Math.pow(1024, 2)); 
private static final int RECORD_COUNT = 4000000; 
private static final String RECORD = "Help I am trapped in a fortune cookie factory\n"; 
private static final int RECSIZE = RECORD.getBytes().length; 

public static void main(String[] args) throws Exception { 
    List<String> records = new ArrayList<String>(RECORD_COUNT); 
    int size = 0; 
    for (int i = 0; i < RECORD_COUNT; i++) { 
     records.add(RECORD); 
     size += RECSIZE; 
    } 
    System.out.println(records.size() + " 'records'"); 
    System.out.println(size/MEG + " MB"); 

    for (int i = 0; i < ITERATIONS; i++) { 
     System.out.println("\nIteration " + i); 

     writeRaw(records); 
     writeBuffered(records, 8192); 
     writeBuffered(records, (int) MEG); 
     writeBuffered(records, 4 * (int) MEG); 
    } 
} 

private static void writeRaw(List<String> records) throws IOException { 
    File file = File.createTempFile("foo", ".txt"); 
    try { 
     FileWriter writer = new FileWriter(file); 
     System.out.print("Writing raw... "); 
     write(records, writer); 
    } finally { 
     // comment this out if you want to inspect the files afterward 
     file.delete(); 
    } 
} 

private static void writeBuffered(List<String> records, int bufSize) throws IOException { 
    File file = File.createTempFile("foo", ".txt"); 
    try { 
     FileWriter writer = new FileWriter(file); 
     BufferedWriter bufferedWriter = new BufferedWriter(writer, bufSize); 

     System.out.print("Writing buffered (buffer size: " + bufSize + ")... "); 
     write(records, bufferedWriter); 
    } finally { 
     // comment this out if you want to inspect the files afterward 
     file.delete(); 
    } 
} 

private static void write(List<String> records, Writer writer) throws IOException { 
    long start = System.currentTimeMillis(); 
    for (String record: records) { 
     writer.write(record); 
    } 
    writer.flush(); 
    writer.close(); 
    long end = System.currentTimeMillis(); 
    System.out.println((end - start)/1000f + " seconds"); 
} 
} 
+2

@rozario प्रत्येक लेखन कॉल केवल 175 एमबी उत्पन्न करना चाहिए और फिर खुद को हटा देना चाहिए।यदि नहीं, तो आप 175 एमबी x 4 अलग-अलग लिखने वाले कॉल x 5 पुनरावृत्तियों = 3.5 जीबी डेटा के साथ समाप्त हो जाएंगे। आप file.delete() से वापसी मान की जांच कर सकते हैं और यदि यह गलत है, तो अपवाद फेंक दें। –

+0

ध्यान दें कि 'writer.flush()' इस मामले में आवश्यक नहीं है क्योंकि 'writer.close()' [फ्लश मेमोरी] (http://docs.oracle.com/javase/7/docs/api/java/io /BufferedWriter.html) implicity। बीटीडब्ल्यू: सर्वोत्तम अभ्यास [संसाधन संसाधन बंद करें] का उपयोग करने की सलाह देते हैं (https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) इसके बजाय स्पष्ट रूप से 'बंद() 'को कॉल करते हैं। –

+2

FWIW, यह जावा 5 के लिए लिखा गया था, जिसे कम से कम बंद करने के लिए दस्तावेज नहीं किया गया था, और जिसमें संसाधनों के साथ प्रयास नहीं किया गया था। यह शायद अद्यतन का उपयोग कर सकता है। –

4

आपकी स्थानांतरण गति जावा द्वारा सीमित नहीं होने की संभावना है। इसके बजाय मैं (किसी विशेष क्रम में) शक

  1. डेटाबेस
  2. डिस्क

के हस्तांतरण की गति से हस्तांतरण की गति आप पूरा डाटासेट पढ़ सकते हैं और फिर इसे बाहर लिखते हैं डिस्क पर, तब इसमें अधिक समय लगेगा, क्योंकि JVM को स्मृति आवंटित करना होगा, और डीबी री/डिस्क लेखन अनुक्रमिक रूप से होगा। इसके बजाय मैं डीबी से किए गए प्रत्येक पढ़ने के लिए buffered लेखक को लिखूंगा, और इसलिए ऑपरेशन समवर्ती एक के करीब होगा (मुझे नहीं पता कि आप ऐसा कर रहे हैं या नहीं)

28

कोशिश स्मृति मैप की गई फ़ाइलें (लेता है 300 मी/से मेरी एम/सी, कोर 2 डुओ, 2.5GB रैम में 174MB लिखने के लिए):

byte[] buffer = "Help I am trapped in a fortune cookie factory\n".getBytes(); 
int number_of_lines = 400000; 

FileChannel rwChannel = new RandomAccessFile("textfile.txt", "rw").getChannel(); 
ByteBuffer wrBuf = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, buffer.length * number_of_lines); 
for (int i = 0; i < number_of_lines; i++) 
{ 
    wrBuf.put(buffer); 
} 
rwChannel.close(); 
+0

जब आप बाइटबफर को तुरंत चालू कर रहे हैं तो प्रतिनिधित्व करने के लिए एक संदेश। लम्बाई() क्या है? – Hotel

+2

जूट फ़ैई, मैकबुक प्रो (देर से 2013) पर चल रहा है, 2.6 गीगा कोर i7, ऐप्पल 1 टीबी एसएसडी के साथ 185 मेगापिक्सल (लाइन = 4 मिलियन) – Egwor

+0

के लिए 140ms लेता है "number_of_lines" का बिंदु क्या है? –

14

केवल आंकड़ों की खातिर :

मशीन नई एसएसडी के साथ पुराने डेल है

CPU: इंटेल पेंटियम डी 2,8 Ghz

एसएसडी: पैट्रियट इन्फर्नो 120GB SSD

4000000 'records' 
175.47607421875 MB 

Iteration 0 
Writing raw... 3.547 seconds 
Writing buffered (buffer size: 8192)... 2.625 seconds 
Writing buffered (buffer size: 1048576)... 2.203 seconds 
Writing buffered (buffer size: 4194304)... 2.312 seconds 

Iteration 1 
Writing raw... 2.922 seconds 
Writing buffered (buffer size: 8192)... 2.406 seconds 
Writing buffered (buffer size: 1048576)... 2.015 seconds 
Writing buffered (buffer size: 4194304)... 2.282 seconds 

Iteration 2 
Writing raw... 2.828 seconds 
Writing buffered (buffer size: 8192)... 2.109 seconds 
Writing buffered (buffer size: 1048576)... 2.078 seconds 
Writing buffered (buffer size: 4194304)... 2.015 seconds 

Iteration 3 
Writing raw... 3.187 seconds 
Writing buffered (buffer size: 8192)... 2.109 seconds 
Writing buffered (buffer size: 1048576)... 2.094 seconds 
Writing buffered (buffer size: 4194304)... 2.031 seconds 

Iteration 4 
Writing raw... 3.093 seconds 
Writing buffered (buffer size: 8192)... 2.141 seconds 
Writing buffered (buffer size: 1048576)... 2.063 seconds 
Writing buffered (buffer size: 4194304)... 2.016 seconds 

हम देख सकते हैं कच्चे विधि बफ़र धीमी है।

+0

हालांकि, पाठ का आकार बड़ा होने पर बफरर्ड विधि धीमी हो जाती है। – FSm

1

package all.is.well; 
 
import java.io.IOException; 
 
import java.io.RandomAccessFile; 
 
import java.util.concurrent.ExecutorService; 
 
import java.util.concurrent.Executors; 
 
import junit.framework.TestCase; 
 

 
/** 
 
* @author Naresh Bhabat 
 
* 
 
Following implementation helps to deal with extra large files in java. 
 
This program is tested for dealing with 2GB input file. 
 
There are some points where extra logic can be added in future. 
 

 

 
Pleasenote: if we want to deal with binary input file, then instead of reading line,we need to read bytes from read file object. 
 

 

 

 
It uses random access file,which is almost like streaming API. 
 

 

 
* **************************************** 
 
Notes regarding executor framework and its readings. 
 
Please note :ExecutorService executor = Executors.newFixedThreadPool(10); 
 

 
* \t for 10 threads:Total time required for reading and writing the text in 
 
*   :seconds 349.317 
 
* 
 
*   For 100:Total time required for reading the text and writing : seconds 464.042 
 
* 
 
*   For 1000 : Total time required for reading and writing text :466.538 
 
*   For 10000 Total time required for reading and writing in seconds 479.701 
 
* 
 
* 
 
*/ 
 
public class DealWithHugeRecordsinFile extends TestCase { 
 

 
\t static final String FILEPATH = "C:\\springbatch\\bigfile1.txt.txt"; 
 
\t static final String FILEPATH_WRITE = "C:\\springbatch\\writinghere.txt"; 
 
\t static volatile RandomAccessFile fileToWrite; 
 
\t static volatile RandomAccessFile file; 
 
\t static volatile String fileContentsIter; 
 
\t static volatile int position = 0; 
 

 
\t public static void main(String[] args) throws IOException, InterruptedException { 
 
\t \t long currentTimeMillis = System.currentTimeMillis(); 
 

 
\t \t try { 
 
\t \t \t fileToWrite = new RandomAccessFile(FILEPATH_WRITE, "rw");//for random write,independent of thread obstacles 
 
\t \t \t file = new RandomAccessFile(FILEPATH, "r");//for random read,independent of thread obstacles 
 
\t \t \t seriouslyReadProcessAndWriteAsynch(); 
 

 
\t \t } catch (IOException e) { 
 
\t \t \t // TODO Auto-generated catch block 
 
\t \t \t e.printStackTrace(); 
 
\t \t } 
 
\t \t Thread currentThread = Thread.currentThread(); 
 
\t \t System.out.println(currentThread.getName()); 
 
\t \t long currentTimeMillis2 = System.currentTimeMillis(); 
 
\t \t double time_seconds = (currentTimeMillis2 - currentTimeMillis)/1000.0; 
 
\t \t System.out.println("Total time required for reading the text in seconds " + time_seconds); 
 

 
\t } 
 

 
\t /** 
 
\t * @throws IOException 
 
\t * Something asynchronously serious 
 
\t */ 
 
\t public static void seriouslyReadProcessAndWriteAsynch() throws IOException { 
 
\t \t ExecutorService executor = Executors.newFixedThreadPool(10);//pls see for explanation in comments section of the class 
 
\t \t while (true) { 
 
\t \t \t String readLine = file.readLine(); 
 
\t \t \t if (readLine == null) { 
 
\t \t \t \t break; 
 
\t \t \t } 
 
\t \t \t Runnable genuineWorker = new Runnable() { 
 
\t \t \t \t @Override 
 
\t \t \t \t public void run() { 
 
\t \t \t \t \t // do hard processing here in this thread,i have consumed 
 
\t \t \t \t \t // some time and eat some exception in write method. 
 
\t \t \t \t \t writeToFile(FILEPATH_WRITE, readLine); 
 
\t \t \t \t \t // System.out.println(" :" + 
 
\t \t \t \t \t // Thread.currentThread().getName()); 
 

 
\t \t \t \t } 
 
\t \t \t }; 
 
\t \t \t executor.execute(genuineWorker); 
 
\t \t } 
 
\t \t executor.shutdown(); 
 
\t \t while (!executor.isTerminated()) { 
 
\t \t } 
 
\t \t System.out.println("Finished all threads"); 
 
\t \t file.close(); 
 
\t \t fileToWrite.close(); 
 
\t } 
 

 
\t /** 
 
\t * @param filePath 
 
\t * @param data 
 
\t * @param position 
 
\t */ 
 
\t private static void writeToFile(String filePath, String data) { 
 
\t \t try { 
 
\t \t \t // fileToWrite.seek(position); 
 
\t \t \t data = "\n" + data; 
 
\t \t \t if (!data.contains("Randomization")) { 
 
\t \t \t \t return; 
 
\t \t \t } 
 
\t \t \t System.out.println("Let us do something time consuming to make this thread busy"+(position++) + " :" + data); 
 
\t \t \t System.out.println("Lets consume through this loop"); 
 
\t \t \t int i=1000; 
 
\t \t \t while(i>0){ 
 
\t \t \t 
 
\t \t \t \t i--; 
 
\t \t \t } 
 
\t \t \t fileToWrite.write(data.getBytes()); 
 
\t \t \t throw new Exception(); 
 
\t \t } catch (Exception exception) { 
 
\t \t \t System.out.println("exception was thrown but still we are able to proceeed further" 
 
\t \t \t \t \t + " \n This can be used for marking failure of the records"); 
 
\t \t \t //exception.printStackTrace(); 
 

 
\t \t } 
 

 
\t } 
 
}

+0

कृपया कुछ पाठ बताएं कि यह उत्तर अन्य उत्तरों की तुलना में बेहतर क्यों है। कोड में टिप्पणियां होने के लिए पर्याप्त नहीं है। –

+0

कारण यह बेहतर हो सकता है: यह वास्तविक समय परिदृश्य है और यह एक कार्यरत राज्य उदाहरण में है। इसके अन्य लाभ, यह अतुल्यकालिक रूप से पढ़ने, प्रसंस्करण और लिखने की प्रक्रिया करता है ... यह कुशल जावा एपीआई (i.e) रैंडम एक्सेस फ़ाइल का उपयोग करता है जो थ्रेड सुरक्षित है और एकाधिक थ्रेड इसे एक साथ पढ़ और लिख सकते हैं। यह रनटाइम पर मेमोरी ओवरहेड का कारण नहीं बनता है, यह सिस्टम को भी क्रैश नहीं करता है ... यह रिकॉर्डिंग प्रोसेसिंग की विफलता से निपटने के लिए बहुउद्देश्यीय समाधान है जिसे संबंधित धागे में ट्रैक किया जा सकता है। अगर मैं और मदद कर सकता हूं तो कृपया मुझे बताएं। – RAM

+1

धन्यवाद, यह वह जानकारी है जिसकी आपकी पोस्ट की आवश्यकता है। शायद इसे पोस्ट बॉडी में जोड़ने पर विचार करें :) –

संबंधित मुद्दे

 संबंधित मुद्दे