Apache Flink: Count window with timeout

前端 未结 3 2170
日久生厌
日久生厌 2020-12-19 13:18

Here is a simple code example to illustrate my question:

case class Record( key: String, value: Int )

object Job extends App
{
  val env = StreamExecutionEn         


        
3条回答
  •  借酒劲吻你
    2020-12-19 13:46

    I think you can implement this use case using ProcessFunction

    In which you have count property and windowEnd property. Using that you can decide when to collect the data.

    public class TimeCountWindowProcessFunction extends ProcessFunction {
    
        protected long windowStart;
        protected long windowEnd;
        protected long count;
        private ValueState state;
    
        public TimeCountWindowProcessFunction(long windowSize, long count) {
    
        this.windowSize = windowSize;
        this.count = count;
    
        }
    
    @Override
        public void open(Configuration parameters) {
    
        TypeInformation typeInformation = TypeInformation.of(new TypeHint() {
        });
        ValueStateDescriptor descriptor = new ValueStateDescriptor("test", typeInformation);
    
        state = getRuntimeContext().getState(descriptor);
    }
    
    
        @Override
        public void processElement(CountPojo input, Context ctx, Collector out)
                throws Exception {
    
        long timestamp = ctx.timestamp();
            windowStart = timestamp - (timestamp % windowSize);
            windowEnd = windowStart + windowSize;
    
            // retrieve the current count
            CountPojo current = (CountPojo) state.value();
    
            if (current == null) {
    
                current = new CountPojo();
            current.count = 1;
    
                ctx.timerService().registerEventTimeTimer(windowEnd);
            } else {
    
                current.count += 1;
            }
    
            if(current.count >= count) {
            out.collect(current);
        }
    
            // set the state's timestamp to the record's assigned event time timestamp
            current.setLastModified(ctx.timestamp());
    
            // write the state back
            state.update(current);
        }
    
    
        @Override
        public void onTimer(long timestamp, OnTimerContext ctx, Collector out)
                throws Exception {
    
    
            if (windowEnd == timestamp) {
    
                out.collect(state.value());
            }
    
            state.clear();
        }
    }
    

    I hope this will helpful to you.

提交回复
热议问题