您好,登錄后才能下訂單哦!
這篇文章主要介紹“Flink的CoGroup如何使用”,在日常操作中,相信很多人在Flink的CoGroup如何使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Flink的CoGroup如何使用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
CoGroup算子:將兩個數據流按照key進行group分組,并將數據流按key進行分區的處理,最終合成一個數據流(與join有區別,不管key有沒有關聯上,最終都會合并成一個數據流)
示例環境
java.version: 1.8.x flink.version: 1.11.1
示例數據源 (項目碼云下載)
Flink 系例 之 搭建開發環境與數據
CoGroup.java
package com.flink.examples.functions; import com.flink.examples.DataSource; import com.google.gson.Gson; import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner; import org.apache.flink.api.common.eventtime.WatermarkStrategy; import org.apache.flink.api.common.functions.CoGroupFunction; import org.apache.flink.api.java.functions.KeySelector; import org.apache.flink.api.java.tuple.Tuple3; import org.apache.flink.streaming.api.TimeCharacteristic; import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows; import org.apache.flink.streaming.api.windowing.time.Time; import org.apache.flink.util.Collector; import java.time.Duration; import java.util.Arrays; import java.util.List; /** * @Description CoGroup算子:將兩個數據流按照key進行group分組,并將數據流按key進行分區的處理,最終合成一個數據流(與join有區別,不管key有沒有關聯上,最終都會合并成一個數據流) */ public class CoGroup { /** * 兩個數據流集合,對相同key進行內聯,分配到同一個窗口下,合并并打印 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.setParallelism(1); env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime); //watermark 自動添加水印調度時間 //env.getConfig().setAutoWatermarkInterval(200); List<Tuple3<String, String, Integer>> tuple3List1 = DataSource.getTuple3ToList(); List<Tuple3<String, String, Integer>> tuple3List2 = Arrays.asList( new Tuple3<>("伍七", "girl", 18), new Tuple3<>("吳八", "man", 30) ); //Datastream 1 DataStream<Tuple3<String, String, Integer>> dataStream1 = env.fromCollection(tuple3List1) //添加水印窗口,如果不添加,則時間窗口會一直等待水印事件時間,不會執行apply .assignTimestampsAndWatermarks(WatermarkStrategy .<Tuple3<String, String, Integer>>forBoundedOutOfOrderness(Duration.ofSeconds(2)) .withTimestampAssigner((element, timestamp) -> System.currentTimeMillis())); //Datastream 2 DataStream<Tuple3<String, String, Integer>> dataStream2 = env.fromCollection(tuple3List2) //添加水印窗口,如果不添加,則時間窗口會一直等待水印事件時間,不會執行apply .assignTimestampsAndWatermarks(WatermarkStrategy .<Tuple3<String, String, Integer>>forBoundedOutOfOrderness(Duration.ofSeconds(2)) .withTimestampAssigner(new SerializableTimestampAssigner<Tuple3<String, String, Integer>>() { @Override public long extractTimestamp(Tuple3<String, String, Integer> element, long timestamp) { return System.currentTimeMillis(); } }) ); //對dataStream1和dataStream2兩個數據流進行關聯,沒有關聯也保留 //Datastream 3 DataStream<String> newDataStream = dataStream1.coGroup(dataStream2) .where(new KeySelector<Tuple3<String, String, Integer>, String>() { @Override public String getKey(Tuple3<String, String, Integer> value) throws Exception { return value.f1; } }) .equalTo(t3->t3.f1) .window(TumblingEventTimeWindows.of(Time.seconds(1))) .apply(new CoGroupFunction<Tuple3<String, String, Integer>, Tuple3<String, String, Integer>, String>() { @Override public void coGroup(Iterable<Tuple3<String, String, Integer>> first, Iterable<Tuple3<String, String, Integer>> second, Collector<String> out) throws Exception { StringBuilder sb = new StringBuilder(); Gson gson = new Gson(); //datastream1的數據流集合 for (Tuple3<String, String, Integer> tuple3 : first) { sb.append(gson.toJson(tuple3)).append("\n"); } //datastream2的數據流集合 for (Tuple3<String, String, Integer> tuple3 : second) { sb.append(gson.toJson(tuple3)).append("\n"); } out.collect(sb.toString()); } }); newDataStream.print(); env.execute("flink CoGroup job"); } }
打印結果
{"f0":"張三","f1":"man","f2":20} {"f0":"王五","f1":"man","f2":29} {"f0":"吳八","f1":"man","f2":30} {"f0":"吳八","f1":"man","f2":30} {"f0":"李四","f1":"girl","f2":24} {"f0":"劉六","f1":"girl","f2":32} {"f0":"伍七","f1":"girl","f2":18} {"f0":"伍七","f1":"girl","f2":18}
到此,關于“Flink的CoGroup如何使用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。