简体   繁体   English

Hive/Athena 中的 JSON SerDe:将一个 JSON object 变成多行?

[英]JSON SerDe in Hive/Athena: turning one JSON object into multiple rows?

I am looking into using AWS Athena to do queries against a mass of JSON files.我正在研究使用 AWS Athena 对大量 JSON 文件进行查询。

My JSON files have this format (prettyprinted for convenience):我的 JSON 文件具有以下格式(为方便起见,进行了漂亮的打印):

{
 "data":[
         {<ROW1>},
         {<ROW2>},
          ...
        ],
 "foo":[...],
 "bar":[...]
}

The ROWs contained in the "data" array are what should be queried. “数据”数组中包含的 ROW 是应该查询的。 The rest of the JSON file is unimportant. JSON 文件的 rest 不重要。

Can this be done without modifying the JSON files?这可以在不修改 JSON 文件的情况下完成吗? If yes, how?如果是,如何? From what I've been able to find, looks like the SerDes (or is it Hive itself?) assume one row of output per line of input, which would mean that I'm stuck with modifying all my JSON files (and turning them into JSONL?) before uploading them to S3.从我所能找到的来看,看起来像 SerDes(或者它本身是 Hive 吗?)假设每行输入一行 output,这意味着我坚持修改我所有的 Z0ECD11C1D1D7A287A2F8DZ 文件(以及将它们转换为 output)到 JSONL?),然后再将它们上传到 S3。

(Athena uses the Hive JSON SerDe and the OpenX JSON SerDe; AFAICT, there is no option to write my own SerDe or file format...) (Athena 使用 Hive JSON SerDe 和 OpenX JSON SerDe;AFAICT,没有选项可以编写我自己的 SerDe 或文件格式...)

You can't make the serde do it automatically, but you can achieve what you're after in a query.您不能让 serde 自动执行此操作,但您可以在查询中实现您所追求的。 You can then create a view to simulate a table with the data elements unwrapped.然后,您可以创建一个视图来模拟未包装数据元素的表。

The way you do this is to use the UNNEST keyword.这样做的方法是使用UNNEST关键字。 This produces one new row per element in an array:这将为数组中的每个元素生成一个新行:

SELECT
  foo,
  bar,
  element
FROM my_table, UNNEST(data) AS t(element)

If your JSON looked like this:如果您的 JSON 看起来像这样:

{"foo": "f1", "bar": "b1", "data": [1, 2, 3]}
{"foo": "f2", "bar": "b2", "data": [4, 5]}

The result of the query would look like this:查询的结果如下所示:

foo | bar | element
----+-----+--------
f1  | b1  | 1
f1  | b1  | 2
f1  | b1  | 3
f2  | b2  | 4
f2  | b2  | 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM