【详述】需要一个函数,对字段进行分组、去重然后合并。目前只有group_cancat函数,但不能去重
【StarRocks版本】2.5.1
【集群规模】例如:3fe(3 follower)+5be
详细描述:
官网文档里只对于udaf的样例,只有整型的,没有字符串类型的。存在两个问题:1、值的长度不一致的时候如何处理,因为serializeLength 需要指定长度;2、中文出现乱码。
代码如下:
import java.nio.charset.StandardCharsets;
public class GroupConcatDistinct {
public static class State {
String res = "";
public int serializeLength() {
return 1;
}
}
public State create() {
return new State();
}
public void destroy(State state) {
}
public final void update(State state, String val) {
if (null != val && !"".equals(val) && !state.res.contains(val)) {
if ("".equals(state.res))
state.res = val;
else
state.res = state.res + "," + val;
}
}
public void serialize(State state, java.nio.ByteBuffer buff) {
buff.put(state.res.getBytes(StandardCharsets.UTF_8));
}
public void merge(State state, java.nio.ByteBuffer buffer) {
String val = StandardCharsets.UTF_8.decode(buffer).toString();
if (null != val && !"".equals(val) && !state.res.contains(val)) {
if ("".equals(state.res))
state.res = val;
else
state.res = state.res + "," + val;
}
}
public String finalize(State state) {
return state.res;
}
}