Hive数组的字符串转换为int数组

时间:2018-06-06 05:04:39

标签: hive hiveql

在Hive中,我有一个表作为字符串数据类型的列,它包含一组数字说标记。所以我想应用一些算术运算,如加法,所以我使用split命令,它返回数组。 Bt我不能对数组应用加法我认为我需要转换为数组来应用任何算术运算。我用了命令

select sum(a) from (select split(marks, ':') as a from tbl) b;

我试图将它转换为数组也不会产生一些错误。我尝试了以下命令

select sum(a) from (select cast(split(marks, ':') as a array<int>) from table) b;
select sum(a) from (select cast(b) as array<int> from (select split(marks, ':') as b from tbl) c) d;

请建议我解决此问题..以及如何将数组转换为数组

2 个答案:

答案 0 :(得分:1)

您可以使用:

select sum(cast(a as int)) from TableName;

答案 1 :(得分:1)

你可以做这样的事情,希望这有帮助

Select 
 sum(a), 
 sum(b), 
 sum(c), 
 sum(d)
From
(Select
  CAST(Split(marks,':')[0] AS bigint) AS a, 
  CAST(Split(marks,':')[1] AS bigint) AS b,
  CAST(Split(marks,':')[2] AS bigint) AS c, 
  CAST(Split(marks,':')[3] AS bigint) AS d
From
Table) split_data
Group by <some column>