how to extract data JSON from zeppelin sql

妖精的绣舞 提交于 2021-01-29 19:26:19

问题


I query to test_tbl table on Zeppelin. the table data structure looks like as below :

%sql
desc stg.test_tbl
col_name | data_type | comment
id       |  string   |
title    |  string   |
tags     |  string   |

The tags column has data JSON type following as :

{"name":[{"family": null, "first": "nelson"}, {"pos_code":{"house":"tlv", "id":"A12YR"}}]}

and I want to see the JSON data with columns, so my query is :

select *, tag.*
from stg.test_tbl as t
lateral view explode(t.tags.name) name as name
lateral view explode(name.pos_code) pos_code as pos_code

but when I query, it returns

Can't extract value from tags#3423: need struct type but got string; line 3 pos 21
set zeppelin.spark.sql.stacktrace = true to see full stacktrace

should i query as string in where statement?


回答1:


You can use get_json_object in string type of JSON. Also, if the JSON is an array type as

{"name":[{"family": null, "first": "nelson"}, {"pos_code":{"house":"tlv", "id":"A12YR"}}]}

, you can query by key like

select * from stg.test_tbl as t
where t.pos_code[0].house = "tlv"


来源:https://stackoverflow.com/questions/59032103/how-to-extract-data-json-from-zeppelin-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!