【问题标题】:Snowflake - copy column headers for empty row雪花 - 复制空行的列标题
【发布时间】:2020-04-03 19:57:02
【问题描述】:

我正在使用copy into 命令将雪花中的查询结果存储到 s3 存储桶中的文件中。它按预期工作,但是当查询没有返回数据时,我仍然想将列名保存在同一个文件中,以便我可以在 UI 中显示带有列名的空行。我在命令中指定了header=true,但是当查询没有返回结果时它不起作用。

谢谢

【问题讨论】:

    标签: amazon-s3 snowflake-cloud-data-platform snowflake-task


    【解决方案1】:

    根据docs:

    如果源表包含 0 行,则 COPY 操作不会 卸载一个空的数据文件;甚至没有包含表格列的文件 标题。

    而且似乎没有办法改变这种行为。

    但是,在将表写入 S3 之前,您可以通过查询 INFORMATION_SCHEMA 来写入标题(作为数据)。然后像往常一样写你的表(使用 OVERWRITE=TRUE)——如果没有写行,头文件将保留。


    更新

    这是一个如何生成标题:

    select listagg(column_name,',') within group (order by ordinal_position)
    from MY_DB.information_schema.columns 
    where table_schema='MY_SCHEMA' and table_name='MY_TABLE';
    

    然后您可以将其作为数据写入 CSV 文件,如下所示:

    copy into @my_stage/my_table  (
        select listagg(column_name,',') 
            within group (order by ordinal_position)
        from MY_DB.information_schema.columns 
        where table_schema='MY_SCHEMA' and table_name='MY_TABLE';
    )
    file_format = (type=csv compression=none field_delimiter=none)
    header=false;
    

    【讨论】:

    • 在信息模式 dbname.information_schema.columns 中获取表头的表名是什么?
    • 你可以这样构造头部:select listagg(column_name,',') within group (order by ordinal_position) header from MY_DB.information_schema.columns where table_schema='MY_SCHEMA' and table_name='MY_TABLE' ;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多