Flink的join操作樣例
join必須依賴視窗及watermark操作
public class Main3 {
public static final long DELAY = 3000l;
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setParallelism(1);
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
//watermark 自動生成時間,預設每100ms一次
env.getConfig().setAutoWatermarkInterval(200);
DataStream<Tuple3<String, String, Long>> src1 = env.addSource(FlinkSourceManager.getServSource()).flatMap(new FlatMapFunction<String, Tuple3<String, String, Long>>() {
@Override
public void flatMap(String value, Collector<Tuple3<String, String, Long>> out) throws Exception {
String str[] = value.split(",");
if (str.length > 2)
out.collect(Tuple3.of(str[0], str[1], Long.parseLong(str[2])));
}
}).name("src1").assignTimestampsAndWatermarks(new AssignerWithPeriodicWatermarks<Tuple3<String, String, Long>>() {
long currentTimeStamp;
//每生成一個watermark即呼叫一次獲取當前watermark時間
@Nullable
@Override
public Watermark getCurrentWatermark() {
return new Watermark(currentTimeStamp);
}
//每來一條資料都會呼叫此函式,更新watermark時間,視窗計算
@Override
public long extractTimestamp(Tuple3<String, String, Long> stringStringLongTuple3, long l) {
long eventTime = stringStringLongTuple3.f2;
currentTimeStamp = Math.max(eventTime, currentTimeStamp) - DELAY;
return eventTime;
}
});
DataStream<Tuple3<String, String, Long>> src2 = env.addSource(FlinkSourceManager.getRankSource()).flatMap(new FlatMapFunction<String, Tuple3<String, String, Long>>() {
@Override
public void flatMap(String value, Collector<Tuple3<String, String, Long>> out) throws Exception {
String str[] = value.split(",");
if (str.length > 2)
out.collect(Tuple3.of(str[0], str[1], Long.parseLong(str[2])));
}
}).name("src2").assignTimestampsAndWatermarks(new AssignerWithPeriodicWatermarks<Tuple3<String, String, Long>>() {
long currentTimeStamp;
//每生成一個watermark即呼叫一次獲取當前watermark時間
@Nullable
@Override
public Watermark getCurrentWatermark() {
return new Watermark(currentTimeStamp);
}
//每來一條資料都會呼叫此函式,更新watermark時間,視窗計算
@Override
public long extractTimestamp(Tuple3<String, String, Long> stringStringLongTuple3, long l) {
long eventTime = stringStringLongTuple3.f2;
currentTimeStamp = Math.max(eventTime, currentTimeStamp) - DELAY;
return eventTime;
}
});
src1.join(src2)
.where(new KeySelector<Tuple3<String, String, Long>, Object>() {
@Override
public Object getKey(Tuple3<String, String, Long> value) throws Exception {
return value.f0;
}
})
.equalTo(new KeySelector<Tuple3<String, String, Long>, Object>() {
@Override
public Object getKey(Tuple3<String, String, Long> value) throws Exception {
return value.f0;
}
})
.window(TumblingEventTimeWindows.of(Time.seconds(10)))
.apply(new FlatJoinFunction<Tuple3<String, String, Long>, Tuple3<String, String, Long>, String>() {
@Override
public void join(Tuple3<String, String, Long> first, Tuple3<String, String, Long> second, Collector<String> out) throws Exception {
System.out.println(first.f0 + "|" + first.f1 + "|" + first.f2 + "|" + second.f1 + "|" + second.f2);
out.collect(first.f0 + "|" + first.f1 + "|" + first.f2 + "|" + second.f1 + "|" + second.f2);
}
}).print();
env.execute();
}
}
注:只有watermark時間達到視窗結束時間才會觸發視窗計算,否則會一直等待資料,或者實現視窗的觸發器來觸發視窗計算
相關文章
- Flink SQL 如何實現資料流的 Join?SQL
- 使用 Alluxio 最佳化 EMR 上 Flink JoinUX
- flink batch dataset 的基本操作BAT
- join、inner join、left join、right join、outer join的區別
- 聊聊flink Table的where及filter操作Filter
- 使用Reactor完成類似的Flink的操作React
- 不一樣的Flink入門教程
- Flink SQL之Over 聚合操作SQL
- Flink -- Operator操作運算元
- Mysql join 的本質,Nested Loop Join 和 Hash JoinMySqlOOP
- sql的left join 、right join 、inner join之間的區別SQL
- mysql中的left join、right join 、inner join的詳細用法MySql
- MySQL LEFT JOIN/ INNER JOIN/RIGHT JOINMySql
- js陣列的常見操作( push、pop、unshift、shift、splice、concat、 join)的用法JS陣列
- flink將bathch dataset 轉換為sql操作BATSQL
- 例2.3 列表操作示例
- 一條SQL完成跨資料庫例項Join查詢SQL資料庫
- Flink CDC 3.0 耍起來到底怎麼樣?
- Spring Cloud使用樣例SpringCloud
- HTML入門(樣例)HTML
- 【openfst樣例1】TokenizationNFS
- Velocity-2 樣例
- jQuery 樣式操作jQuery
- Inner Join, Left Outer Join和Association的區別
- ThreeJS 的效果樣例流水管線(五)JS
- java的join()方法Java
- MySQL JOIN的使用MySql
- 樣例軍聽認行通年油例uun
- dom操作程式碼例項
- mysql left join轉inner joinMySql
- WPF登入介面樣例
- Flink 對鍵控流和非鍵控流的開窗操作
- SQLAlchemy Join Query 得到的不同資料行相同資料是同一個例項SQL
- .join()
- sql 連線查詢例項(left join)三表連線查詢SQL
- LEFT JOIN 和JOIN 多表連線
- sql:left join和join區別SQL
- [Flink] Flink 版本特性的演進