本文主要是介绍Doris学习笔记-Java自定义UDAF,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目最近需要做一些数据统计方面的东西,发现数据字段都是很长一串数字的字符串,Doris自带的函数无法对其进行相应的运算操作,需要扩展实现相关的操作运算。
主要参考官方的文档资料完成相关的自定义扩展。需要注意的是在使用Java代码编写UDAF时,有一些必须实现的函数(标记required)和一个内部类State。
- SUM求和运算函数
public class Sum {//Need an inner class to store data/*required*/public static class State {/*some variables if you need */public BigDecimal sum;}/*required*/public State create() {/* here could do some init work if needed */State state = new State();state.sum = new BigDecimal(0);return state;}/*required*/public void destroy(State state) {/* here could do some destroy work if needed */}/*Not Required*/public void reset(State state) {/*if you want this udaf function can work with window function.*//*Must impl this, it will be reset to init state after calculate every window frame*//**state.sum = new BigDecimal(0);**/}/*required*///first argument is State, then other types your inputpublic void add(State state, String value) throws Exception {try {/* here doing update work when input data*/if (null != value && !"".equals(value)) {state.sum = state.sum.add(new BigDecimal(value));}} catch (Exception e) {log.info(e.getMessage());}}/*required*/public void serialize(State state, DataOutputStream out) {/* serialize some data into buffer */try {out.writeUTF(state.sum.toString());} catch (Exception e) {/* Do not throw exceptions */log.info(e.getMessage());}}/*required*/public void deserialize(State state, DataInputStream in) {/* deserialize get data from buffer before you put */String value = "0";try {value = in.readUTF();} catch (Exception e) {/* Do not throw exceptions */log.info(e.getMessage());}state.sum = new BigDecimal(value);}/*required*/
这篇关于Doris学习笔记-Java自定义UDAF的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!