AWS Redshift Spectrum外部表-分配给列的Varchar数据类型无法处理同一列的数组和字符串数据

问题描述

任务:尝试使用Redshift Spectrum从s3存储桶中将一堆JSON文件加载到Redshift。 问题:少数文件中的JSON对象的数据用方括号括起来,但是其他JSON文件的对象相同,但没有方括号。使用Redshift频谱表创建外部表时,有没有办法使用带/不带方括号“ []”的数据?

要使用的JSON文件

文件1:"x":{"y":{ "z":["ABCD"]}}

文件2:"x":{"y":{ "z":"EFGH"}}

案例1

当z列定义为数组时,我从JSON文件中丢失了“没有方括号”的数据

CREATE EXTERNAL TABLE spectrum.table
(x struct<y:struct<z:array<varchar(256)>>>)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES ( 'dots.in.keys'='true') STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeytextoutputFormat'
location 's3://****'

查询Select c from spectrum.table t,t.x.y.z c;

案例2

当将z列定义为varchar(不声明为数组)时,出现以下错误

创建语句:

CREATE EXTERNAL TABLE spectrum.table
(x struct<y:struct<z:varchar(256)>>)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES ( 'dots.in.keys'='true') STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeytextoutputFormat'
location 's3://****'

查询Select regexp_replace( t.x.y.z,'\\([\\"])','' ) from spectrum.table t;Select t.x.y.z from spectrum.table t;

 [XX000][500310] [Amazon](500310) Invalid operation: Spectrum Scan Error
 Details:
-----------------------------------------------
 error:  Spectrum Scan Error
 code:      15001
 context:   Unsupported implicit cast: Column ('x' 'y' 'z'),From Type: LIST,To Type: VARCHAR,----------------------------------------------

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)