如何处理 JSON 与 AWS ATHENA 中列名中的特殊字符

How to deal with JSON with special characters in Column Names in AWS ATHENA

我是 athena 的新手,尽管我对 Hive 的经验不多。

我正在尝试从 JSON 文件创建一个 table,这些文件是从 MongoDB 导出的。我的问题是 MongoDB 使用 $oid、$numberInt、$numberDoble 和其他作为内部引用,但 Athena 的列名称中不接受 '$'。

这是我创建的用于测试的单行 JSON 文件:

{"_id":{"$oid":"61f87ebdf655d153709c9e19"}}

这是引用它的 table:

CREATE EXTERNAL TABLE landing.json_table (
`_id` struct<`$oid`:string>
)
ROW FORMAT SERDE 'org.apache.hive.hcatalog.data.JsonSerDe'
LOCATION 's3://bucket-name/test/';

当我运行一个简单的SELECT*它returns这个错误:

HIVE_METASTORE_ERROR: Error: name expected at the position 7 of 'struct<$oid:string>' but '$' is found. (Service: null; Status Code: 0; Error Code: null; Request ID: null; Proxy: null)

这与 JSON 列包含 $.

这一事实有关

知道如何处理这种情况吗?我现在唯一的解决办法是创建一个脚本,从不接受的字符中“清除”json 文件,但如果可能的话,我真的更愿意直接在 Athena 中处理它

如果切换到 OpenX SerDe,您可以为 JSON 字段创建一个 SerDe 映射,名称中包含特殊字符,例如 $

请参阅 AWS 博客条目 Create Tables in Amazon Athena from Nested JSON and Mappings Using JSONSerDe,“演练:使用映射处理禁止字符”部分。

适用于您的示例的映射:

CREATE EXTERNAL TABLE landing.json_table (
`_id` struct<`oid`:string>
)
ROW FORMAT SERDE 'org.openx.data.jsonserde.JsonSerDe'
WITH SERDEPROPERTIES (
"mapping.oid"="$oid"
)
LOCATION 's3://bucket-name/test/';