从一个 CSV 文件(带有一个标题和一个管道分隔符)我得到了以下内容,其中包含一个 JSON 列(里面有一个集合),如下所示:
ProductId|IngestTime|ProductOrders
9180|20171025145034|[{"OrderId":"299","Location":"NY"},{"OrderId":"499","Location":"LA"}]
8251|20171026114034|[{"OrderId":"1799","Location":"London"}]
我需要创建一个返回的 SELECT Hive 查询:
ProductId IngestTime OrderId OrderLocation
9180 20171025145034 299 NY
9180 20171025145034 499 LA
8251 20171026114034 1799 London
到目前为止,我尝试了很多组合,使用 'explode'、'get_json_object' 等,但我仍然没有找到正确的 SQL 查询。
你有解决办法吗?
非常感谢您的帮助:-)
最佳答案
我有类似的要求。来自 this 的解决方案链接帮我解决了。 顺便说一句,假设您的 DB_TABLE 中的所有列都是“字符串”类型,下面是对您的要求的查询。
SELECT ProductId,
IngestTime,
split(split(results,",")[0],':')[1] AS OrderId,
regexp_replace(split(split(results,",")[1],':')[1], "[\\]|}]", "") AS OrderLocation
FROM
(SELECT ProductId,
IngestTime,
split(translate(ProductOrders, '"\\[|]|\""',''), "},") AS r
FROM DB_TABLE) t1 LATERAL VIEW explode(r) rr AS results
关于json - 配置单元 : How to explode a JSON column with an array, 并嵌入到 CSV 文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46942426/