SSIS - 如何加载从文本文件数据,其中文件的路径里面另一个文本文件? [英] SSIS - How do I load data from text files where the path of files is inside another text file?

查看:1708
本文介绍了SSIS - 如何加载从文本文件数据,其中文件的路径里面另一个文本文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含的文件加载到数据库的列表的文本文件

I have a text file that contains a list of files to load into database.

该列表包含两列:

FilePath,Type
c:\f1.txt,A
c:\f2.txt,B
c:\f3.txt,B

我要提供此文件作为源SSIS。然后,我希望它通过线通过它去行。对于每一行,我希望它来读取文件路径列中的文件,并检查类型。

I want to provide this file as the source to SSIS. I then want it to go through it line by line. For each line, I want it to read the file in the FilePath column and check the Type.

如果类型为A,则我希望它忽略第一个4行位于在当前行的文件路径柱,然后将文件加载数据的其余部分中的表中的文件内。
。如果类型为B,则我希望它打开的文件和文件的第一列复制到表1和第二列到表2的所有行。

If type is A then I want it to ignore the first 4 lines of the file that is located at the FilePath column of the current line and then load rest of the data inside that file in a table. If type is B then I want it to open the file and copy first column of the file into table 1 and second column into table 2 for all of the lines.

我会很感激,如果有人可以请提供给我的,我需要按照步骤高水平的列表。

I would really appreciate if someone can please provide me a high level list of steps I need to follow.

任何帮助表示赞赏。

推荐答案

下面是SSIS中做的一种方式。下面的步骤是相对于的 SSIS 2008 R2

Here is one way of doing it within SSIS. Below steps are with respect to SSIS 2008 R2.


  • 创建一个SSIS包,并分别建立三个包变量文件名 FilesToRead 键入。 FilesToRead变量将保持的文件的列表和它们的类型的信息。我们将有一个循环,将逐一对这些记录和信息存储在的文件名的和的键入的变量每次都遍历。

  • Create an SSIS package and create three package variables namely FileName, FilesToRead and Type. FilesToRead variable will hold the list of files and their types information. We will have a loop that will go through each of those records and store the information in FileName and Type variables every time it loops through.


  • 在控制流选项卡,将一个数据流任务之后foreach循环容器。数据流任务将读取包含有要处理的文件列表中的文件。然后循环将通过每个文件。控制流选项卡最终会是这个样子。现在,会有误差,因为没有什么配置。我们将获得该表格。

控制流


  • 在连接管理器部分,您需要四个连接。

  • 首先,你需要一个OLE DB连接来连接到数据库。命名此为 的SQLServer

  • 二,平面文件连接管理器来读取包含文件文件和类型的列表。此平面文件连接管理器将包含分别配置两列文件名键入命名此为 文件

  • 三,另一个平面文件的连接管理器来读取A型名称的所有文件,以此作为 Type_A 。在此平面文件连接管理器,在文本框中输入值4 标题行跳过这样第一四行总是跳过。

  • 四,多了一个平面文件的连接管理器读取类型B.名称以此为 Type_B
  • $ b中的所有文件$ b
  • On the connection manager section, you need four connections.
  • First, you need an OLE DB connection to connect to the database. Name this as SQLServer.
  • Second, a flat file connection manager to read the file that contains the list of files and types. This flat file connection manager will contain two columns configured namely FileName and Type Name this as Files.
  • Third, another flat file connection manager to read all files of type A. Name this as Type_A. In this flat file connection manager, enter the value 4 in the text box Header rows to skip so that the first four rows are always skipped.
  • Fourth, one more flat file connection manager to read all files of type B. Name this as Type_B.


  • 让我们回到以控制流量。对第一个数据流任务双击。数据流任务内,放置,将使用连接管理器读取所有文件的平面文件源 文件 ,然后将记录目标。配置在记录目标变量 FilesToRead 。你的第一个数据流任务想,如下图所示。

  • Let's get back to control flow. Double-click on the first data flow task. Inside the data flow task, place a flat file source that would read all the files using the connection manager Files and then place a Recordset Destination. Configure the variable FilesToRead in the recordset destination. Your first data flow task would like as shown below.


  • 现在,让我们回去再控制流选项卡。配置ForEach循环如下所示。这个循环将通过存储在变量 FilesToRead 记录。因为,记录包含两列,每一个记录通过循环时,变量文件名键入将被分配当前记录的值。

  • Now, let's go back to control flow tab again. Configure the ForEach loop as shown below. This loop will go through the recordset stored in the variable FilesToRead. Since, the recordset contains two columns, each time a record is looped through, the variables FileName and Type will be assigned the value of the current record.

为每个集合


  • 里面,每一个循环的容器中,有两个数据流任务即 A型文件 B型文件。每个根据您的要求,这些数据流任务时,可以配置为从连接管理器读取这些文件。但是,我们需要根据正在读取的文件以禁用任务。

  • A类文件数据流任务应该是当处理一个类型的文件才会启用。

  • 同样, b型文件数据流任务应启用只有当b类型的文件正在处理中。

  • 要实现这一点,请点击 A类文件数据流任务,按F4带来的属性。点击省略号的按钮上的表达属性中。

  • 在属性表达式编辑器,选择停用属性和输入表达式!(@ [用户:: TYPE] ==A)

  • Inside, the for each loop container, there are two data flow tasks namely Type A files and Type B files. You can configure each of these data flow tasks according to your requirements to read the files from connection managers. However, we need to disable the tasks based on the file that is being read.,
  • Type A files data flow task should be enabled only when A type files are being processed.
  • Similarly, Type B files data flow task should be enabled only when B type files are being processed.
  • To achieve this, click on the Type A files data flow task and press F4 to bring the properties. Click on the Ellipsis button available on the Expression property.
  • On the Property Expressions Editor, select Disable Property and enter the expression !(@[User::Type] == "A")


  • 同样,点击类型乙文件数据流任务,按F4带来的属性。点击省略号的按钮上的表达属性中。

  • 在属性表达式编辑器,选择停用属性和输入表达式!(@ [用户:: TYPE] ==b)

  • Similarly, click on the Type B files data flow task and press F4 to bring the properties. Click on the Ellipsis button available on the Expression property.
  • On the Property Expressions Editor, select Disable Property and enter the expression !(@[User::Type] == "B")


  • 这是一个只包含一个类型的文件样本Files.txt在列表中。当执行包读取这个文件,你会发现,只有 A类文件数据流任务。

  • Here is a sample Files.txt containing only A type file in the list. When the package is executed to read this file, you will notice that only the Type A files data flow task.


  • 这是包含在列表中只有b型文件的另一个样本Files.txt。当执行包读取这个文件,你会发现,只有 B型文件数据流任务。

  • Here is another sample Files.txt containing only B type files in the list. When the package is executed to read this file, you will notice that only the Type B files data flow task.


  • 如果Files.txt同时包含A和b类型的文件,该循环将执行基于正在处理的文件类型相应的数据流任务。


    <李>让我们假设你的A型平面文件有三栏布局像与逗号分隔值如下所示。这里的文件数据,使用记事本++的所有特殊字符显示。 CR LF 表示,该行与回车和换行结束。此文件存放的路径C:\f1.txt
  • Let's assume that your flat files of type A have three column layout like as shown below with comma separated values. The file data here is shown using Notepad++ with all special characters. CR LF denotes that the lines are ending with Carriage return and Line Feed. This file is stored in the path C:\f1.txt


  • 我们需要一个数据库表中导入数据。让我们创建一个在SQL Server数据库名为 dbo.Table_A 表如下所示。

  • We need a table in the database to import the data. Let's create a table named dbo.Table_A in the SQL Server database as shown here.


  • 现在,到SSIS包。下面是配置名为* Type_A *平面文件连接管理器的详细信息。给一个名字到连接管理器。您需要在标题行指定的值4跳过文本框。你的平面文件连接管理器应该是这个样子。


  • 在高级选项卡,你可以重命名的列名,如果你想。


  • 现在,连接管理器配置,我们需要配置数据流任务 A类文件来处理相应的文件。在数据流任务双击code> A类文件。将里面的任务平面文件源和OLE DB目标

  • Now that the connection manager is configured, we need to configure data flow task Type A files to process the corresponding files. Double-click on the data flow task Type A files. Place a Flat file source and OLE DB Destination inside the task.


  • 平面文件源必须配置为从读取文件平面文件连接管理器。


  • 数据流任务没有做什么特别的事情。它只是读取A型的平面文件和数据插入到表dbo.Table_A。现在,我们需要配置OLE DB目标将数据插入到数据库中。在平面文件的连接管理器和配置表的列名不一样。因此,他们必须手动映射


  • 现在,数据流任务进行配置。我们必须让从Files.txt正在读取的文件路径是否正确传递。要做到这一点,点击Type_A平面文件连接管理器,然后按F4带来的属性。将 DelayValidation 属性。点击上的表达式的省略号按钮属性。

  • Now, that the data flow task is configured. We have to make that the file path being read from the Files.txt is passed correctly. To do this, click on the Type_A flat file connection manager and press F4 to bring the properties. Set the DelayValidation property to True. Click on the Ellipsis button on the Expressions property.


  • 在属性表达式生成器中,选择的ConnectionString 属性并将其设置为表达 @ [用户::文件名]

  • On the Property Expression builder, select ConnectionString property and set it to the Expression @[User::FileName]

表情


  • 下面是一个包含A型样本Files.txt文件中的文件而已。


  • 下面是样本类型的文件f01.txt和f02.txt


  • 包执行后,下面的数据会在表中找到表-A

表中的数据


  • 上面提到的配置步骤也应遵循的b类文件。然而,数据流任务看起来略有不同,因为该文件处理逻辑是不同的。数据流任务B类文件会是这样的。既然你已经在B类文件中的两列插入到不同的表。你必须使用组播改造,将创建输入数据的克隆。可以使用各多播输出的通过,从而使不同的转化或目的地

希望帮助你实现你的任务。

Hope that helps you to achieve your task.

这篇关于SSIS - 如何加载从文本文件数据,其中文件的路径里面另一个文本文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆