平面文件中的 SSIS 列计数

发布于 2024-11-15 01:49:17 字数 340 浏览 4 评论 0原文

我正在尝试找到一种方法来计算来自平面文件的列数。实际上,我的所有列都连接在一个符号单元格中,并用“|”分隔,
经过各种尝试,似乎只有脚本任务可以处理这个问题。 有人可以帮助我吗?遗憾的是,我没有使用 C# 或 VB 脚本的经验。

多谢 为了更好地理解

,以下是我想要实现的输出。例如,包含来自 FF 的所有标头的单个单元格。问题是,为了得到这个结果,我在上一步(派生列)中手动附加了所有列名称,以便用“|”将它们连接起来分隔符。 现在,如果我的 FF 源布局发生变化,由于这个手动过程,它将不再工作。所以我想我必须使用一个脚本,它基本上返回变量中的列数( header ),并且允许删除派生列 transfo 中的硬编码部分

I'm trying to find a way to count my columns coming from a Flat File. Actually, all my columns are concatened in a signe cell, sepatared with a '|' ,
after various attempts, it seems that only a script task can handle this.
Does anyone can help me upon that ? I've shamely no experience with script in C# ou VB.

Thanks a lot
Emmanuel

To better understand, below is the output of what I want to achieve to. e.g a single cell containing all headers coming from a FF. The thing is, to get to this result, I appended manually in the previous step ( derived column) all column names each others in order to concatenate them with a '|' separator.
Now , if my FF source layout changes, it won't work anymore, because of this manualy process. So I think I would have to use a script instead which basically returns my number of columns (header ) in a variable and will allow to remove the hard coded part in the derived column transfo for instance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

峩卟喜欢 2024-11-22 01:49:17

这是一个非常古老的线程;然而,我偶然发现了类似的问题。内部有多种不同记录“格式”的平面文件。许多不同的格式,没有任何特定的顺序,这意味着一行中可能有 57 个字段,然后接下来的 1000 个字段中有 59 个字段,然后接下来的 10000 个字段中有 56 个字段,最后回到 57 个......好吧,我想你明白了。

由于缺乏更好的想法,我决定根据每行中的逗号数量来破坏该文件,然后使用每种类型的 SSIS 包导入不同的记录类型(现在聚集在一起)。

所以这个问题的答案就在那里,需要更多的代码来生成文件。

希望这可以帮助遇到同样问题的人。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace OddFlatFile_Transformation
{
    class RedistributeLines
    {
    /*
     * This routine opens a text file and reads it line by line
     * for each line the number of "," (commas) is counted
     * and then the line is written into a another text file
     * based on that number of commas found
     * For example if there are 15 commas in a given line
     * the line is written to the WhateverFileName_15.Ext
     * WhaeverFileName and Ext are the same file name and 
     * extension from the original file that is being read
     * The application tests WhateverFileName_NN.Ext for existance
     * and creates the file in case it does not exist yet
     * To Better control splited records a sequential identifier, 
     * based on the number of lines read, is added to the beginning
     * of each line written independently of the file and record number
     */
        static void Main(string[] args)
        {
            // get full qualified file name from console
            String strFileToRead;
            strFileToRead = Console.ReadLine();

            // create reader & open file
            StreamReader srTextFileReader = new StreamReader(strFileToRead);

            string strLineRead = "";
            string strFileToWrite = "";
            string strLineIdentifier = "";
            string strLineToWrite = "";
            int intCountLines = 0;
            int intCountCommas = 0;
            int intDotPosition = 0;
            const string strZeroPadding = "00000000";

            // Processing begins
            Console.WriteLine("Processing begins: " + DateTime.Now);

            /* Main Loop */
            while (strLineRead != null)
            {
                // read a line of text count commas and create Linde Identifier
                strLineRead = srTextFileReader.ReadLine();
                if (strLineRead != null)
                {
                    intCountLines += 1;
                    strLineIdentifier = strZeroPadding.Substring(0, strZeroPadding.Length - intCountLines.ToString().Length) + intCountLines;
                    intCountCommas = 0;
                    foreach (char chrEachPosition in strLineRead)
                    {
                        if (chrEachPosition == ',') intCountCommas++;
                    }

                    // Based on the number of commas determined above
                    // the name of the file to be writen to is established
                    intDotPosition = strFileToRead.IndexOf(".");
                    strFileToWrite = strFileToRead.Substring (0,intDotPosition) + "_";
                    if ( intCountCommas < 10)
                    {
                        strFileToWrite += "0" + intCountCommas;
                    }
                    else
                    {
                        strFileToWrite += intCountCommas;
                    }
                    strFileToWrite += strFileToRead.Substring(intDotPosition, (strFileToRead.Length - intDotPosition));

                    // Using the file name established above the line captured
                    // during the text read phase is written to that file

                    StreamWriter swTextFileWriter = new StreamWriter(strFileToWrite, true);
                    strLineToWrite = "[" + strLineIdentifier + "] " + strLineRead; 
                    swTextFileWriter.WriteLine (strLineToWrite);
                    swTextFileWriter.Close();
                     Console.WriteLine(strLineIdentifier);
               }
            }

            // close the stream
            srTextFileReader.Close();
            Console.WriteLine(DateTime.Now);
            Console.ReadLine();
        }
    }


}

This is an very old thread; however, I just stumbled on a similar problem. A flat file with a number of different record "formats" inside. Many different formats, not in any particular order, meaning you might have 57 fields in one line, then 59 in the next 1000, then 56 in the next 10000, back to 57... well, think you got the idea.

For lack of better ideas, I decided to break that file based on the number of commas in each line, and then import the different record types (now bunched together) using SSIS packages for each type.

So the answer for this question is there, with a bit more code to produce the files.

Hope this helps somebody with the same problem.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace OddFlatFile_Transformation
{
    class RedistributeLines
    {
    /*
     * This routine opens a text file and reads it line by line
     * for each line the number of "," (commas) is counted
     * and then the line is written into a another text file
     * based on that number of commas found
     * For example if there are 15 commas in a given line
     * the line is written to the WhateverFileName_15.Ext
     * WhaeverFileName and Ext are the same file name and 
     * extension from the original file that is being read
     * The application tests WhateverFileName_NN.Ext for existance
     * and creates the file in case it does not exist yet
     * To Better control splited records a sequential identifier, 
     * based on the number of lines read, is added to the beginning
     * of each line written independently of the file and record number
     */
        static void Main(string[] args)
        {
            // get full qualified file name from console
            String strFileToRead;
            strFileToRead = Console.ReadLine();

            // create reader & open file
            StreamReader srTextFileReader = new StreamReader(strFileToRead);

            string strLineRead = "";
            string strFileToWrite = "";
            string strLineIdentifier = "";
            string strLineToWrite = "";
            int intCountLines = 0;
            int intCountCommas = 0;
            int intDotPosition = 0;
            const string strZeroPadding = "00000000";

            // Processing begins
            Console.WriteLine("Processing begins: " + DateTime.Now);

            /* Main Loop */
            while (strLineRead != null)
            {
                // read a line of text count commas and create Linde Identifier
                strLineRead = srTextFileReader.ReadLine();
                if (strLineRead != null)
                {
                    intCountLines += 1;
                    strLineIdentifier = strZeroPadding.Substring(0, strZeroPadding.Length - intCountLines.ToString().Length) + intCountLines;
                    intCountCommas = 0;
                    foreach (char chrEachPosition in strLineRead)
                    {
                        if (chrEachPosition == ',') intCountCommas++;
                    }

                    // Based on the number of commas determined above
                    // the name of the file to be writen to is established
                    intDotPosition = strFileToRead.IndexOf(".");
                    strFileToWrite = strFileToRead.Substring (0,intDotPosition) + "_";
                    if ( intCountCommas < 10)
                    {
                        strFileToWrite += "0" + intCountCommas;
                    }
                    else
                    {
                        strFileToWrite += intCountCommas;
                    }
                    strFileToWrite += strFileToRead.Substring(intDotPosition, (strFileToRead.Length - intDotPosition));

                    // Using the file name established above the line captured
                    // during the text read phase is written to that file

                    StreamWriter swTextFileWriter = new StreamWriter(strFileToWrite, true);
                    strLineToWrite = "[" + strLineIdentifier + "] " + strLineRead; 
                    swTextFileWriter.WriteLine (strLineToWrite);
                    swTextFileWriter.Close();
                     Console.WriteLine(strLineIdentifier);
               }
            }

            // close the stream
            srTextFileReader.Close();
            Console.WriteLine(DateTime.Now);
            Console.ReadLine();
        }
    }


}
一梦等七年七年为一梦 2024-11-22 01:49:17

请参考我在以下 Stack Overflow 问题中的回答。这些答案可能会让您了解如何加载包含不同列数的平面文件。

  1. 以下问题中的示例读取包含由特殊字符 Ç (c-cedilla) 分隔的数据的文件。在您的情况下,分隔符是 竖线 (|)
    UTF-8 平面文件导入到 SQL Server 2008 时无法识别 {LF} 行分隔符

  2. 示例如下问题读取一个 EDI 文件,其中包含具有不同列数的不同部分。包读取文件并将其与父子关系相应地加载到 SQL 表中。
    如何将具有标头和详细信息父子关系的平面文件加载到 SQL Server

根据这些答案中使用的逻辑,您还可以通过以下方式计算列数通过列分隔符 (Vertical Bar |) 拆分文件中的行。

希望有帮助。

Please refer my answers in the following Stack Overflow questions. Those answers might give you an idea of how to load a flat file that contains varying number of columns.

  1. Example in the following question reads a file containing data separated by special character Ç (c-cedilla). In your case, the delimiter is Vertical Bar (|)
    UTF-8 flat file import to SQL Server 2008 not recognizing {LF} row delimiter

  2. Example in the following question reads an EDI file that contains different sections with varying number of columns. The package reads the file loads it accordingly with parent-child relationships into an SQL table.
    how to load a flat file with header and detail parent child relationship into SQL server

Based on the logic used in those answers, you can also count the number of columns by splitting the rows in the file by the column delimiter (Vertical Bar |).

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文