将数据从阶段发送到雪花中的多列 table

send data from stage to multi column table in snowflake

我有一个存储 json 文件的内部命名阶段,我想从那里将​​它们存储在雪花 table 中。 destinationtable的结构如下,

file_name (string)
load_date (timestamp)
data      (variant)

我正在使用以下查询将数据从阶段移动到 table

copy into tableName (data) from @stagename/filename.json;

但是上面的查询只是填充数据列,我想要的是也插入时间戳和文件名。知道我需要在查询中进行哪些更改吗?谢谢

您需要使用带有转换的 COPY 语句 - 文档 here. When you use that method you can query the metadata of the files to get the filename, row number etc - documentation for that here

示例文件 filename.json 已上传到名为 stagename 的内部阶段:

[{"name": "simon"},{"name": "jason"}, {"name": "jessica"}]

Sql 加载创建并加载 table:

-- Create example table first with 3 columns
create or replace transient table test_table
(
    file_name varchar,
    load_date timestamp,
    data      variant
);


-- Load with transformation: 
copy into test_table (file_name, load_date, data) from (
    select
        metadata$filename,
        current_timestamp,
        f.
    from @stagename/filename.json f
)
    file_format = (
        type = json
            strip_outer_array = true
        )
    force=true
;

结果:

+-------------+-----------------------------+-----------------------+
|FILE_NAME    |LOAD_DATE                    |DATA                   |
+-------------+-----------------------------+-----------------------+
|filename.json|2021-07-16 08:56:24.075000000|{"name": "simon"}      |
|filename.json|2021-07-16 08:56:24.075000000|{"name": "jason"}      |
|filename.json|2021-07-16 08:56:24.075000000|{"name": "jessica"}    |
+-------------+-----------------------------+-----------------------+