【问题标题】:How can I convert array to string in hive sql?如何在 hive sql 中将数组转换为字符串?
【发布时间】:2016-12-07 06:18:53
【问题描述】:

我想在 hive 中将数组转换为字符串。我想在没有[[""]] 的情况下收集设置数组值以转换为字符串。

select actor, collect_set(date) as grpdate from actor_table group by actor;

这样[["2016-07-01", "2016-07-02"]] 就会变成2016-07-01, 2016-07-02

【问题讨论】:

    标签: arrays string hive concatenation hiveql


    【解决方案1】:

    有时,您可能需要 JSON 格式的列表,因此您可以简单地使用:

    SELECT CAST(COLLECT_SET(date) AS STRING) AS dates FROM actor_table 
    

    PS:我需要这个,但只发现了你关于数组到字符串转换的问题。

    【讨论】:

    • 它在哪个版本的 Hive 中工作?在 1.2 和 2,3,6 中它说 cast 只能用于原始类型
    【解决方案2】:

    使用concat_ws(string delimiter, array<string>)函数连接数组:

    select actor, concat_ws(',',collect_set(date)) as grpdate from actor_table group by actor;
    

    如果日期字段不是字符串,则将其转换为字符串:

    concat_ws(',',collect_set(cast(date as string)))
    

    如果您已经有一个(int)数组并且不想将其分解以将元素类型转换为字符串,请阅读有关替代方法的答案:How to concatenate the elements of int array to string in Hive

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多