package com.ak.hive.udf.test;
import java.util.ArrayList;
import org.apache.hadoop.hive.ql.exec.UDF;
public final class ArraySumUDF extends UDF {
public int evaluate(ArrayList<Integer>arrayOfIntegers,int startIndex,int endIndex) {
// add code to handle all index problem
int sum=0;
int count=startIndex-1;
for(;count<endIndex;count++){
sum+=arrayOfIntegers.get(count);
}
return sum;
}
}
同时发布表创建和其他查询。
create table table1 (col1 int,col2 array<int>)ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' COLLECTION ITEMS TERMINATED BY '~' STORED AS TEXTFILE;
load data local inpath '/home/ak/Desktop/hivedata' into table table1;
2条答案
按热度按时间goqiplq21#
上面的答案解释得很好。我发布了一个非常简单的udf实现。
同时发布表创建和其他查询。
我的输入文件看起来像
1,3~5~8~5~7~9
2,93~5~8~5~7~29
3,3~95~8~5~27~9
4,3~5~58~15~7~9
5,3~25~8~55~7~49
6,3~25~8~15~7~19
7,3~55~78~5~7~9
我已经为我的udf创建了一个jar,我使用以下命令将jar添加到hive
然后创建临时函数,如图所示
执行如下示例查询,
这应该解决最基本的需要。如果这不是问题陈述,请回复,以便我可以再次帮助您。
isr3a4wc2#
我会写一个简单的
UDF
为此目的。你需要有hive-exec
在构建路径中。e、 g在以下情况下:
Maven
:简单的原始实现如下所示:
接下来,构建一个jar并将其加载到hive shell中:
现在可以用它来计算数组的和。
例如:
假设您有一个输入文件,其中有制表符分隔的列:
将其加载到mytable:
执行一些查询,然后:
在m,n范围内求和,其中m=1,n=3
或