dan*_*anD 1 snowflake-cloud-data-platform
我们有一个 aws s3 存储桶,其中每天都会重写相同的文件。我们需要将该文件加载到今天才加载的雪花中。
复制命令工作正常。但是,当未加载新文件时,它只会加载不需要的前一个文件。相反,它应该抛出一个错误让团队知道。
我们可以查询last_modified日期:
ls @stage_name
Run Code Online (Sandbox Code Playgroud)
或者我们可以获得阶段元数据信息。
通过查询
select metadata@filename from @stage_name
Run Code Online (Sandbox Code Playgroud)
但我无法找到任何查询或过滤last_modified阶段列的方法。
ls 命令是元数据查询。虽然您无法对其进行过滤,但您可以对其生成的结果集进行过滤:
ls @MYSTAGE;
-- You must run this immediately after the previous query in the same session
select * from table(result_scan(last_query_id()));
Run Code Online (Sandbox Code Playgroud)
但有两个并发症。首先,元数据列的名称是小写的,因此请务必在过滤器中使用小写名称并将其括在双引号中。第二个问题是日期字段的格式不适合在查询中进行过滤。为了解决这个问题,我编写了一个UDF,将last_modified字段转换为时间戳,以便于过滤:
-- Convert the last modified value from the Snowflake LIST
-- command into a timestamp.
create or replace function LAST_MODIFIED_TO_TIMESTAMP(LAST_MODIFIED string)
returns timestamp_tz
as
$$
to_timestamp_tz(left(LAST_MODIFIED, len(LAST_MODIFIED) - 4) || ' ' || '00:00', 'DY, DD MON YYYY HH:MI:SS TZH:TZM')
$$;
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样过滤结果:
ls @TESTSTAGE;
select * from table(result_scan(last_query_id()))
where LAST_MODIFIED_TO_TIMESTAMP("last_modified") < '2020-11-01' ;
Run Code Online (Sandbox Code Playgroud)