flink通过ProcessFunction和定时器onTimer实现一个窗口累加的功能



import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

/**
 * @program: flink-demo
 * @description: 通过onTimer实现一个累加功能,一分钟一个窗口累计,然后下一个窗口累加上一个窗口的值
 * @author: ZhangYitian
 * @create: 2022-01-08 09:14
 */
public class ProcessingTimeTimerDemo2 {


    public static void main(String[] args) throws Exception {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> line = env.socketTextStream("doitedu03", 8888);

        SingleOutputStreamOperator<Tuple2<String, Integer>> wordAndCount = line.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String value) throws Exception {
                String[] fields = value.split(",");
                return Tuple2.of(fields[0], Integer.parseInt(fields[1]));
            }
        });


        KeyedStream<Tuple2<String, Integer>, String> keyed = wordAndCount.keyBy(t -> t.f0);


        keyed.process(new KeyedProcessFunction<String, Tuple2<String, Integer>, Tuple2<String, Integer>>() {

            private  transient ValueState<Integer> counter;

            @Override
            public void open(Configuration parameters) throws Exception {
                ValueStateDescriptor<Integer> valueStateDescriptor = new ValueStateDescriptor<>("wc-state", Integer.class);
                counter = getRuntimeContext().getState(valueStateDescriptor);
            }

            @Override
            public void processElement(Tuple2<String, Integer> value, Context ctx, Collector<Tuple2<String, Integer>> out) throws Exception {
                //获取当前的ProcessingTime
                long currentProcessingTime = ctx.timerService().currentProcessingTime();
                //将当前的ProcessingTime的下一分钟时间注册一个定时器
                long fireTime = currentProcessingTime - currentProcessingTime % 60000 + 60000;
                //如果注册相同数据的timeTimer,后面的会将前面的覆盖,即相同的timeTimer只会被触发一次
                ctx.timerService().registerProcessingTimeTimer(fireTime);

                Integer currentCount = value.f1;
                Integer historyCount = counter.value();

                if (historyCount == null){
                    historyCount = 0;
                }
                int totalCount = historyCount + currentCount;
                //更新状态
                counter.update(totalCount);
            }


            //当闹钟到了指定的时间,就会执行onTimer方法
            @Override
            public void onTimer(long timestamp, OnTimerContext ctx, Collector<Tuple2<String, Integer>> out) throws Exception {

                //获取value
                Integer currentValue = counter.value();
                //获取当前的key
                String currentKey = ctx.getCurrentKey();

                //如果想实现类似滚动窗口,不累加历史数据,只是累加当前窗口的数据,就清空状态
                //counter.update(0);

                //输出key和value
                out.collect(Tuple2.of(currentKey,currentValue));

            }
        }).print();

        env.execute();


    }
}

你可能感兴趣的:(Flink,flink,apache,big,data)