【问题标题】:Split flat file into multiple files将平面文件拆分为多个文件
【发布时间】:2016-09-20 22:12:34
【问题描述】:

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

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

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

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

我有什么选择?

我在这个项目中使用 Visual Studio 2012。

【问题讨论】:

  • 您是否能够或有能力使用脚本任务,或者您是否希望使用数据流
  • 我不擅长写脚本,但我愿意接受建设性的建议。

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


【解决方案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 个平面文件应该只有 50 k 行的条件。很抱歉问你这个愚蠢的问题。
  • 对不起,我忘了在代码中注释它是 recper 变量。您可以在 while 循环中看到,每次迭代都会检查当前的记录数是否等于每个文件的记录数,如果是,则它会增加文件计数以更改输出文件的名称,然后重新发送记录计数变量以开始计数再次。我将编辑我的答案以显示这一点
【解决方案2】:

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

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

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 -> 文件 2

等等

【讨论】:

  • 我有几百万条记录,按照这个逻辑,我需要300+组播转换吗?任何替代方法?
猜你喜欢
  • 2013-08-24
  • 2014-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-22
  • 1970-01-01
  • 2015-02-27
相关资源
最近更新 更多