I ran into a similar problem awhile back. I didn't want to have to write a full-on UDAF
so I just did a combo with brickhouse collect and my own UDF
. Say you have this data
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
my UDF
was
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;
}
}
and then my query was
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
output
1 ["A","B","A","C","D"]
2 ["D","F","A","W","A"]
As an aside, the built-in collect_list
will not necessary keep the elements of the list in the order they were grouped in; brickhouse collect
will. hope this helps.