【问题标题】:SSIS column count from a flat file来自平面文件的 SSIS 列数
【发布时间】:2011-06-13 11:32:32
【问题描述】:

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

非常感谢 伊曼纽尔

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

【问题讨论】:

    标签: file ssis flat


    【解决方案1】:

    这是一个非常古老的线程;但是,我偶然发现了一个类似的问题。一个平面文件,里面有许多不同的记录“格式”。许多不同的格式,没有任何特定的顺序,这意味着您可能在一行中有 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();
            }
        }
    
    
    }
    

    【讨论】:

      【解决方案2】:

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

      1. 以下问题中的示例读取包含由特殊字符 Ç (c-cedilla) 分隔的数据的文件。在您的情况下,分隔符是 Vertical Bar (|) UTF-8 flat file import to SQL Server 2008 not recognizing {LF} row delimiter

      2. 以下问题中的示例读取了一个 EDI 文件,该文件包含具有不同列数的不同部分。包读取文件,将其与父子关系相应地加载到 SQL 表中。 how to load a flat file with header and detail parent child relationship into SQL server

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

      希望对您有所帮助。

      【讨论】:

      • 好的,非常感谢您的回答,但我目前要做的是计算我的标题,这些标题用分隔符分隔
      • 我知道我的解释可能并不完美,但是我怎样才能把屏幕截图放在这里呢?它肯定有助于更好地理解我的问题
      • 我添加了更多文字,但我不确定我的图片是否已上传
      • 为了更好地理解,最后我想要一个包含来自 FF 的所有标题的单个单元格。问题是,为了得到这个结果,我在上一步(派生列)中手动附加了所有列名,以便将它们与“|”连接起来分隔器。现在,如果我的 FF 源布局发生变化,它将不再起作用,因为这个手动过程。所以我认为我将不得不使用一个脚本,它基本上会在一个变量中返回我的列数(标题),并允许删除派生列 transfo 中的硬编码部分
      猜你喜欢
      • 2017-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多