gpt4 book ai didi

amazon-web-services - Redshift DEFAULT GETDATE() 处理 INSERT 但不处理 COPY

转载 作者:行者123 更新时间:2023-12-03 23:22:06 25 4
gpt4 key购买 nike

我的 Redshift 表中有一个带有默认约束的列,以便为其填充当前时间戳。

CREATE TABLE test_table(
...
etl_date_time timestamp DEFAULT GETDATE(),
...
);

这在 INSERTS 上按预期工作,但从 S3 复制没有该列键的 json 文件时,我仍然得到空值
COPY test_table FROM 's3://bucket/test_file.json' 
CREDENTIALS '...' FORMAT AS JSON 'auto';

// There shouldn't be any NULLs here, but there are
select count(*) from test_table where etl_date_time is null;

我还尝试在源 JSON 中为键放置一个空值,但这也会导致表中出现 NULL 值。
{
...
"etl_date_time": null,
...
}

最佳答案

如果字段总是 NULL ,请考虑完全从 S3 的文件中省略它。 COPY让我们指定要复制的列,并用它们的 DEFAULT 填充缺失的列。值。

所以对于文件 data.json :

{"col1":"r1_val1", "col3":"r1_val2"}
{"col1":"r2_val1", "col3":"r2_val2"}

和表定义:
create table _test (
col1 varchar(20)
, col2 timestamp default getdate()
, col3 varchar(20)
);

特定列名
COPY具有显式列名的命令
copy _test(col1,col3) from 's3://bucket/data.json' format as json 'auto'

将产生以下结果:
db=# select * from _test;
col1 | col2 | col3
---------+---------------------+---------
r1_val1 | 2016-07-27 18:27:08 | r1_val2
r2_val1 | 2016-07-27 18:27:08 | r2_val2
(2 rows)

省略列名

如果省略列名,
copy _test from 's3://bucket/data.json' format as json 'auto'

永远不会使用 DEFAULT但插入 NULL反而:
db=# select * from _test;
col1 | col2 | col3
---------+---------------------+---------
r1_val1 | | r1_val2
r2_val1 | | r2_val2
(2 rows)

关于amazon-web-services - Redshift DEFAULT GETDATE() 处理 INSERT 但不处理 COPY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38619753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com