从命名阶段将数据合并到表中

Posted

技术标签:

【中文标题】从命名阶段将数据合并到表中【英文标题】:Merge Data into Table from Named Stage 【发布时间】:2020-02-13 14:22:27 【问题描述】:

我无法从内部命名阶段将数据合并到 Snowflake 表中。我确实参考了知识库article。

My code is pasted below and the error is also pasted below.

代码:

MERGE INTO ORDERS TGT 
USING 
    (
        SELECT $1::NUMBER                             o_orderkey,
               $2::NUMBER                             o_custkey,
               $3::STRING                             o_orderstatus,
               $4::FLOAT                              o_totalprice,
               TO_DATE($5::VARCHAR, 'YYYY-MM-DD')     O_ORDERDATE,
               $6::STRING                             o_orderpriority,
               $7::STRING                             o_clerk,
               $8::STRING                             o_shippriority,
               $9::STRING                             o_comment
        from '@STG_IVP_REF_BULK_DATA_MIGRATION/ORDERS/Snowflake_DB_Data_12-Feb-2020_02-59-50-PM.csv.gz'
        (file_format = (field_delimiter = '|'))
    )SRC
ON SRC.O_ORDERKEY = TGT.O_ORDERKEY
WHEN MATCHED 
THEN
    UPDATE SET TGT.LAST_MODIFIED_DATE = CURRENT_TIMESTAMP()::TIMESTAMP_NTZ
WHEN NOT MATCHED
THEN
    INSERT (o_orderkey, o_custkey, o_orderstatus, o_totalprice, O_ORDERDATE, o_orderpriority, o_clerk, o_shippriority, o_comment)
    VALUES (o_orderkey, o_custkey, o_orderstatus, o_totalprice, O_ORDERDATE, o_orderpriority, o_clerk, o_shippriority, o_comment);

错误输出:

SQL Error [2018] [22023]: SQL compilation error:
Invalid argument [2=>[unresolved function: "="](FILE_FORMAT, [unresolved function: "="](FIELD_DELIMITER, '|'))] for table function. Table function argument is required to be a constant.

【问题讨论】:

【参考方案1】:

您能否创建一个 file_format 对象来将分隔符定义为“|”,并在您的查询中使用它?例如:

CREATE FILE FORMAT myformat TYPE = 'CSV' FIELD_DELIMITER = '|';


MERGE INTO ORDERS TGT 
USING 
    (
        SELECT $1::NUMBER                             o_orderkey,
               $2::NUMBER                             o_custkey,
               $3::STRING                             o_orderstatus,
               $4::FLOAT                              o_totalprice,
               TO_DATE($5::VARCHAR, 'YYYY-MM-DD')     O_ORDERDATE,
               $6::STRING                             o_orderpriority,
               $7::STRING                             o_clerk,
               $8::STRING                             o_shippriority,
               $9::STRING                             o_comment
        from '@STG_IVP_REF_BULK_DATA_MIGRATION/ORDERS/Snowflake_DB_Data_12-Feb-2020_02-59-50-PM.csv.gz'
        (file_format => myformat)
    )SRC
ON SRC.O_ORDERKEY = TGT.O_ORDERKEY
WHEN MATCHED 
THEN
    UPDATE SET TGT.LAST_MODIFIED_DATE = CURRENT_TIMESTAMP()::TIMESTAMP_NTZ
WHEN NOT MATCHED
THEN
    INSERT (o_orderkey, o_custkey, o_orderstatus, o_totalprice, O_ORDERDATE, o_orderpriority, o_clerk, o_shippriority, o_comment)
    VALUES (o_orderkey, o_custkey, o_orderstatus, o_totalprice, O_ORDERDATE, o_orderpriority, o_clerk, o_shippriority, o_comment);

从 staging 中查询数据时,需要使用 FILE_FORMAT 对象:

https://docs.snowflake.net/manuals/user-guide/querying-stage.html#query-syntax-and-parameters

【讨论】:

以上是关于从命名阶段将数据合并到表中的主要内容,如果未能解决你的问题,请参考以下文章

将SP的输出游标合并到表中?

使用 Python 将数据写入雪花

Snowpipe 自动摄取

我们可以使用雪花中的存储过程将文件从文件位置加载到命名内部阶段吗?

MySQL:30 阶段总结:MySQL存储模型以及数据读写机制

map join 与 reduce join