Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use TimeWindowAll of Flink

Shulou Source: shulou.com Published: 2022-06-01 06:16:04 11月21日 Update

This article mainly explains how to use Flink's TimeWindowAll. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn how to use Flink's TimeWindowAll!

timeWindow time window (sliding window [the difference between sliding window and scrolling window is that sliding window may overlap data elements, while scrolling window does not overlap elements])

example environment

java.version: 1.8.xflink.version: 1.11.1

Sample Data Source (Project Code Cloud Download)

Flink System Example: Building 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 (sliding window [the difference between sliding window and scrolling window is that sliding window may overlap data elements, while scrolling window does not overlap elements])*/public class TimeWindow { /** * Traversing the collection, returning the maximum age data record in each gender partition under 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 inStream = env.addSource(new MyRichSourceFunction()); DataStream dataStream = inStream .keyBy((KeySelector) k ->k.f1) //slide by time window, every 3 seconds for a time window, and slide by 2 seconds each time (in short: every 2 seconds for the input data stream in the previous 3 seconds), calculate once .timeWindow(Time.seconds(3), Time.seconds(2)) //Note: The calculated variable is f2 .maxBy(2); dataStream.print(); env.execute("flink TimeWindow job"); } /** * Analog data continuous output */ public static class MyRichSourceFunction extends RichSourceFunction { @Override public void run(SourceContext ctx) throws Exception { List tuple3List = DataSource.getTuple3ToList(); for (Tuple3 tuple3 : tuple3List){ ctx.collect(tuple3); //1 second output one Thread.sleep(1 * 1000); } } @Override public void cancel() { try{ super.close(); }catch (Exception e){ e.printStackTrace(); } } }}

print result

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) At this point, I believe that everyone has a deeper understanding of "How to use Flink's TimeWindowAll". Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Tags: Data time elements content environment examples Wang Wu learning output practical deeper maximum interest variables practicality actual age gender simple operation data flow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Huawei Xiaomi Microsoft MariaDB