How do I read a large file from disk to database without running out of memory

后端 未结 6 880
野性不改
野性不改 2021-01-13 14:53

I feel embarrassed to ask this question as I feel like I should already know. However, given I don\'t....I want to know how to read large files from disk to a database witho

6条回答
  •  粉色の甜心
    2021-01-13 15:42

    Instead of reading csv rows one by one and inserting into db one by one I suggest read a chunk and insert it into database. Repeat this process until the entire file has been read.

    You can buffer in memory, say 1000 csv rows at a time, then insert them in the database.

    int MAX_BUFFERED=1000;
    int counter=0;
    List> bufferedRows= new ...
    
    while (scanner.hasNext()){
      List rowEntries= getData(scanner.getLine())
      bufferedRows.add(rowEntries);
    
      if (counter==MAX_BUFFERED){
        //INSERT INTO DATABASE
        //append all contents to a string buffer and create your SQL INSERT statement
        bufferedRows.clearAll();//remove data so it could be GCed when GC kicks in
      }
    }
    

提交回复
热议问题