【问题标题】:Hive nested get_json_object works fine in select query but returns null in create table as select query - weird behaviorHive 嵌套的 get_json_object 在选择查询中工作正常,但在创建表中返回 null 作为选择查询 - 奇怪的行为
【发布时间】:2019-02-25 18:48:28
【问题描述】:

我有一个 hive 表,它有 3 列,其中组合 1 列是一个字符串,第二个是字符串化的 json 数组,第三个是字符串化的 json 对象。我想从字符串化的 json 对象中检索一个字段,其相关键可以通过将第一列与第二个数组中的字符串化 json 数组中的第一个元素组合来获得。

get_json_object(
get_json_object(
column3,concat(
"$.",column1,"__",
get_json_object(
column2,"$[0]"
))),
"$.fieldofinterest") 
as field_of_interest

我编写了上述构造,以检索感兴趣的字段。

当这是作为select...from 语句的一部分编写时,我在field_of_interest 列中得到正确的输出。

当它作为create table t1 as select...from 语句的一部分编写时,将创建表,其中所有行的field_of_interestNULL

create...select 语句没有失败。所有其他列都很好地填充。我在其他列中使用get_json_object,它们没有嵌套。他们很好。只有这一列没有。

这可能是什么原因造成的?我该如何开始调试呢?其他堆栈溢出答案没有运气。

【问题讨论】:

    标签: sql hive


    【解决方案1】:

    想通了。这是因为我测试查询的位置和最终运行它的位置之间存在版本差异。

    我在 hiveserver2 上测试它。它在 spark 2.2 后端运行,get_json_object 能够解析 json 数组。在我运行它的服务器上,hive 0.13 版本正在运行,它不解析 JSON 数组。我将数组包裹在一个对象中,它就像一个魅力。

    get_json_object(
     get_json_object(
      column3, 
       concat(
        "$.",
        column1,
        "__",
        get_json_object(
         concat('{"x":',column2,'}'),
         "$.x[0]"
        )
       )
      ),
     "$.fieldofinterest"
    ) as field_of_interest
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-23
      • 2012-08-01
      • 1970-01-01
      • 2017-02-18
      • 2014-11-26
      • 1970-01-01
      • 2016-02-19
      • 1970-01-01
      相关资源
      最近更新 更多