Developing a Custom Destination Component
Applies To: SQL Server 2016 Preview
Microsoft SQL Server Integration Services gives developers the ability to write custom destination components that can connect to and store data in any custom data source. Custom destination components are useful when you need to connect to data sources that cannot be accessed by using one of the existing source components included with Integration Services.
Destination components have one or more inputs and zero outputs. At design time, they create and configure connections and read column metadata from the external data source. During execution, they connect to their external data source and add rows that are received from the components upstream in the data flow to the external data source. If the external data source exists prior to execution of the component, the destination component must also ensure that the data types of the columns that the component receives match the data types of the columns at the external data source.
This section discusses the details of how to develop destination components, and provides code examples to clarify important concepts. For a general overview of data flow component development, see Developing a Custom Data Flow Component.
Implementing the design-time functionality of a destination component involves specifying a connection to an external data source and validating that the component has been correctly configured. By definition, a destination component has one input and possibly one error output.
Destination components connect to external data sources by using ConnectionManager objects defined in a package. The destination component indicates its requirement for a connection manager to the SSIS Designer, and to users of the component, by adding an element to the RuntimeConnectionCollection collection of the ComponentMetaData. This collection serves two purposes: first, it advertises the need for a connection manager to SSIS Designer; then, after the user has selected or created a connection manager, it holds a reference to the connection manager in the package that is being used by the component. When an IDTSRuntimeConnection100 is added to the collection, the Advanced Editor displays the Connection Properties tab, to prompt the user to select or create a connection in the package for use by the component.
Imports System Imports System.Data Imports System.Data.SqlClient Imports Microsoft.SqlServer.Dts.Pipeline Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper Imports Microsoft.SqlServer.Dts.Runtime Namespace Microsoft.Samples.SqlServer.Dts <DtsPipelineComponent(DisplayName:="Destination Component", ComponentType:=ComponentType.DestinationAdapter)> _ Public Class DestinationComponent Inherits PipelineComponent Public Overrides Sub ProvideComponentProperties() ' Reset the component. Me.RemoveAllInputsOutputsAndCustomProperties() ComponentMetaData.RuntimeConnectionCollection.RemoveAll() Dim input As IDTSInput100 = ComponentMetaData.InputCollection.New() input.Name = "Input" Dim connection As IDTSRuntimeConnection100 = ComponentMetaData.RuntimeConnectionCollection.New() connection.Name = "ADO.net" End Sub End Class End Namespace
After a connection is added to the RuntimeConnectionCollection, you override the AcquireConnections method to establish a connection to the external data source. This method is called at design time and at run time. The component must establish a connection to the connection manager specified by the run-time connection, and subsequently, to the external data source. Once established, the component should cache the connection internally and release it when ReleaseConnections is called. Developers override this method, and release the connection established by the component during AcquireConnections. Both of these methods, ReleaseConnections and AcquireConnections, are called at design time and at run time.
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper Private sqlConnection As SqlConnection Public Overrides Sub AcquireConnections(ByVal transaction As Object) If IsNothing(ComponentMetaData.RuntimeConnectionCollection(0).ConnectionManager) = False Then Dim cm As ConnectionManager = DtsConvert.GetWrapper(ComponentMetaData.RuntimeConnectionCollection(0).ConnectionManager) Dim cmado As ConnectionManagerAdoNet = CType(cm.InnerObject,ConnectionManagerAdoNet) If IsNothing(cmado) Then Throw New Exception("The ConnectionManager " + cm.Name + " is not an ADO.NET connection.") End If sqlConnection = CType(cmado.AcquireConnection(transaction), SqlConnection) sqlConnection.Open() End If End Sub Public Overrides Sub ReleaseConnections() If IsNothing(sqlConnection) = False And sqlConnection.State <> ConnectionState.Closed Then sqlConnection.Close() End If End Sub
Destination component developers should perform validation as described in Component Validation. In addition, they should verify that the data type properties of the columns defined in the component's input column collection match the columns at the external data source. At times, verifying the input columns against the external data source can be impossible or undesirable, such as when the component or the SSIS Designer is in a disconnected state, or when round trips to the server are not acceptable. In these situations, the columns in the input column collection can still be validated by using the ExternalMetadataColumnCollection of the input object.
This collection exists on both input and output objects and must be populated by the component developer from the columns at the external data source. This collection can be used to validate the input columns when the SSIS Designer is offline, when the component is disconnected, or when the ValidateExternalMetadata property is false.
The following sample code adds an external metadata column based on an existing input column.
Private Sub AddExternalMetaDataColumn(ByVal input As IDTSInput100, ByVal inputColumn As IDTSInputColumn100) ' Set the properties of the external metadata column. Dim externalColumn As IDTSExternalMetadataColumn100 = input.ExternalMetadataColumnCollection.New() externalColumn.Name = inputColumn.Name externalColumn.Precision = inputColumn.Precision externalColumn.Length = inputColumn.Length externalColumn.DataType = inputColumn.DataType externalColumn.Scale = inputColumn.Scale ' Map the external column to the input column. inputColumn.ExternalMetadataColumnID = externalColumn.ID End Sub
During execution, the destination component receives a call to the ProcessInput method each time a full PipelineBuffer is available from the upstream component. This method is called repeatedly until there are no more buffers available and the EndOfRowset property is true. During this method, destination components read columns and rows in the buffer, and add them to the external data source.
The input buffer for a component contains all the columns defined in the output column collections of the components upstream from the component in the data flow. For example, if a source component provides three columns in its output, and the next component adds an additional output column, the buffer provided to the destination component contains four columns, even if the destination component will write only two columns.
The order of the columns in the input buffer is not defined by the index of the column in the input column collection of the destination component. Columns can be reliably located in a buffer row only by using the FindColumnByLineageID method of the BufferManager. This method locates the column that has the specified lineage ID in the specified buffer, and returns its location in the row. The indexes of the input columns are typically located during the PreExecute method, and cached by the developer for use later during ProcessInput.
The following code example finds the location of the input columns in the buffer during PreExecute and stores them in an array. The array is subsequently used during ProcessInput to read the values of the columns in the buffer.
Private cols As Integer() Public Overrides Sub PreExecute() Dim input As IDTSInput100 = ComponentMetaData.InputCollection(0) ReDim cols(input.InputColumnCollection.Count) For x As Integer = 0 To input.InputColumnCollection.Count cols(x) = BufferManager.FindColumnByLineageID(input.Buffer, input.InputColumnCollection(x).LineageID) Next x End Sub
Once the input columns have been located in the buffer, they can be read and written to the external data source.
While the destination component writes rows to the external data source, you may want to update the "Rows read" or "BLOB bytes read" performance counters by calling the IncrementPipelinePerfCounter method. For more information, see Performance Counters.
The following example shows a component that reads rows from the buffer provided in ProcessInput. The indexes of the columns in the buffer were located during PreExecute in the preceding code example.
Public Overrides Sub ProcessInput(ByVal inputID As Integer, ByVal buffer As PipelineBuffer) While (buffer.NextRow()) For Each col As Integer In cols If buffer.IsNull(col) = False Then ' TODO: Read the column data. End If Next col End While End Sub
The following sample shows a simple destination component that uses a File connection manager to save binary data from the data flow into files. This sample does not demonstrate all the methods and functionality discussed in this topic. It demonstrates the important methods that every custom destination component must override, but does not contain code for design-time validation.
Imports System Imports System.IO Imports Microsoft.SqlServer.Dts.Pipeline Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper Namespace BlobDst <DtsPipelineComponent(DisplayName="BLOB Extractor Destination", Description="Writes values of BLOB columns to files")> _ Public Class BlobDst Inherits PipelineComponent Private m_DestDir As String Private m_FileNameColumnIndex As Integer = -1 Private m_BlobColumnIndex As Integer = -1 Public Overrides Sub ProvideComponentProperties() Dim input As IDTSInput100 = ComponentMetaData.InputCollection.New input.Name = "BLOB Extractor Destination Input" input.HasSideEffects = True Dim conn As IDTSRuntimeConnection100 = ComponentMetaData.RuntimeConnectionCollection.New conn.Name = "FileConnection" End Sub Public Overrides Sub AcquireConnections(ByVal transaction As Object) Dim conn As IDTSRuntimeConnection100 = ComponentMetaData.RuntimeConnectionCollection(0) m_DestDir = CType(conn.ConnectionManager.AcquireConnection(Nothing), String) If m_DestDir.Length > 0 AndAlso Not (m_DestDir(m_DestDir.Length - 1) = "\"C) Then m_DestDir += "\" End If End Sub Public Overrides Function SetUsageType(ByVal inputID As Integer, ByVal virtualInput As IDTSVirtualInput100, ByVal lineageID As Integer, ByVal usageType As DTSUsageType) As IDTSInputColumn100 Dim inputColumn As IDTSInputColumn100 = MyBase.SetUsageType(inputID, virtualInput, lineageID, usageType) Dim custProp As IDTSCustomProperty100 custProp = inputColumn.CustomPropertyCollection.New custProp.Name = "IsFileName" custProp.Value = CType(False, Object) custProp = inputColumn.CustomPropertyCollection.New custProp.Name = "IsBLOB" custProp.Value = CType(False, Object) Return inputColumn End Function Public Overrides Sub PreExecute() Dim input As IDTSInput100 = ComponentMetaData.InputCollection(0) Dim inputColumns As IDTSInputColumnCollection100 = input.InputColumnCollection Dim custProp As IDTSCustomProperty100 For Each column As IDTSInputColumn100 In inputColumns custProp = column.CustomPropertyCollection("IsFileName") If CType(custProp.Value, Boolean) = True Then m_FileNameColumnIndex = CType(BufferManager.FindColumnByLineageID(input.Buffer, column.LineageID), Integer) End If custProp = column.CustomPropertyCollection("IsBLOB") If CType(custProp.Value, Boolean) = True Then m_BlobColumnIndex = CType(BufferManager.FindColumnByLineageID(input.Buffer, column.LineageID), Integer) End If Next End Sub Public Overrides Sub ProcessInput(ByVal inputID As Integer, ByVal buffer As PipelineBuffer) While buffer.NextRow Dim strFileName As String = buffer.GetString(m_FileNameColumnIndex) Dim blobLength As Integer = CType(buffer.GetBlobLength(m_BlobColumnIndex), Integer) Dim blobData As Byte() = buffer.GetBlobData(m_BlobColumnIndex, 0, blobLength) strFileName = TranslateFileName(strFileName) Dim fi As FileInfo = New FileInfo(strFileName) ' Make sure directory exists before creating file. If Not fi.Directory.Exists Then fi.Directory.Create End If ' Write the data to the file. Dim fs As FileStream = New FileStream(strFileName, FileMode.Create, FileAccess.Write, FileShare.None) fs.Write(blobData, 0, blobLength) fs.Close End While End Sub Private Function TranslateFileName(ByVal fileName As String) As String If fileName.Length > 2 AndAlso fileName(1) = ":"C Then Return m_DestDir + fileName.Substring(3, fileName.Length - 3) Else Return m_DestDir + fileName End If End Function End Class End Namespace
| Stay Up to Date with Integration Services|
For the latest downloads, articles, samples, and videos from Microsoft, as well as selected solutions from the community, visit the Integration Services page on MSDN:
Visit the Integration Services page on MSDN
For automatic notification of these updates, subscribe to the RSS feeds available on the page.