SSIS参差不齐的文件未被认可CRLF

时间:2017-05-29 15:13:31

标签: sql-server ssis etl flat-file

在SSIS中,我尝试从平面文件加载数据。 平面文件具有固定宽度的列,但某些列不存在于一行中(列可以具有CRLF,必须是新行),如下所示

                    @Override
        protected User doInBackground(List<Pair<String, String>>... params) {
            randomAPI api = randomAPI
                    .getInstance(context);

            try {
                **thing that will generate error**
                }

            } catch (RandomException e) {
                Log.e(TAG, "Error", e);
            }
            return somethingThatIsUsableInOnPostExecute;
        }

        @Override
        protected void onPostExecute(User result) {

            }

如何在输出中获得完全相同数量的行和精确数据?

我设置了一个平面文件连接,右边是粗糙的。​​

在此示例中,第1行正确检索,但对于第2行,它无法识别CRLF,并且在第3行中放入b列...

提前致谢

此致

1 个答案:

答案 0 :(得分:1)

解决方法

  1. 在平面文件连接管理器中将整行读为一列(仅添加一列DT_STR类型和长度4000)
  2. enter image description here

    1. 然后在数据流任务中添加一个脚本组件
    2. 添加类型为DT_STR
    3. 的三个输出列(a,b,c)

      enter image description here

      1. 编写一个脚本,将每行分开并将值放入列中(如果一个值丢失则为null)(我使用的是vb.net)
      2. 制表符分隔列

            Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
        
        
                If Not Row.Column0_IsNull AndAlso
                        Not String.IsNullOrEmpty(Row.Column0.Trim) Then
        
                    Dim str() As String = Row.Column0.Split(CChar(vbTab))
        
        
                    If str.Length >= 3 Then
        
                        Row.a = str(0)
                        Row.b = str(1)
                        Row.c = str(2)
        
                    ElseIf str.Length = 2 Then
        
                        Row.a = str(0)
                        Row.b = str(1)
                        Row.c_IsNull = True
        
                    ElseIf str.Length = 1 Then
        
        
                        Row.a = str(0)
                        Row.b_IsNull = True
                        Row.c_IsNull = True
        
        
        
                    Else
        
                        Row.a_IsNull = True
                        Row.b_IsNull = True
                        Row.c_IsNull = True
        
        
                    End If
        
        
        
        
                Else
        
                    Row.a_IsNull = True
                    Row.b_IsNull = True
                    Row.c_IsNull = True
        
                End If
        
        
            End Sub
        

        固定宽度列

        Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
        
        
            If Not Row.Column0_IsNull AndAlso
                        Not String.IsNullOrEmpty(Row.Column0.Trim) Then
        
                'Assuming that
                'Col a => 0-5
                'Col b => 5-15
                'Col c => 15-
        
                Dim intlength As Integer = Row.Column0.Length
        
        
        
                If intlength <= 5 Then
        
                    Row.a = Row.Column0
                    Row.b_IsNull = True
                    Row.c_IsNull = True
        
                ElseIf intlength > 5 AndAlso intlength <= 15 Then
        
        
                    Row.a = Row.Column0.Substring(0, 5)
                    Row.b = Row.Column0.Substring(5, 10)
                    Row.c_IsNull = True
        
                ElseIf intlength > 15 Then
        
                    Row.a = Row.Column0.Substring(0, 5)
                    Row.b = Row.Column0.Substring(5, 10)
                    Row.c = Row.Column0.Substring(15)
        
                End If
        
        
        
            Else
        
                Row.a_IsNull = True
                Row.b_IsNull = True
                Row.c_IsNull = True
        
            End If
        
        
        End Sub
        

        您也可以使用派生列转换

        来实现此目的