如何在处理ssis中的文件之前比较列结构 [英] how to compare columns structure before processing files in ssis

查看:91
本文介绍了如何在处理ssis中的文件之前比较列结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我对ssis有一个疑问



我希望每天使用ssis将3个不同的元数据文件加载到3个不同的表中。

源路径文件名称中的日复一日变得不同&b;

样本文件。

第一天的文件是a.txt和b .txt和c.txt 

第二天的文件是xy.txt和yz.txt以及za.txt 

第三天的文件来自其他一些不同的名字。

超过3天是样本filesnames.file名称不会出现它会有不同的名称。

这里的文件应该是3个不同文件的不同列。

所以在这里我需要在使用ssis处理源文件之前找到列结构比较(源到目标)。




sourcepath :  c:\\\\
$
源路径有文件1)a.txt   

                        2)b.txt

                        3)c.txt 

a.txt文件结构:id | name 

b.txt文件结构:id |名字| deptno

c.txt文件结构:id | name  | Locaiton | sal | deptno

目标有  3表:1)emp和结构:id | name 

                        2)emp_dept和结构:id | name | deptno

                        3)emp_sal和结构:id | name | location | sal | deptno



有时文件名来的名称不同,处理前的那个时候我们需要比较源到目标列结构

然后处理它。

这里我按照下面的步骤使用ssis 



in controal流程:拖放1 dft并转到此处的数据流我使用3个不同的flatfile源并创建3个不同的平面文件连接

第二种方式我花了3 dft 每个数据流任务我创建一个平面文件soruce连接并配置destionaton表。



然后 将1flat文件源配置为emp tabe 和empflade的第三个平面文件连接的2flatfilesource连接和用于将已处理文件移动到存档文件夹的文件系统任务之后的


。$

当我第一天运行时,
其工作文件如果我第二天运行相同的包裹,则为


 包将失败,因为文件名不同,并且源路径中不存在flatfile连接配置文件名。



日常文件名是 来的不一样,你可以告诉我怎么样?
在加载到表之前比较列结构以及如何确定哪个源文件需要加载哪个源b
请告诉我如何在ssis中执行此任务。







$
           
$



解决方案

由于文件的元数据不同而且没有固定的命名约定来识别每组文件,我会像下面这样解决这个问题



1。使用文件枚举器添加For Each循环以指向您的目录( C:\test\ )。通过设置
完全限定选项(让变量为@ [User :: Filename])


2,在里面添加一个变量来获取文件名。内部循环添加具有平面文件源和oledb目标的数据流任务。 


对于平面文件源,将connectionstring的表达式设置为指向上面创建的@ [User :: Filename]变量


内部平面文件连接管理器删除列分隔符,只需添加行分隔符。这将导致整行作为单个列传输到您创建的表(具有单列数据的登台表和
int类型的文件类型列)并使用OLEDB目标指向


3。在数据流任务之后执行执行sql任务以检查列数和相应的设置文件类型值



1用于文件类型1 with  id | name 


2适用于  id | name | deptno


和 


3适用于 id | name  | Locaiton | sal | deptno


4。使用OLEDB源指向您创建的表的另一个数据流任务。使用查询作为源来根据分隔符(例如,)分割值。然后将值分离到多列


所以像

 SELECT t.FileType,f.Value AS Data $ b这样的查询$ b FROM YourSingleDataColumnTable t 
CROSS APPLY String_Split(t.Yourdatacolumn,',')f


然后添加条件拆分任务,根据文件类型定义三个输出。每个输出链接到相应的目标表(


1 - 表格与
id | name 


2- table with  id | name | deptno

3-table with id | name | location | sal | deptno




这将确保每次执行包时它会根据您设置的类型值识别行并将它们移动到corresp目的地中的表格


唯一要确保的是,即使缺少值也会有分隔符,否则列数会出错并且数据最终会出现在错误的表格中


Hi I have one doubt in ssis

I want load every day 3 different metadata files into 3 different table using ssis.
day by day in the source path files name are coming different 

for sample files.
1st day files are a.txt and b.txt and c.txt 
2nd day files are xy.txt and yz.txt and za.txt 
3rd day files are comes some other different name.
above 3 days are sample filesnames.file names not come exactely it will come different names.
here inside files should be diffrent columns for 3 different files.
so here I need to find columns structure comparision(source to target) before processing the source files using ssis.


sourcepath :  c:\test\
source path have files 1)a.txt   
                       2)b.txt
                       3)c.txt 
a.txt file structure : id |name 
b.txt file structure : id | name | deptno
c.txt file structure : id |name  | Locaiton|sal|deptno
Target have  3 tables : 1)emp and structure : id|name 
                        2)emp_dept and structure : id|name |deptno
                        3)emp_sal and structure : id|name |location|sal|deptno

some times file names are comes different name, that time before processing we need compare source to target columns structure
then process it.
here I follow the steps like below using ssis 

in controal flow: drag and drop 1 dft and go the dataflow here I used 3 differet flatfile source and create 3 diferent flat file connections
2nd way I took 3 dft  and each dataflow task I create one flat file soruce connection and configure to destionaton tables.

then  conffigure to 1flat file source to emp tabe  and 2flatfilesource connection to emp_dept and 3rd flatfile soure connection to emp_sal

after that used file system task for move processed files into archive folder .

its working file when i run the first day

if i run same package next day  package will failed due files names are comes different and flatfile connection configruation file names are not exist in the sourcepath.

day by day file names are  comes different ,
can you please tell me how to  compare columns structure before loading to the table and how will I decide which source file which table need to load
please tell me how to achive this task in ssis .




           


解决方案

Since the metadata of files are different and there's no fixed naming convention to identify each set of files, I would approach this problem like below

1. Add a For Each loop with file enumerator to point to your directory (c:\test\). Add a variable inside to get filenames by setting Fully Qualified option (let variable be @[User::Filename])

2. Inside loop add a data flow task with flat file source and oledb destination. 

For flat file source set an expression for connectionstring to point to @[User::Filename] variable created above

Inside flat file connection manager remove column delimiter and just add row delimiter alone. This would cause entire row to be transferred as a single column to a table you create (staging table with single column for data and a filetype column of type int) and point using OLEDB destination

3. Have a execute sql task after data flow task to check for the rows for column count and set filetype value accordingly 

i.e

1 for file type 1 with id |name 

2 for files with id|name |deptno

and 

3 for files with id |name  | Locaiton|sal|deptno

4. Have another data flow task with OLEDB source pointing to the table you created. Use a query as the source to split the values based on delimiter (for example ,). Then separate out values to multiple columns

So a query like

SELECT t.FileType,f.Value AS Data
FROM YourSingleDataColumnTable t
CROSS APPLY String_Split(t.Yourdatacolumn,',')f

Then add a conditional split task after to define three outputs based on file type. Each output link to corresponding destination table (

1 - table with id|name 
2- table with  id|name |deptno
3- table with id|name |location|sal|deptno

)

This will ensure everytime package is executing it identifies rows based on type value you set and move them to corresponding tables in the destination

Only thing to make sure is that you will have delimiters even for missing value otherwise column count goes wrong and data will end up in wrong table


这篇关于如何在处理ssis中的文件之前比较列结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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