Change Data Capture Using Snowflake Streams - by Alexander - Snowflake - Medium
Change Data Capture Using Snowflake Streams - by Alexander - Snowflake - Medium
Alexander
Oct 18, 2022 · 5 min read · Listen
Snowflake Streams capture an initial snapshot of all the rows present in the
source table as the current version of the table with respect to an initial point
in time. Streams then enable Change Data Capture every time you insert,
update, or delete data in your source table. The Streams will have additional
columns whenever any DML change is committed. So, by capturing the CDC
Events you can easily merge just the changes from source to target using the
MERGE statement.
-- DESTINATION TABLE
CREATE OR REPLACE TABLE TEST_DESTINATION(
ID INT,
FIRSTNAME VARCHAR(100),
LASTNAME VARCHAR(100));
Tables present in the database (SHOW TABLES)
SHOW STREAMS
For the purpose of this demonstration, let’s add a couple of records to the
TEST_SOURCE table. Run the following command to do so.
Step 3: Let’s view the change log in the stream before proceeding.
As you can see, here are the records and metadata fields. There are 3
additional META columns (METADATA$ROW_ID, METADATA$ISUPDATE and
METADATA$ACTION) introduced and they make it very easy to detect if a
row has been updated, deleted, or inserted. And, since it’s an initial load
everything is inserted.
METADATA$ACTION — Specifies the action (INSERT or DELETE).
Note that streams record the differences between two offsets. If a row is
added and then updated in the current offset, the delta change is a new row.
The METADATA$ISUPDATE row records a FALSE value.
Now, there are no records in the destination table as they’re still in the
stream.
Step 4: Let’s move the records to the destination using the MERGE statement.
Now, the data has moved to the destination and there will be nothing in the Alexander
stream. After the MERGE command has consumed, the stream object will 179 Followers
become empty. Alexander
The cycle continues, and the stream will continue to record all the changes Follow
As you can see, the destination table is updated with the records.
Step 6: Now, it’s time to demonstrate the Snowflake Change Data Capture.
For that purpose, go ahead and make a change in the source table. Let’s
update a record and observe the stream.
UPDATE TEST_SOURCE
SET LASTNAME = 'WASHINGTON'
WHERE ID = 1;
While observing the stream, you’ll find 2 records, INSERT and DELETE. This
is because the original last_name (i.e. HARRITON) was deleted for id=1 and
the new last_name (i.e. WASHINGTON) was updated.
Use the MERGE command to update the destination table as shown below.
Step 7: Run the following command to take a look at the destination table.
Step 8: Go ahead and make a change in the source table. Let’s delete a record
and observe the stream.
Use the MERGE command to update the destination table as shown below.
Here, you will be clearly able to observe the changes made to the source
table updated in the destination table.
References:-
https://www.snowflake.com/
Subscribe
Your email
By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our
privacy practices.