Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use TimeWindowAll of Flink

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report