将平面文件拆分为多个文件

时间:2016-05-23 21:40:32

标签: visual-studio-2012 ssis ssis-2012

我需要创建一个将大平面文件拆分成多个平面文件的包。

我有平面文件,有2000万行,现在我需要拆分这个平面文件(每个平面文件需要55 k行)

示例: - 如果总共有111行,我将不得不创建3个文件。

file1.txt将有1-55行 file2.txt将有55-110行 file3.txt将有1行

我有哪些选择?

我正在将Visual Studio 2012用于此项目。

2 个答案:

答案 0 :(得分:1)

你可以尝试这样的东西...它非常简陋,我相信有人会指出它不是最有效的东西,但它是一个可靠的选择。请注意,您需要添加一些try catch错误处理。

            int recper = 0; // this is where you will assign the number of records per file 
            int reccount = 0;
            int filecount = 1;
            string filename = "testfilename";
            string networkDirectory = @"c:\fakepath\";
            string fileToRead = @"c:\fakepath\textfile.txt";

            using (StreamReader reader = new StreamReader(fileToRead,Encoding.Default,true))
            {
                while (reader.Peek() > 0)
                {
                    using (StreamWriter writer = new StreamWriter(Path.Combine(networkDirectory, filename + filecount + ".txt"), true, Encoding.Default))
                    {
                        writer.Write(reader.ReadLine());
                    }
                    reccount++;
                    // checks on each iteration of the while loop to see if the 
                    // current record count matches the number of records per file
                    // if sso reset reccount and change increment filecount to change the file name 
                    if (reccount == recper)
                    {
                        reccount = 0;
                        filecount++;
                    }
                }
            }

答案 1 :(得分:1)

另一种方法是在数据流中:

首先使用您选择的方法添加"行号"数据流的列(除非您的平面文件输出中已有一个,在这种情况下跳过此步骤并使用它):

https://www.google.com/search?sourceid=navclient&aq=&oq=add+rownumber+column+to+ssis+dataflow&ie=UTF-8&rlz=1T4GGNI_enUS551US551&q=add+rownumber+column+to+ssis+dataflow&gs_l=hp....0.0.0.6218...........0._Qm62-0x_YQ

然后向您的数据流添加MultiCast转换,并使用行号拆分流并将其发送到不同的目的地:

第1行 - 55k, - >文件1

第55001行 - 110k - > file2的