【问题标题】:Hive Struct to String conversionHive 结构到字符串的转换
【发布时间】:2017-03-16 16:55:35
【问题描述】:

我有一个表,其中有一个结构 - 假设:

create external table table1 ( 
    a int,
    b STRUCT <c:double,d:double>,
    e string
)

我在这张桌子上执行选择并得到类似 -

1100 {"c":12.3,"d":45.6} 字符串

但是当我将此数据插入另一个表时 -

create external table table2 (
    a string,
    b string,
    c string
)

insert overwrite table table2
select a,b,c
from table1;

我得到以下奇怪的行为,表明 hive 中结构和字符串之间的转换没有按预期工作

select * from table2;

结果-

1100 12.345.6 长度

结果是结构中值的一种奇怪的串联,在处理更复杂的结构时会发生更奇怪的事情

  1. 有没有办法阻止这种自动转换?让 hive 在这种情况下抛出错误?

  2. 有没有一种干净的方法可以改变这种自动转换以不同的方式工作?

【问题讨论】:

    标签: hadoop struct hive hiveql


    【解决方案1】:
    1. 当直接调用insert overwrite table table2 select a,b,c from table1; 时,我们无法阻止自动转换。后面发生的只是concat 来自struct 的所有值。

    2. 您可以编写通用 UDF 来使用 struct 参考:http://www.dataiku.com/blog/2013/05/01/a-complete-guide-to-writing-hive-udf.html

    更快的方法:

    如果您的意图是从 struct 中获取值并将其存储为原始值,请尝试如下所示,

    create external table table2 (
        a string,
        b_c string,
        b_d string,
        c string
    )
    
    insert overwrite table table2
    select a,b.c,b.e,c
    from table1;
    

    如果这有帮助,请告诉我。

    【讨论】:

    • 非常感谢您的回答!有没有办法让你建议在 2 中编写的 UDF 在这种情况下自动运行?
    • 没有..据我所知:)
    猜你喜欢
    • 2014-05-26
    • 2018-04-28
    • 2020-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多