SSIS教程:創建簡單的ETL包 -- 1. 創建項目和基本包


在本課中,將創建一個簡單 ETL 包,該包可以從單個平面文件(Flat File)源中提取數據,使用兩個查找轉換組件轉換該數據,然后將該數據寫入AdventureWorksDW2012 的 FactCurrency 事實數據表中。 在本課中,您還將學習如何創建新包、添加和配置數據源和目標連接以及使用新的控制流和數據流組件。

AdventureWorksDW2012 下載地址:http://msftdbprodsamples.codeplex.com/releases/view/55330

Step 1:創建新的Integration Services項目

在開始菜單中找到SQL Server Data Tools並打開,在Microsoft SQL Server下。

創建新的Integration Services Project。

 

Step 2:添加和配置平面文件連接管理器

 

文件中的數據導入到目標庫,需要創建平面文件(Flat File)連接。(點擊文件連接,下載SQL2012.Integration_Services.Create_Simple_ETL_Tutorial.Sample.zip)

1. Connection Managers中右鍵,選擇New Flat File Connection...

2. 在File Name中點擊瀏覽,選中剛才下載文件中的SampleCurrencyData.txt文件

3. 取消"Column names in the first data row"復選框

4. 在Advanced頁簽,更改Column名和數據類型。

  • 將 Column 0 名稱屬性改為 AverageRate。類型改為float。

  • 將 Column 1 名稱屬性改為 CurrencyID。類型改為DT_WSTR。

  • 將 Column 2 名稱屬性改為 CurrencyDate。類型改為DT_DBDATE。

  • 將 Column 3 名稱屬性改為 EndOfDayRate。類型改為float。

 

Step 3:添加和配置OLE DB連接管理器

1. Connection Managers中右鍵,選擇New OLE DB Connection...

2. 點擊New,並連接上AdventureWorksDW2012數據庫

Step 4:將數據流任務添加到包

1. 單擊Control Flow,然后打開SSIS Toolbox

2. 將Data Flow Task拖拽到Control Flow,並Rename為Extract Sample Currency Data

Step 5:添加並配置平面文件源

1. 雙擊Step 4創建的Extract Sample Currency Data,會打開Data Flow頁簽。

2. 打開SSIS Toolbox => Other Source => Flat File Source,拖拽到Data Flow中,並Rename額外Extract Sample Currency Data。

Step 6:添加並配置查找轉換

添加Lookup轉換組件,給予DimCurrency表的CurrencyKey,該字段和平面文件的CurrencyID匹配。

  1. 在SSIS Toolbox中找到Lookup組件,拖拽到Data Folw中,並Rename為Lookup Currency Key

  2. 雙擊Lookup Currency Key,進行編輯

    General頁簽Cache Model選擇"Full cache",Connection Type選擇"OLE DB connection manage"

    Connection頁簽,OLE DB連接管理器選擇localhost.AdventureWorksDW2012,使用SQL查詢的結果,輸入如下SQL語句,

select * from (select * from [dbo].[DimCurrency]) as refTable
where [refTable].[CurrencyAlternateKey] = 'ARS'
OR
[refTable].[CurrencyAlternateKey] = 'AUD'
OR
[refTable].[CurrencyAlternateKey] = 'BRL'
OR
[refTable].[CurrencyAlternateKey] = 'CAD'
OR
[refTable].[CurrencyAlternateKey] = 'CNY'
OR
[refTable].[CurrencyAlternateKey] = 'DEM'
OR
[refTable].[CurrencyAlternateKey] = 'EUR'
OR
[refTable].[CurrencyAlternateKey] = 'FRF'
OR
[refTable].[CurrencyAlternateKey] = 'GBP'
OR
[refTable].[CurrencyAlternateKey] = 'JPY'
OR
[refTable].[CurrencyAlternateKey] = 'MXN'
OR
[refTable].[CurrencyAlternateKey] = 'SAR'
OR
[refTable].[CurrencyAlternateKey] = 'USD'
OR
[refTable].[CurrencyAlternateKey] = 'VEB'

  3. 在Columns中,將CurrencyID拖放到CurrencyAlternateKey上,並選中CurrencyKey的復選框。

 

  

  

添加Lookup轉換組件,給予DimDate表的DateKey,該字段和平面文件的CurrencyDate匹配。

  1. 在SSIS Toolbox中拖拽Lookup到Data Flow中,Rename為Lookup Date Key並放在Lookup Currency Key下面。

  2. 將Lookup Currency Key 和Lookup Date Key連接,Output選擇Lookup Match Output.

  3. 雙擊Lookup Date Key進行編輯

    Gerenal:選擇partial cache和OLE DB connection manager

    Connection:選中DimDate表

    Columns:將Currency Date拖放到FullDateAlternateKey上並選中DateKey的復選框。

  

  

Step 7:添加和配置OLE DB目標

  1. 將SSIS Toolbox => Other Destincations => OLE DB Destination拖放到Data Folw中,並Rename為Sample OLE DB Destination。

  2. 連接Lookup Date Key和Sample OLE DB Destination,Output選擇Lookup Match Output。

  3. 雙擊Sample OLE DB Destination,進行編輯

    Connection Manager:在Name of the table or the view中選擇[dbo].[FactCurrencyRate],點擊New,將腳本中的TableName更改為NewFactCurrencyRate。

    Mappings:所有源列都已經映射到了目標列。

  

  

Step 8:使ETL包更易於理解

  1. 選中所有的數據流組件,在菜單欄上進行Format =>Make Same Size => Both,Format => Align => Lefts

  2. 在Data Folw空白處點擊右鍵,Add annotation,輸入批注信息

  

Step 9:測試ETL包

  1. 工程完成后,控制流和數據流如下圖所示。

  2. 點擊Debug => Start Debugging,包開始運行,結果有1097個行被成功添加到NewFactCurrency表中

  

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM