配置单元数组类型的求和值

vhipe2zx  于 2021-06-03  发布在  Hadoop
关注(0)|答案(2)|浏览(318)

hive有一个非常好的数组类型,在理论上非常有用,但在实践中,我发现很少有关于如何使用它进行任何操作的信息。我们将一系列数字存储在数组类型列中,并需要在查询中求和,最好是从第n个元素到第m个元素。是否可以使用标准hiveql,或者是否需要自定义项或客户Map器/缩减器?
注意:我们在emr环境中使用的是hive0.8.1。

goqiplq2

goqiplq21#

上面的答案解释得很好。我发布了一个非常简单的udf实现。

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;

我的输入文件看起来像
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

add jar file:///home/ak/Desktop/array.jar;

然后创建临时函数,如图所示

create temporary function getSum as 'com.ak.hive.udf.test.ArraySumUDF';

执行如下示例查询,

select col1,getSum(col2,1,3) from table1;

这应该解决最基本的需要。如果这不是问题陈述,请回复,以便我可以再次帮助您。

isr3a4wc

isr3a4wc2#

我会写一个简单的 UDF 为此目的。你需要有 hive-exec 在构建路径中。
e、 g在以下情况下: Maven :

<dependency>
  <groupId>org.apache.hive</groupId>
  <artifactId>hive-exec</artifactId>
  <version>0.8.1</version>
</dependency>

简单的原始实现如下所示:

package com.myexample;

import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.IntWritable;

public class SubArraySum extends UDF {

    public IntWritable evaluate(ArrayList<Integer> list, 
      IntWritable from, IntWritable to) {
        IntWritable result = new IntWritable(-1);
        if (list == null || list.size() < 1) {
            return result;
        }

        int m = from.get();
        int n = to.get();

        //m: inclusive, n:exclusive
        List<Integer> subList = list.subList(m, n);

        int sum = 0;
        for (Integer i : subList) {
            sum += i;
        }
        result.set(sum);
        return result;
    }
}

接下来,构建一个jar并将其加载到hive shell中:

hive> add jar /home/user/jar/myjar.jar;
hive> create temporary function subarraysum as 'com.myexample.SubArraySum';

现在可以用它来计算数组的和。
例如:
假设您有一个输入文件,其中有制表符分隔的列:

1   0,1,2,3,4
2   5,6,7,8,9

将其加载到mytable:

hive> create external table mytable (
  id int,
  nums array<int>
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
COLLECTION ITEMS TERMINATED BY ','
STORED AS TEXTFILE
LOCATION '/user/hadoopuser/hive/input';

执行一些查询,然后:

hive> select * from mytable;
1   [0,1,2,3,4]
2   [5,6,7,8,9]

在m,n范围内求和,其中m=1,n=3

hive> select subarraysum(nums, 1,3) from mytable;
3
13

hive> select sum(subarraysum(nums, 1,3)) from mytable;
16

相关问题