【问题标题】:hive sql transform big int array to string arrayhive sql将大int数组转换为字符串数组
【发布时间】:2018-02-26 14:18:17
【问题描述】:

我正在尝试将一个大整数数组转换为 hive SQL 中的字符串数组

我尝试过使用

concat_ws

但这给了我一个错误

Argument 2 of function CONCAT_WS must be "string or array<string>", but "array<bigint>" was found.

我尝试过使用

transform(mycolumn) using '/bin/cat' as mycolumn_as_string

它给出了一个错误

 cannot recognize input near 'transform' '(' 'mycolumn' in selection target

如何将这个 bigint 数组转换为数组字符串?

【问题讨论】:

  • 你在哪里使用concat_ws这里?
  • 我之前尝试过使用它,但你不能将 concat_ws 与 bigint 一起使用,它会报错
  • 请向我们展示 1) 表架构和 2) 出错的配置单元查询。

标签: sql hive


【解决方案1】:

假设您的表格如下所示:

tb_name mycolumn
------- --------
tblname [111,222,333]
tblname [1234,123456,3423]

那么下面的查询将不起作用,因为 concat_ws 只接受字符串或字符串数​​组:

select tb_name, concat_ws('-', mycolumn) from mytable;
FAILED: Argument 2 of function CONCAT_WS must be "string or array<string>", but "array<int>" was found.

基于此 SO:How to concatenate the elemets of int array to string in Hive

不正确:

select concat_ws('-', transform(mycolumn) using '/bin/cat') as mycolumnstr from mytable;
FAILED: ParseException line 1:22 cannot recognize input near 'transform' '(' 'mycolumn' in select expression

正确:

with tbl as (select transform(mycolumn, tb_name) using '/bin/cat' as (mycolumnstr, tb_name) from mytable) 
select concat_ws('-', tb_name, mycolumnstr) from tbl;
Result:
tblname-[111,222,333]
tblname-[1234,123456,3423]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 2019-11-26
    • 1970-01-01
    • 2021-12-21
    • 2016-08-14
    • 1970-01-01
    相关资源
    最近更新 更多