2021年大数据Flink(二十一):案例三 会话窗口
生活随笔
收集整理的這篇文章主要介紹了
2021年大数据Flink(二十一):案例三 会话窗口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
案例三 會話窗口
需求
代碼實現
案例三 會話窗口
需求
設置會話超時時間為10s,10s內沒有數據到來,則觸發上個窗口的計算
代碼實現
package cn.it.window;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.ProcessingTimeSessionWindows;
import org.apache.flink.streaming.api.windowing.time.Time;/*** Author lanson* Desc* nc -lk 9999* 有如下數據表示:* 信號燈編號和通過該信號燈的車的數量
9,3
9,2
9,7
4,9
2,6
1,5
2,3
5,7
5,4* 需求:設置會話超時時間為10s,10s內沒有數據到來,則觸發上個窗口的計算(前提是上一個窗口得有數據!)*/
public class WindowDemo03_SessionWindow {public static void main(String[] args) throws Exception {//1.envStreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();//2.SourceDataStreamSource<String> socketDS = env.socketTextStream("node1", 9999);//3.Transformation//將9,3轉為CartInfo(9,3)SingleOutputStreamOperator<CartInfo> cartInfoDS = socketDS.map(new MapFunction<String, CartInfo>() {@Overridepublic CartInfo map(String value) throws Exception {String[] arr = value.split(",");return new CartInfo(arr[0], Integer.parseInt(arr[1]));}});//需求:設置會話超時時間為10s,10s內沒有數據到來,則觸發上個窗口的計算(前提是上一個窗口得有數據!)SingleOutputStreamOperator<CartInfo> result = cartInfoDS.keyBy(CartInfo::getSensorId).window(ProcessingTimeSessionWindows.withGap(Time.seconds(10))).sum("count");//4.Sinkresult.print();//5.executeenv.execute();}@Data@AllArgsConstructor@NoArgsConstructorpublic static class CartInfo {private String sensorId;//信號燈idprivate Integer count;//通過該信號燈的車的數量}
}
總結
以上是生活随笔為你收集整理的2021年大数据Flink(二十一):案例三 会话窗口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021年大数据Flink(二十):案例
- 下一篇: 2021年大数据Flink(二十二):T