【问题标题】:how to extract data JSON from zeppelin sql如何从 zeppelin sql 中提取数据 JSON
【发布时间】:2019-12-23 02:10:43
【问题描述】:

我在 Zeppelin 上查询 test_tbl 表。 表数据结构如下:

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

标签列的数据 JSON 类型如下:

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

我想查看带有列的 JSON 数据,所以我的查询是:

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

但是当我查询时,它会返回

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

我应该在where语句中以字符串形式查询吗?

【问题讨论】:

  • 数据类型是字符串,不是结构。 json数据被视为字符串,所以不能使用struct。
  • @Lamanus 你说得对,我不知道列类型是字符串,而不是数组作为 json 类型。因此,我使用 get_json_object cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF 查询了 String 类型的数据

标签: apache-spark apache-zeppelin


【解决方案1】:

您可以在 JSON 的字符串类型中使用 get_json_object。 此外,如果 JSON 是数组类型,则为

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

,可以像

这样按键查询
select * from stg.test_tbl as t
where t.pos_code[0].house = "tlv"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-21
    • 2019-07-27
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多