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

标签 amazon-web-services amazon-redshift

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

相关文章:

amazon-web-services - 在 StepFunctions 中使用 Params 的 AWS CDK 替代方案

amazon-web-services - 亚马逊 Elasticsearch 服务 : Authorization header issue when calling ES domain via proxy

amazon-web-services - AWS Kinesis Firehose - 按纪元以外的时间戳进行动态分区

csv - 使用标题将 CSV 加载到 Redshift 中?

amazon-web-services - 使用 AWS SDK (PHP) 更改生成的 S3 链接的基本 URL

amazon-web-services - Yaml ">"将多行连接为多个字符串,而不是 AWS::ECS::TaskDefinition 中的单个字符串

python - 如何将 Select * Postgres/Redshift 查询结果放入字典(列/值)

sql - 尽管数据有效,但 Redshift 上的 COPY 总是失败并出现时间戳错误

sql - 在 Redshift 中将多行透视为列

sql - 在 Redshift 中将时间戳转换为整数