无重复项的hadoop/hive collect\u列表

ezykj2lf  于 2021-05-29  发布在  Hadoop
关注(0)|答案(2)|浏览(389)

基于post、hive0.12-collect\u列表,我试图找到实现udaf的java代码,该udaf将完成这个或类似的功能,但没有重复的序列。
例如, collect_all() 返回序列 A, A, A, B, B, A, C, C 我想要序列 A, B, A, C 返回。按顺序重复的项目将被删除。
有没有人知道hive0.12中有一个函数可以完成或者已经编写了自己的udaf?
一如既往,谢谢你的帮助。

fquxozlt

fquxozlt1#

不久前我也遇到了类似的问题。我不想写一篇完整的文章 UDAF 所以我就和brickhouse collect和我自己的 UDF . 假设你有这些数据

id  value
1   A
1   A
1   A
1   B
1   B
1   A
1   C
1   C
1   D
2   D
2   D
2   D
2   D
2   F
2   F
2   F
2   A
2   W
2   A

我的 UDF

package com.something;

import java.util.ArrayList;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

public class RemoveSequentialDuplicates extends UDF {
    public ArrayList<Text> evaluate(ArrayList<Text> arr) {
        ArrayList<Text> newList = new ArrayList<Text>();
        newList.add(arr.get(0));
        for (int i=1; i<arr.size(); i++) {

            String front = arr.get(i).toString();
            String back = arr.get(i-1).toString();

            if (!back.equals(front)) {
                newList.add(arr.get(i));
            }
        }
        return newList;
    }
}

我的问题是

add jar /path/to/jar/brickhouse-0.7.1.jar;
add jar /path/to/other/jar/duplicates.jar;

create temporary function remove_seq_dups as 'com.something.RemoveSequentialDuplicates';
create temporary function collect as 'brickhouse.udf.collect.CollectUDAF';

select id
  , remove_seq_dups(value_array) no_dups
from (
  select id
    , collect(value) value_array
  from db.table
  group by id ) x

输出

1   ["A","B","A","C","D"]
2   ["D","F","A","W","A"]

另外,内置的 collect_list 将不必保持列表中的元素按其分组顺序排列;砖房 collect 威尔。希望这有帮助。

crcmnpdw

crcmnpdw2#

如果你有这样的事

index  value
1       A
2       A
3       A
4       B
5       B
6       A
7       c
8       c

其中索引是一些排序值,例如直接索引或类似日期的内容。我想在你的情况下秩序很重要。
然后查询:

select collect_all(value)
from
  (select index, value 
   from table) a
   left outer join
  (select index, 
     last_value(value) over (order by index row between current row and 1 following) as nextvalue 
   from table) b
  on a.index=b.index
  where value <> nextvalue
;

这里的问题是,因为没有下一个值,所以无法得到c的最后一个值,因此add或nextvalue为null,您应该得到结果。

select collect_all(value)
from
  (select index, value 
   from table) a
   left outer join
  (select index, 
     last_value(value) over (order by index row between current row and 1 following) as nextvalue 
   from table) b
  on a.index=b.index
  where (value <> nextvalue) or (nextvalue is null)
;

这将产生[“a”,“b”,“a”,“c”]

相关问题