SSIS - Script Component, Split single row to multiple rows

reference1:

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
Dim RowId As String = Row.RowID
Dim DelimitedList As String = Row.DelimitedList
Dim delimiter As String = ","

If Not String.IsNullOrEmpty(RowId.Trim) Then
If Not (String.IsNullOrEmpty(DelimitedList)) Then
Dim DelimitedListArray() As String = DelimitedList.Split(New String() {delimiter}, StringSplitOptions.RemoveEmptyEntries)

For Each item As String In DelimitedListArray
With Output0Buffer
.AddRow()
.RowId = RowId
.Item = item.Replace(ControlChars.Cr, "").Replace(ControlChars.Lf, "").Replace(ControlChars.Tab, "").Trim()
End With
Next
End If
End If
End Sub

reference2:

Scenario:

We have a text file and we need to read each row and the output would have multiple rows per row in the input file.

Input File:

1."201001","1;3"
2."201002","1;2;3;4"
Expected output:
1.2010-01-01  01
2.2010-01-03  03
3.2010-02-01  01
4.2010-02-02  02
5.2010-02-03  03
6.2010-02-04  04

Solution:

Check this post to see how to setup the Script Component.

Code to be used-

C#

01.using System;
02.using System.Data;
03.using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
04.using Microsoft.SqlServer.Dts.Runtime.Wrapper;
05. 
06.//Add this Namespace for IO Operations
07.using System.IO;
08. 
09.[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
10.public class ScriptMain : UserComponent
11.{
12.//StramReader to read the input file stream
13.private StreamReader textReader;
14. 
15.//String to save the source file path
16.private string srcFilePath;
17. 
18.public override void PreExecute()
19.{
20.base.PreExecute();
21.srcFilePath = @"I:\SSIS\BLOGS\One2Many\Input\one2many.txt";
22.//Set the textReader at the PreExecute Phase so that we donot initialize it for each record.
23.textReader = new StreamReader(srcFilePath);
24.}
25. 
26.public override void CreateNewOutputRows()
27.{
28.string nextLine;
29.string[] columns;
30.string[] col2Split;
31.string outputCol1;
32.char[] colDelimiters;
33.char[] rowDelimiters;
34.colDelimiters = ",".ToCharArray();
35.rowDelimiters = ";".ToCharArray();
36. 
37.//Read next line from the file to the string variable
38.nextLine = textReader.ReadLine();
39. 
40.//Read the file till nextLine variable is not NULL ie. EOF
41.while (nextLine != null && nextLine.Length>0)
42.{
43.//Split the records by commas to later extract the data
44.columns = nextLine.Split(colDelimiters);
45. 
46.//split the 2nd part of the string based on semi colun after removing the double quotes
47.col2Split = columns[1].Substring(1,columns[1].Length-2).Split(rowDelimiters);
48. 
49.//Format the column one as per the requirement
50.outputCol1 = columns[0].Substring(1, 4) + "-" + columns[0].Substring(5, 2) + "-";
51. 
52.foreach (string str in col2Split)
53.{               
54.string outputCol2 = "0"+str;
55. 
56.//Add output
57.this.OutputBuffer.AddRow();
58. 
59.//Add the column values for the row added above.
60.this.OutputBuffer.Col1 = outputCol1+ outputCol2;
61. 
62.//Left pad the 2nd coutput column with 0.
63.this.OutputBuffer.Col2 = outputCol2.Substring(outputCol2.Length-2);
64.}
65. 
66.//Read the next line
67.nextLine = textReader.ReadLine();
68.}
69.}
70. 
71.public override void PostExecute()
72.{
73.base.PostExecute();
74. 
75.//Close the Text reader once the file has been read in the PostExecute Phase.
76.textReader.Close();
77.}
78.}

VB.NET

01.using System;
02.Imports System.Data
03.Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
04.Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
05. 
06.'Add this Namespace for IO Operations
07.Imports System.IO
08. 
09.Public Class ScriptMain
10.Inherits UserComponent
11.'StramReader to read the input file stream
12.Private textReader As StreamReader
13. 
14.'String to save the source file path
15.Private srcFilePath As String
16. 
17.Public Overrides Sub PreExecute()
18.MyBase.PreExecute()
19.srcFilePath = "I:\SSIS\BLOGS\One2Many\Input\one2many.txt"
20.'Set the textReader at the PreExecute Phase so that we donot initialize it for each record.
21.textReader = New StreamReader(srcFilePath)
22.End Sub
23. 
24.Public Overrides Sub CreateNewOutputRows()
25.Dim nextLine As String
26.Dim columns As String()
27.Dim col2Split As String()
28.Dim outputCol1 As String
29.Dim colDelimiters As Char()
30.Dim rowDelimiters As Char()
31.colDelimiters = ",".ToCharArray()
32.rowDelimiters = ";".ToCharArray()
33. 
34.'Read next line from the file to the string variable
35.nextLine = textReader.ReadLine()
36. 
37.'Read the file till nextLine variable is not NULL ie. EOF
38.While nextLine IsNot Nothing AndAlso nextLine.Length > 0
39.'Split the records by commas to later extract the data
40.columns = nextLine.Split(colDelimiters)
41. 
42.'split the 2nd part of the string based on semi colun after removing the double quotes
43.col2Split = columns(1).Substring(1, columns(1).Length - 2).Split(rowDelimiters)
44. 
45.'Format the column one as per the requirement
46.outputCol1 = columns(0).Substring(1, 4) & "-" & columns(0).Substring(5, 2) & "-"
47. 
48.For Each str As String In col2Split
49.Dim outputCol2 As String "0" & str
50. 
51.'Add output
52.Me.OutputBuffer.AddRow()
53. 
54.'Add the column values for the row added above.
55.Me.OutputBuffer.Col1 = outputCol1 & outputCol2
56. 
57.'Left pad the 2nd coutput column with 0.
58.Me.OutputBuffer.Col2 = outputCol2.Substring(outputCol2.Length - 2)
59.Next
60. 
61.'Read the next line
62.nextLine = textReader.ReadLine()
63.End While
64.End Sub
65. 
66.Public Overrides Sub PostExecute()
67.MyBase.PostExecute()
68. 
69.'Close the Text reader once the file has been read in the PostExecute Phase.
70.textReader.Close()
71.End Sub
72.End Class
發佈了1 篇原創文章 · 獲贊 4 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章