【问题标题】:How to convert array<string> to string in hive如何将数组<string>转换为hive中的字符串
【发布时间】:2020-04-17 13:45:23
【问题描述】:

我想将 array&lt;string&gt; 转换为 hive 中的字符串。数组数据如下:

+-------------------------------------+--+
| NULL                                |
| ["Extension","Terms & Conditions"]  |
| ["Value (generic or item level)"]   |
+-------------------------------------+--+

我想收集数组值以在没有[""] 的情况下转换为字符串,这样我就可以得到如下结果:

+-------------------------------------+--+
| NULL                                |
| Extension,Terms & Conditions        |
| Value (generic or item level)       |
+-------------------------------------+--+

以下查询:select concat_ws(',', col_name) as col_name from table_stg; 提供了结果,但将 NULL 返回为空。我尝试了几个参考,例如:

How can I convert array to string in hive sql?

Hive - How to cast array to string?

但没有得到想要的结果。有什么方法可以得到想要的结果吗?

【问题讨论】:

  • 使用case 表达式。 case when size(col_name) = 0 then null else concat_ws(',',col_name) end
  • 嗨@VamsiPrabhala - 检查大小不起作用,但它给了我解决这个问题的想法。感谢您的评论:)

标签: arrays string hive hiveql


【解决方案1】:

参考 Vamsi 评论,我设法得到了这个,并想为社区参考回答。

select case when col_name is NULL then NULL 
    else concat_ws(',',col_name) end from table_name;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    • 2010-11-05
    • 2018-04-28
    • 1970-01-01
    相关资源
    最近更新 更多