我的 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/