当前位置:网站首页>Flink's timewindow

Flink's timewindow

2022-06-10 05:45:00 Dragon man who can't fly

timeWindow Time window ( The sliding window 【 The difference between scrolling windows and sliding windows , The reason is that the data elements may overlap in the sliding window , There is no element overlap in the scrolling window 】)

Sample environment

java.version: 1.8.x
flink.version: 1.11.1

Sample data source  ( Project code cloud download )

Flink System examples And Build development environment and data

TimeWindow.java

import com.flink.examples.DataSource;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.source.RichSourceFunction;
import org.apache.flink.streaming.api.windowing.time.Time;
import java.util.List;

/**
 * @Description timeWindow Time window ( The sliding window 【 The difference between scrolling windows and sliding windows , The reason is that the data elements may overlap in the sliding window , There is no element overlap in the scrolling window 】)
 */
public class TimeWindow {

    /**
     *  Ergodic set , Returns the maximum age data record of each gender partition in the specified time sliding window 
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //env.setParallelism(1);
        DataStream<Tuple3<String, String, Integer>> inStream = env.addSource(new MyRichSourceFunction());
        DataStream<Tuple3<String, String, Integer>> dataStream = inStream
                .keyBy((KeySelector<Tuple3<String, String, Integer>, String>) k ->k.f1)
                // Press the time window to slide , Every time 3 Seconds is a time window , And slide every time 2 second ( Simply speaking : every other 2 Seconds to front 3 Second input data stream ), Calculate once 
                .timeWindow(Time.seconds(3), Time.seconds(2))
                // Be careful : The calculated variable is f2
                .maxBy(2);
        dataStream.print();
        env.execute("flink TimeWindow job");
    }

    /**
     *  Analog data continues to output 
     */
    public static class MyRichSourceFunction extends RichSourceFunction<Tuple3<String, String, Integer>> {
        @Override
        public void run(SourceContext<Tuple3<String, String, Integer>> ctx) throws Exception {
            List<Tuple3<String, String, Integer>> tuple3List = DataSource.getTuple3ToList();
            for (Tuple3 tuple3 : tuple3List){
                ctx.collect(tuple3);
                //1 One per second 
                Thread.sleep(1 * 1000);
            }
        }
        @Override
        public void cancel() {
            try{
                super.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}

Print the results

3> ( Zhang San ,1,20)
4> ( Li Si ,2,24)
3> ( Wang Wu ,1,29)
3> ( Wang Wu ,1,29)
4> ( Liu Liu ,2,32)
原网站

版权声明
本文为[Dragon man who can't fly]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100538394925.html