雪花:Object_construct 当我使用复制命令将 json 文件作为输出帧时留下空值

Snowflake : Object_construct leaving null values when i used copy command to frame json file as out put

我使用 snowflake 的复制命令 returns 一个内容为 json

的文件
 copy into @elasticsearch/product/sf_index 
 from (select object_construct('id',id, alpha,'alpha')from table limit 1)  
 file_format = (type = json, COMPRESSION=NONE), overwrite=TRUE, single = TRUE, max_file_size=5368709120;

数据是 阿尔法 1 空

输出文件是

{
   "id" :1
}

但我需要空值

{
   "id" :  1,
   "alpha" : null
}

您是否可以通过编程方式检查该值是否为 null 并且它是否为 null 使用下面的

select object_construct('id',1,'alpha',parse_json('null'));

根据 SnowFlake 文档

如果键或值为 NULL(即 SQL NULL),key-value 对将从结果对象中省略。由 not-null 字符串作为键和 JSON NULL 作为值(即 PARSE_JSON('NULL'))组成的 key-value 对不会被省略。

另一种选择是,在 Elastic 中不带 null 属性发送它,然后负责从 Elastic 中检索。

这个怎么样

select object_construct('id',id, 'alpha', case when alpha is not null  then alpha  else 'null' end )from table limit 1;

复制命令应该支持大小写

“null”在 json 文档中根据此 SO

有效

Is null valid JSON (4 bytes, nothing else)

好的,另一种可能的方法是使用 union

select object_construct('id',id, 'alpha', parse_json('NULL') )from table  where  alpha is null
union 
select object_construct('id',id, 'alpha', alpha )from table  where  alpha is not null;
select object_construct('id', id,'alpha', IFNULL(alpha, PARSE_JSON('null'))) from table limit 1

使用 IFNULL 检查值是否为 null 并替换为 JSON 'null'

您可以使用函数OBJECT_CONSTRUCT_KEEP_NULL

文档:https://docs.snowflake.com/en/sql-reference/functions/object_construct_keep_null.html

示例:

select OBJECT_CONSTRUCT_KEEP_NULL('id',id, alpha,'alpha')