ssis 角本组件更新数据
介绍 (Introduction)
SSIS Script component is one data transformation tasks in SQL Server Integration Services (SSIS). SSIS is an integration tool in the Microsoft BI family to extract data from heterogeneous data sources and transform it to your need. Apart from the standard data sources such as databases, text files, excel files, and web services, there can be instances where you need to retrieve non-traditional data sources. For example, let us say you want to extract the details of text files such as file sizes, created date, etc. In these types of scenarios, traditional data sources cannot be used.
SSIS脚本组件是SQL Server集成服务(SSIS)中的一项数据转换任务。 SSIS是Microsoft BI家族中的集成工具,可以从异构数据源中提取数据并将其转换为您的需求。 除了标准数据源(例如数据库,文本文件,excel文件和Web服务)外,在某些情况下您还需要检索非传统数据源。 例如,假设您要提取文本文件的详细信息,例如文件大小,创建日期等。在这些类型的方案中,不能使用传统的数据源。
This article explains how the SSIS Script component can be used as a data source in such instances. In simple terms, this component will use the functions and commands of the C# or languages.
本文介绍了在这种情况下如何将SSIS脚本组件用作数据源。 简单来说,该组件将使用C#或语言的功能和命令。
Before we start the discussion, it is important to note that there are two script related components in SSIS. One is the Script Task in the control flow, whereas the other is the script component in the Data flow task. We will be discussing the script component data flow task in this article.
在开始讨论之前,重要的是要注意SSIS中有两个与脚本相关的组件。 一个是控制流中的脚本任务 ,而另一个是数据流任务中的脚本组件。 我们将在本文中讨论脚本组件数据流任务。
Let us look at how the SSIS Script component can be used with a real-world example. Let us say we have a set of different files in multiple folders. Now we need to capture data such as file name, size, created date, and other details, as shown in the below screenshot.
让我们看看如何将SSIS脚本组件与实际示例结合使用。 假设我们在多个文件夹中有一组不同的文件。 现在,我们需要捕获数据,例如文件名,大小,创建日期和其他详细信息,如下面的屏幕快照所示。
Let us start the SSIS solution and drag and drop a data flow task to the control flow, and your screen should look like below.
让我们启动SSIS解决方案并将数据流任务拖放到控制流中,您的屏幕应如下图所示。
Next, double click the data flow task and drag and drop a script component flow to the data flow task. As soon as you drag and drop the script component, the following screen will appear for you to choose.
接下来,双击数据流任务并将脚本组件流拖放到数据流任务。 拖放脚本组件后,将出现以下屏幕供您选择。
This will decide what the type of the script component is. In this example, we will be using a Source Script Component Type. It is important to note that this configuration cannot be changed later. If you need to change the script component type, you need to drag and drop another component and reconfigure again. Therefore, before configuring the script component type, make sure you are selecting the correct configuration as per your requirement.
这将决定脚本组件的类型。 在此示例中,我们将使用源脚本组件类型。 重要的是要注意此配置以后不能更改。 如果需要更改脚本组件类型,则需要拖放另一个组件,然后重新配置。 因此,在配置脚本组件类型之前,请确保根据需要选择正确的配置。
Let us see the configuration of the SSIS Script Component as a data source. First, we will configure theScripttab.
让我们将SSIS脚本组件的配置视为数据源。 首先,我们将配置“脚本”选项卡。
We will be using Microsoft Visual C# as the scripting language, whereas you have the option of writing the script in Visual VB Script as well. However, once you start to write the script, you are not allowed to change the script language.
我们将使用Microsoft Visual C#作为脚本语言,而您也可以选择使用Visual VB脚本编写脚本。 但是,一旦开始编写脚本,就不允许更改脚本语言。
You can pass the read-only variable into the script so that there is higher maintainability. For example, we can configure the file path into an SSIS variable, and that can be modified from the package. If not, this has to be hardcoded into the script component that will become difficult to manage. This SSIS variable is configured inReadOnlyVariables,as shown in the above screenshot. Further, you can use theReadWriteVariablesto write a value that was generated inside the script component. We will not be using theReadWriteVariablesoption as it is not relevant to the example that we are discussing.
您可以将只读变量传递到脚本中,以便具有更高的可维护性。 例如,我们可以将文件路径配置为SSIS变量,并且可以从包中对其进行修改。 如果不是,则必须将其硬编码到脚本组件中,这将变得难以管理。 如上面的屏幕快照所示,在ReadOnlyVariables中配置了此SSIS变量。 此外,您可以使用ReadWriteVariables写入在脚本组件内部生成的值。 我们将不使用ReadWriteVariables选项,因为它与我们正在讨论的示例无关。
Next, we will configure theInputs and Outputstabs. Since the SSIS Script Component is configured as a source, we will have only output to be configured.
接下来,我们将配置“输入和输出”选项卡。 由于SSIS脚本组件被配置为源,因此我们只有要配置的输出。
In this configuration, output columns are defined along with the data type and data length. The following table shows the data type and length for each output column.
在此配置中,将定义输出列以及数据类型和数据长度。 下表显示了每个输出列的数据类型和长度。
Next is to create the script of the SSIS script component, which is the important configuration. By clicking theEdit Scriptbutton, you will be taken to the script editor, which is the visual studio editor. The following code is included.
接下来是创建SSIS脚本组件的脚本,这是重要的配置。 通过单击“编辑脚本”按钮,您将进入脚本编辑器,即Visual Studio编辑器。 包含以下代码。
public override void CreateNewOutputRows(){// Lock the variable for writeVariableDispenser variableDispenser = (VariableDispenser)this.VariableDispenser;variableDispenser.LockForRead("User::FilePath");IDTSVariables100 vars;variableDispenser.GetVariables(out vars);string filePath = vars["User::FilePath"].Value.ToString();// Unlock the variablevars.Unlock();foreach (string file in Directory.EnumerateFiles(filePath,"*.*",SearchOption.AllDirectories)){FileInfo oFileInfo = new FileInfo(file);OutputBuffer.AddRow();OutputBuffer.FileName = oFileInfo.FullName;OutputBuffer.DateCreated = oFileInfo.CreationTime;OutputBuffer.DateModified = oFileInfo.LastWriteTime;OutputBuffer.FileType = oFileInfo.Extension;OutputBuffer.Size = oFileInfo.Length; OutputBuffer.IsReadOnly = oFileInfo.IsReadOnly;}}
In the above script, FilePath is the SSIS variable that is used to store the file path. To implement the recursive search of files,SearchOption.AllDirectoriesis selected. OutputBuffer is the buffer that will be used to add the records for the source. Since this C#, you have the luxury of using many C# language features.
在上面的脚本中,FilePath是SSIS变量,用于存储文件路径。 要实现文件的递归搜索,请选择SearchOption.AllDirectories。 OutputBuffer是将用于添加源记录的缓冲区。 从此C#开始,您可以奢侈地使用许多C#语言功能。
Now let us create a table as shown in the below script, and data will be written to the database after the package is executed.
现在,让我们创建一个表,如下面的脚本所示,执行包后,数据将被写入数据库。
CREATE TABLE [FileList] ([FileName] varchar(1500),[FileType] varchar(50),[DateModified] datetime,[IsReadOnly] bit,[Size] bigint,[DateCreated] datetime)
Following is the file SSIS data flow after SQL Server destination is included.
以下是包含SQL Server目标之后的文件SSIS数据流。
When the package is executed, data will be stored into the FileList table, as shown in the below screenshot.
执行该软件包时,数据将存储到FileList表中,如下面的屏幕快照所示。
SSIS脚本组件中的其他配置 (Additional Configurations in SSIS Script Components)
After the script component is configured, typical data flow tasks can be used. For example, let us say we want to split the data set, depending on the size of the file.
配置脚本组件之后,可以使用典型的数据流任务。 例如,假设我们要根据文件大小拆分数据集。
Let us use SSIS Conditional Split to split the above data stream.
让我们使用SSIS条件拆分来拆分上述数据流。
The Conditional Split data flow task can be configured, as shown in the following screenshot.
可以配置条件拆分数据流任务,如以下屏幕截图所示。
As shown in the above screen, files that have a size of more than 2KB will be sent to one output, and the rest of the data will be into another output name.
如上面的屏幕所示,文件大小超过2KB的文件将被发送到一个输出,其余数据将被保存到另一个输出名称中。
Following is the SSIS data flow task after the inclusion of Conditional Split.
以下是包含条件拆分后的SSIS数据流任务。
In this configuration, both Morethan2KB and LessThan2KB output have the same attributes.
在此配置中,Morethan2KB和LessThan2KB输出具有相同的属性。
Similarly, all the data flow tasks can be used to the Output of the script component task.
同样,所有数据流任务都可以用于脚本组件任务的输出。
SSIS脚本组件中的多个输出路径 (Multiple Output paths in the SSIS Script Component)
The above implementation can be done differently inside the script component by defining multiple outputs.
通过定义多个输出,可以在脚本组件内部以不同方式完成上述实现。
Following is the way to configure multiple outputs in the SSIS script component.
以下是在SSIS脚本组件中配置多个输出的方法。
In the above configuration, two outputs,FileSizeLess2KBandFileSizeMore2KB, were added with the same columns and the same data types.
在以上配置中,两个输出FileSizeLess2KB和FileSizeMore2KB被添加了相同的列和相同的数据类型。
Next, we will be adding the script as shown below:
接下来,我们将添加脚本,如下所示:
foreach (string file in Directory.EnumerateFiles(filePath,"*.*",SearchOption.AllDirectories)){FileInfo oFileInfo = new FileInfo(file);if (oFileInfo.Length > 2048){FileSizeMore2KBBuffer.AddRow();FileSizeMore2KBBuffer.FileName = oFileInfo.FullName;FileSizeMore2KBBuffer.DateCreated = oFileInfo.CreationTime;FileSizeMore2KBBuffer.DateModified = oFileInfo.LastWriteTime;FileSizeMore2KBBuffer.FileType = oFileInfo.Extension;FileSizeMore2KBBuffer.Size = oFileInfo.Length;FileSizeMore2KBBuffer.IsReadOnly = oFileInfo.IsReadOnly; }else{FileSizeLess2KBBuffer.AddRow();FileSizeLess2KBBuffer.FileName = oFileInfo.FullName;FileSizeLess2KBBuffer.DateCreated = oFileInfo.CreationTime;FileSizeLess2KBBuffer.DateModified = oFileInfo.LastWriteTime;FileSizeLess2KBBuffer.FileType = oFileInfo.Extension;FileSizeLess2KBBuffer.Size = oFileInfo.Length;FileSizeLess2KBBuffer.IsReadOnly = oFileInfo.IsReadOnly;}}
In the above script, two buffers were added, depending on the size of the file. After both outputs were added, there will be two outputs from the SSIS Script component, as shown in the bellow SSIS package.
在上面的脚本中,根据文件的大小添加了两个缓冲区。 添加两个输出之后,SSIS脚本组件将有两个输出,如下面的SSIS包所示。
Similar to the SSIS conditional split, you will see the same numbers for the two outputs. Similar to the above example, there are similar instances where traditional data sources cannot be used.
与SSIS条件拆分类似,两个输出将看到相同的数字。 与以上示例相似,在某些相似的情况下,无法使用传统数据源。
In the script component, you have the ability to create two different outputs with different attributes, which was not possible with the SSIS Conditional Split data flow task. For example, for the image files, you might need width and height that are not required for the other files. In this example, we can add height and width attributes to the required output buffers.
在脚本组件中,您可以创建两个具有不同属性的不同输出,而这对于SSIS条件拆分数据流任务是不可能的。 例如,对于图像文件,您可能需要其他文件不需要的宽度和高度。 在此示例中,我们可以将height和width属性添加到所需的输出缓冲区。
However, during the development of the SSIS script component, it is important to make a copy of the script as a backup option.
但是,在开发SSIS脚本组件期间,重要的是制作一个脚本副本作为备份选项。
结论 (Conclusion)
In this article, we discussed the use of the SSIS Script component in order to generate non-traditional data sources apart from the traditional data sources. Since C# and has a rich set of commands, developers can use it for different purposes. This script component can be used to generate multiple outputs. In these multiple outputs, you can add different parameters to the different buffer outputs so that users have higher flexibility that the SSIS Conditional split.
在本文中,我们讨论了SSIS脚本组件的使用,以生成除传统数据源之外的非传统数据源。 由于C#和具有丰富的命令集,因此开发人员可以将其用于不同的目的。 该脚本组件可用于生成多个输出。 在这些多个输出中,可以将不同的参数添加到不同的缓冲区输出中,从而使用户比SSIS条件拆分具有更高的灵活性。
翻译自: /using-the-ssis-script-component-as-a-data-source/
ssis 角本组件更新数据