Home Database Mysql Tutorial 在SSIS2012中使用CDC(数据变更捕获)

在SSIS2012中使用CDC(数据变更捕获)

Jun 07, 2016 pm 03:55 PM
cdc use change capture data new project

最新项目稍有空隙,开始研究SQL Server 2012和2014的一些BI特性,参照(Matt)的一个示例,我们开始体验SSIS中的CDC(Change Data Capture,变更数据捕获)。 注:如果需要了解关于SQL Server 2008中的CDC,请看这里http://blog.csdn.net/downmoon/article/deta

最新项目稍有空隙,开始研究SQL Server 2012和2014的一些BI特性,参照(Matt)的一个示例,我们开始体验SSIS中的CDC(Change Data Capture,变更数据捕获)。

注:如果需要了解关于SQL Server 2008中的CDC,请看这里http://blog.csdn.net/downmoon/article/details/7443627),本文假定读者对CDC的工作方式已有所了解。^_^。

我们分三步完成实例:

1、准备基础数据;

2、设计一个初始包;

3、在2的基础上设计一个增量包。

首先请完成以下准备安装:

(1)Visual studio 2012或Visual Studio 2012 Shell (Isolated) Redistributable Package

http://www.microsoft.com/en-us/download/details.aspx?id=30678

http://www.microsoft.com/en-us/download/details.aspx?id=30670

(2)SQL Server Data Tools - Business Intelligence for Visual Studio 2012

http://www.microsoft.com/zh-cn/download/details.aspx?id=36843

(2)SQL Server 2012企业版或开发版

http://www.microsoft.com/en-us/download/details.aspx?id=29066

(3)示例数据库AdventureWorksDW2012(本文必须,如果自建表则不必)

http://msftdbprodsamples.codeplex.com/releases/view/55330

好了,开始第一步:

/*
-- =============================================
-- 创建测试数据库及数据表,借助AdventureWorksDW2012示例数据库
---Generate By downmoon(邀月),3w@live.cn
-- =============================================
*/
--Create database CDCTest
--GO
--USE [CDCTest]
--GO

--SELECT * INTO DimCustomer_CDC
--FROM [AdventureWorksDW2012].[dbo].[DimCustomer]
--WHERE CustomerKey < 11500;

--select * from DimCustomer_CDC;
Copy after login
/* -- ============================================= -- 启用数据库级别CDC,只对企业版和开发版有效 ---Generate By downmoon(邀月),3w@live.cn -- ============================================= */ USE [CDCTest] GO EXEC sys.sp_cdc_enable_db GO -- add a primary key to the DimCustomer_CDC table so we can enable support for net changes IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[DimCustomer_CDC]') AND name = N'PK_DimCustomer_CDC') ALTER TABLE [dbo].[DimCustomer_CDC] ADD CONSTRAINT [PK_DimCustomer_CDC] PRIMARY KEY CLUSTERED ( [CustomerKey] ASC ) GO /* -- ============================================= -- 启用表级别CDC ---Generate By downmoon(邀月),3w@live.cn -- ============================================= */ EXEC sys.sp_cdc_enable_table @source_schema = N'dbo', @source_name = N'DimCustomer_CDC', @role_name = N'cdc_admin', @supports_net_changes = 1 GO
/* -- ============================================= -- 创建一个目标表,与源表(Source)有相同的表结构 --注意,在生产环境中,完全可以是不同的实例或服务器,本例为了方便,在同一个数据库实例的同一个数据库中演示 ---Generate By downmoon(邀月),3w@live.cn -- ============================================= */ SELECT TOP 0 * INTO DimCustomer_Destination FROM DimCustomer_CDC --select @@version; select * from DimCustomer_Destination; 
Copy after login

邀月工作室

第二步:创建初始包

-- =============================================
-- 我们使用两个包来完成示例,一个初始包完成数据的初始加载,一个增量包完成数据的变更捕获
---Generate By downmoon(邀月),3w@live.cn
-- ============================================= 
Copy after login

初始包包含如下逻辑:

新建一个SSIS项目,创建一个包“Initial Load”,如下图:

邀月工作室

新建两个CDC Control Task,分别命名为“CDC Control Task Start”和“CDC Control Task End”,分别对应属性为“Mark initial load start”和""Mark initial load end"

连接管理器均为ADO.NET方式,其他属性如下图:

邀月工作室

邀月工作室

中间加入一个“Data Flow Task”,属性默认。

邀月工作室

此时,运行包,可见CDC_States有初始标记。

邀月工作室

第三步:创建增量包

增量包包含如下逻辑:

在项目中创建一个新包,命名为“Incremental Load”

在包的"Control Flow"视图中,自上而下分别手动6个Task,顺序如下图,除去上面用到的三个Task,其余均为Execute SQL Task

邀月工作室

注意:CDC Control Task End的CDC运算符为MARK Process Range,CDC Control Task Start的CDC运算符为Get Process Range

其余4个Execute SQL Task的SQL语句如下:

--Create stage Tables
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N&#39;[dbo].[stg_DimCustomer_UPDATES]&#39;) AND type in (N&#39;U&#39;))
BEGIN
   SELECT TOP 0 * INTO stg_DimCustomer_UPDATES
   FROM DimCustomer_Destination
END

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N&#39;[dbo].[stg_DimCustomer_DELETES]&#39;) AND type in (N&#39;U&#39;))
BEGIN
   SELECT TOP 0 * INTO stg_DimCustomer_DELETES
   FROM DimCustomer_Destination
END
Copy after login
-- batch update UPDATE dest SET dest.FirstName = stg.FirstName, dest.MiddleName = stg.MiddleName, dest.LastName = stg.LastName, dest.YearlyIncome = stg.YearlyIncome FROM [DimCustomer_Destination] dest, [stg_DimCustomer_UPDATES] stg WHERE stg.[CustomerKey] = dest.[CustomerKey]
-- batch delete DELETE FROM [DimCustomer_Destination] WHERE[CustomerKey] IN ( SELECT [CustomerKey] FROM [dbo].[stg_DimCustomer_DELETES] )
Copy after login
-- truncate table truncate table [dbo].[stg_DimCustomer_DELETES] truncate table [dbo].[stg_DimCustomer_UPDATES]
Copy after login
最关键的一步,选中CDC Control Task Start,并切换到Data Flow,自上而下分别拖动CDC Source,CDC Splitter Transformer,三个ADO.NET Destination,如下图:
Copy after login

邀月工作室

其中三个的目标表分别为:[DimCustomer_Destination],stg_DimCustomer_DELETES,stg_DimCustomer_UPDATES。

邀月工作室

邀月工作室

而CDC Source的连接管理器属性如下图:

邀月工作室

此时,可运行增量包,但我们不会看到任何运行结果,因为此时我们还没有进行数据的Insert或Update操作。

下来我们提供一个脚本,测试下效果:

-- =============================================
-- 更新一些数据,以显示SSIS 2012中CDC的效果
---Generate By downmoon(邀月),3w@live.cn
-- =============================================

USE [CDCTest]
GO
 
-- Transfer the remaining customer rows
SET IDENTITY_INSERT DimCustomer_CDC ON
 
INSERT INTO DimCustomer_CDC
(
       CustomerKey, GeographyKey, CustomerAlternateKey, Title, FirstName, 
       MiddleName, LastName, NameStyle, BirthDate, MaritalStatus, 
       Suffix, Gender, EmailAddress, YearlyIncome, TotalChildren, 
       NumberChildrenAtHome, EnglishEducation, SpanishEducation,
       FrenchEducation, EnglishOccupation, SpanishOccupation, 
       FrenchOccupation, HouseOwnerFlag, NumberCarsOwned, AddressLine1, 
       AddressLine2, Phone, DateFirstPurchase, CommuteDistance
)
SELECT CustomerKey, GeographyKey, CustomerAlternateKey, Title, FirstName, 
       MiddleName, LastName, NameStyle, BirthDate, MaritalStatus, 
       Suffix, Gender, EmailAddress, YearlyIncome, TotalChildren, 
       NumberChildrenAtHome, EnglishEducation, SpanishEducation,
       FrenchEducation, EnglishOccupation, SpanishOccupation, 
       FrenchOccupation, HouseOwnerFlag, NumberCarsOwned, AddressLine1, 
       AddressLine2, Phone, DateFirstPurchase, CommuteDistance
FROM [AdventureWorksDW2012].[dbo].[DimCustomer]
WHERE CustomerKey =11502
 
SET IDENTITY_INSERT DimCustomer_CDC OFF
GO
 
-- give 10 people a raise
UPDATE DimCustomer_CDC 
SET 
    YearlyIncome = YearlyIncome + 10
WHERE
    CustomerKey >= 11000 AND CustomerKey <= 11010
 
GO
Copy after login

此时,我们可以看到变更捕获的结果:

 

邀月工作室

如果您觉得还不够直观,请"Enable Data Viewer",

邀月工作室

邀月工作室

至此,一个SSIS 2012中CDC的实例演示结束,如果还有进一步的研究,请移驾MSDN,下面有链接。本文也提供示例项目包,以作研究之用。

项目文件下载1,项目文件下载2

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use ddrescue to recover data on Linux Use ddrescue to recover data on Linux Mar 20, 2024 pm 01:37 PM

DDREASE is a tool for recovering data from file or block devices such as hard drives, SSDs, RAM disks, CDs, DVDs and USB storage devices. It copies data from one block device to another, leaving corrupted data blocks behind and moving only good data blocks. ddreasue is a powerful recovery tool that is fully automated as it does not require any interference during recovery operations. Additionally, thanks to the ddasue map file, it can be stopped and resumed at any time. Other key features of DDREASE are as follows: It does not overwrite recovered data but fills the gaps in case of iterative recovery. However, it can be truncated if the tool is instructed to do so explicitly. Recover data from multiple files or blocks to a single

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Apr 03, 2024 pm 12:04 PM

0.What does this article do? We propose DepthFM: a versatile and fast state-of-the-art generative monocular depth estimation model. In addition to traditional depth estimation tasks, DepthFM also demonstrates state-of-the-art capabilities in downstream tasks such as depth inpainting. DepthFM is efficient and can synthesize depth maps within a few inference steps. Let’s read about this work together ~ 1. Paper information title: DepthFM: FastMonocularDepthEstimationwithFlowMatching Author: MingGui, JohannesS.Fischer, UlrichPrestel, PingchuanMa, Dmytr

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Apr 01, 2024 pm 07:46 PM

The performance of JAX, promoted by Google, has surpassed that of Pytorch and TensorFlow in recent benchmark tests, ranking first in 7 indicators. And the test was not done on the TPU with the best JAX performance. Although among developers, Pytorch is still more popular than Tensorflow. But in the future, perhaps more large models will be trained and run based on the JAX platform. Models Recently, the Keras team benchmarked three backends (TensorFlow, JAX, PyTorch) with the native PyTorch implementation and Keras2 with TensorFlow. First, they select a set of mainstream

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

See all articles