標籤:
需求:
在項目開發中,我們會遇到單個檔案大小超過1TB的檔案,這樣的檔案只能進行單檔案讀取,往往會造成讀取完成耗時過長,導致客戶在使用體驗過程中不滿意。
為瞭解決提升大檔案的解析速度,我想到了先分割大檔案為小檔案,之後進行並行多個檔案同時解析入庫方案。
那麼,怎麼才可以把一個大檔案分割為多個小檔案呢?
如果我按照大小來控制分割出來的小檔案,會造成檔案的丟失問題,如果按照行數來分割,一行一行進行讀取務必會造成分割檔案耗時過長。
討論:如果一個1TB的檔案,我們按照大小來控制檔案個數,假設每個分割出來的檔案大小為200M,這樣的話1TB分割出來約5200個檔案,這樣子的話最多造成約10000行資訊被破壞,可以忽略不計。
所以我們為了減少分割檔案帶來的耗時時間長度,採取分割方案採用定長控制分割出來的檔案大小。
- 實現方案1:一次性讀取1M,直到讀取到200M為止,開始寫入下一個分割檔案。
1 using (FileStream readerStream = new FileStream(file, FileMode.Open, FileAccess.Read)) 2 { 3 // 如果大於1GB 4 using (BinaryReader reader = new BinaryReader(readerStream)) 5 { 6 int fileCursor = 0; 7 int readerCursor = 0; 8 char[] buffer = new char[1024 * 1024]; 9 int length = 0;10 11 NextFileBegin:12 string filePath = string.Format(splitFileFormat, fileCursor);13 14 Console.WriteLine("開始讀取檔案【{1}】:{0}", filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));15 using (FileStream writerStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))16 {17 using (BinaryWriter writer = new BinaryWriter(writerStream))18 {19 while ((length = reader.Read(buffer, 0, buffer.Length)) > 0)20 {21 readerCursor++;22 23 writer.Write(buffer, 0, length);24 25 if (readerCursor >= splitFileSize)26 {27 Console.WriteLine("結束讀取檔案【{1}】:{0}", filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));28 29 readerCursor = 0;30 fileCursor++;31 32 goto NextFileBegin;33 }34 }35 }36 }37 }38 }
- 實現方案2:一次性讀取200M,立即寫入分割檔案,開始下一個分割檔案操作。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.IO; 6 using System.Configuration; 7 8 namespace BigFileSplitTest 9 {10 class Program11 {12 static void Main(string[] args)13 {14 /*15 * <!--是否開啟大檔案分隔策略-->16 <add key="BigFile.Split" value="true"/>17 <!--當檔案大於這個配置項時就執行檔案分隔,單位:GB -->18 <add key="BigFile.SplitMinFileSize" value="10" />19 <!--當執行檔案分割時,每個分隔出來的檔案大小,單位:MB -->20 <add key="BigFile.SplitFileSize" value="200"/>21 * <add key="BigFile.FilePath" value="\\172.x1.xx.xx\檔案拷貝\xx\FTP\xx\2016-04-07\x_20160407.txt"/>22 <add key="BigFile.FileSilitPathFormate" value="\\172.x1.xx.xx\檔案拷貝\liulong\FTP\xx\2016-04-07\x_20160407{0}.txt"/>23 */24 25 string file = ConfigurationManager.AppSettings.Get("BigFile.FilePath");26 string splitFileFormat = ConfigurationManager.AppSettings.Get("BigFile.FileSilitPathFormate");27 int splitMinFileSize = Convert.ToInt32(ConfigurationManager.AppSettings.Get("BigFile.SplitMinFileSize")) * 1024 * 1024 * 1204;28 int splitFileSize = Convert.ToInt32(ConfigurationManager.AppSettings.Get("BigFile.SplitFileSize")) * 1024 * 1024;29 30 FileInfo fileInfo = new FileInfo(file);31 if (fileInfo.Length > splitMinFileSize)32 {33 Console.WriteLine("判定結果:需要分隔檔案!");34 }35 else36 {37 Console.WriteLine("判定結果:不需要分隔檔案!");38 Console.ReadKey();39 return;40 }41 42 int steps = (int)(fileInfo.Length / splitFileSize);43 using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))44 {45 using (BinaryReader br = new BinaryReader(fs))46 {47 int couter = 1;48 bool isReadingComplete = false;49 while (!isReadingComplete)50 {51 string filePath = string.Format(splitFileFormat, couter);52 Console.WriteLine("開始讀取檔案【{1}】:{0}", filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));53 54 byte[] input = br.ReadBytes(splitFileSize);55 using (FileStream writeFs = new FileStream(filePath, FileMode.Create))56 {57 using (BinaryWriter bw = new BinaryWriter(writeFs))58 {59 bw.Write(input);60 }61 }62 63 isReadingComplete = (input.Length != splitFileSize);64 if (!isReadingComplete)65 {66 couter += 1;67 }68 Console.WriteLine("完成讀取檔案【{1}】:{0}", filePath, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));69 }70 }71 }72 73 74 Console.WriteLine("分隔完成,請按下任意鍵結束操作。。。");75 Console.ReadKey();76 77 }78 }79 }
從實驗結果發現:方案一的效能較方案二的效能約耗時10倍。
具體原因為什嗎?
請你思考下:
一次性讀取1M,直到讀取到200M為止,開始寫入下一個分割檔案。
一次性讀取200M,立即寫入分割檔案,開始下一個分割檔案操作。
c#大檔案分割過程