21.streams in Snowflake
21.streams in Snowflake
21.streams in Snowflake
by
Janardhan
Bandi
Agenda
• What is a stream?
• Metadata of streams (Stream Columns)
• How a stream works?
• Consuming data from streams
• Types of Streams
What is a Stream?
• A stream object records DML changes made to tables including inserts, updates, and
deletes.
• Also Stream stores metadata about each change, so that actions can be taken using this
metadata.
• We call this process as change data capture (CDC).
• Streams tracks all row level changes to a source table using offset but doesn't store the
changed data.
• Once these changes are consumed by the Target table, this offset moves to the next
point.
• Streams can be combined with tasks to set continuous data pipelines.
• Snowpipe + Stream + Task Continuous Data Load.
Metadata of Streams
Along with the changes made to the source table, Streams maintain 3 metadata fields.
1. METADATA$ACTION: Indicates the DML operation (INSERT, DELETE) recorded.
2. METADATA$ISUPDATE: Indicates whether the operation was part of an UPDATE
statement. Updates to rows in the source object are represented as a pair of DELETE and
INSERT records in the stream with a metadata column METADATA$ISUPDATE values set
to TRUE.
3. METADATA$ROW_ID: Specifies the unique and immutable ID for the row, which can be
used to track changes to specific rows over time.
How a Stream works? or Data flow in a Stream
Consuming data from Streams
We use merge statement for consuming the changes from stream and applying the same
on Target tables.
// To identify Insert records
WHERE metadata$action = 'INSERT AND metadata$isupdate = 'FALSE’;
Note: If we want to consume these changes to multiple tables then we have to create multiple
streams.
Types of Streams
1. Standard Streams: A Standard stream tracks all DML changes to the source object,
including inserts, updates, and deletes (including table truncates).
CREATE OR REPLACE STREAM my_stream ON TABLE my_table;
2. Append-only Streams: An Append-only stream tracks row inserts only. Update and
delete operations (including table truncates) are not recorded.
CREATE OR REPLACE STREAM my_stream ON TABLE my_table
APPEND_ONLY = TRUE;
3. Insert-only Streams: Supported for External tables only. An insert-only stream tracks
row inserts only. They do not record delete operations.
CREATE OR REPLACE STREAM my_stream ON EXTERNAL TABLE my_table
INSERT_ONLY = TRUE;
Thank You