在 Snowflake 中手动刷新 table 流中的数据

Manually flush data from table streams in Snowflake

我正在为我的 ETL 管道使用 table 流。我只是想知道是否有可能手动刷新 table 流中的数据而不将其保存在其他 table 中的任何地方?

当 Snowflake 在 DML 语句中看到 FROM 之后的流时,它会推进流。它不关心你如何或是否使用这些行,所以你可以这样做:

insert into JUNK_TABLE select ANY_COLUMN from MY_STREAM where false;

如果您 运行 只插入这一部分,您会发现不会插入任何内容:

select ANY_COLUMN from MY_STREAM where false;

where 子句对每一行的计算结果都是假的,因为它被设置为 return。这意味着 insert 语句不会插入一行,但会消耗流。

这是一个测试这个的小脚本:

-- Quick setup:
create or replace table MY_TABLE(COL1 varchar);
create or replace stream MY_STREAM on table MY_TABLE;

--Create a junk table so the syntax works:
create table JUNK_TABLE like MY_TABLE;

insert into MY_TABLE(COL1) values ('Row1'), ('Row2'), ('Row3');

select * from MY_STREAM; --The stream has three rows

insert into JUNK_TABLE select COL1 from MY_STREAM where false;  --Consume the stream

select * from MY_STREAM; -- The stream has no rows

select * from JUNK_TABLE; -- Neither does the junk table because "where false" on the insert

您是否考虑过使用 CREATE OR REPLACE STREAM ... 重新创建流? 如果您必须定期执行此操作,您还可以将其作为计划任务中 运行 的存储过程的一部分。