Hive:解析 JSON [英] Hive: parsing JSON

查看:21
本文介绍了Hive:解析 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数百万行(5 TB+ 表)的嵌套 JSON 中获取一些值.最有效的方法是什么?

I am trying to get some values out of nested JSON for millions of rows (5 TB+ table). What is the most efficient way to do this?

这是一个例子:

{"country":"US","page":227,"data":{"ad":{"impressions":{"s":10,"o":10}}}}

我需要上述 JSON 中的这些值:

I need these values out of the above JSON:

Country        Page      impressions_s       impressions_o
---------      -----     -------------       --------------
US              2        10                  10

这是 Hive 的 json_tuple 函数,我不确定这是不是最好的函数.https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-getjsonobject

This is Hive's json_tuple function, I am not sure if this is the best function. https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-getjsonobject

推荐答案

你可以使用 get_json_object:

You can use get_json_object:

 select get_json_object(fieldname, '$.country'), 
        get_json_object(fieldname, '$.data.ad.s') from ... 

使用 json_tuple 可以获得更好的性能,但我找到了如何"在 json 中获取 json 中的值;要格式化您的表格,您可以使用以下内容:

You will get better performance with json_tuple but I found a "how to" to get the values in json inside json; To formating your table you can use something like this:

从表 t 侧视图爆炸(拆分(regexp_replace(get_json_object(ln,''$.data.ad.s'),'\[|\]',''),','))tb1 as s上面的代码会将您转换为列中的数组".

from table t lateral view explode( split(regexp_replace(get_json_object(ln, ''$.data.ad.s'), '\[|\]', ''), ',' ) ) tb1 as s this code above will transform you "Array" in a column.

表单更多:https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF

我希望这有助于...

这篇关于Hive:解析 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆