我正在开始使用flink并研究one of the official tutorials。
据我所知,本练习的目的是在time属性上加入两个流。
任务:
此练习的结果是Tuple2记录的数据流,每个不同的rideId一个。您应该忽略 END事件,并且只能在每次骑行的START时加入该事件 其相应的票价数据。
结果流应打印为标准输出。
问题:EnrichmentFunction如何又可以将两个流合并在一起。它怎么知道参加哪个游乐项目的公平?我希望它可以缓冲多个博览会/竞赛,直到传入的博览会/竞赛有一个匹配的伙伴。
以我的理解,它只是保存了它看到的每一个乘车/展览,并将其与下一个最佳乘车/展览结合在一起。为什么这是适当的联接?
提供的解决方案:
/*
* Copyright 2017 data Artisans GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dataartisans.flinktraining.solutions.datastream_java.state;
import com.dataartisans.flinktraining.exercises.datastream_java.datatypes.TaxiFare;
import com.dataartisans.flinktraining.exercises.datastream_java.datatypes.TaxiRide;
import com.dataartisans.flinktraining.exercises.datastream_java.sources.TaxiFareSource;
import com.dataartisans.flinktraining.exercises.datastream_java.sources.TaxiRideSource;
import com.dataartisans.flinktraining.exercises.datastream_java.utils.ExerciseBase;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.configuration.Configuration;
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.functions.co.RichCoFlatMapFunction;
import org.apache.flink.util.Collector;
/**
* Java reference implementation for the "Stateful Enrichment" exercise of the Flink training
* (http://training.data-artisans.com).
*
* The goal for this exercise is to enrich TaxiRides with fare information.
*
* Parameters:
* -rides path-to-input-file
* -fares path-to-input-file
*
*/
public class RidesAndFaresSolution extends ExerciseBase {
public static void main(String[] args) throws Exception {
ParameterTool params = ParameterTool.fromArgs(args);
final String ridesFile = params.get("rides", pathToRideData);
final String faresFile = params.get("fares", pathToFareData);
final int delay = 60; // at most 60 seconds of delay
final int servingSpeedFactor = 1800; // 30 minutes worth of events are served every second
// set up streaming execution environment
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
env.setParallelism(ExerciseBase.parallelism);
DataStream<TaxiRide> rides = env
.addSource(rideSourceOrTest(new TaxiRideSource(ridesFile, delay, servingSpeedFactor)))
.filter((TaxiRide ride) -> ride.isStart)
.keyBy("rideId");
DataStream<TaxiFare> fares = env
.addSource(fareSourceOrTest(new TaxiFareSource(faresFile, delay, servingSpeedFactor)))
.keyBy("rideId");
DataStream<Tuple2<TaxiRide, TaxiFare>> enrichedRides = rides
.connect(fares)
.flatMap(new EnrichmentFunction());
printOrTest(enrichedRides);
env.execute("Join Rides with Fares (java RichCoFlatMap)");
}
public static class EnrichmentFunction extends RichCoFlatMapFunction<TaxiRide, TaxiFare, Tuple2<TaxiRide, TaxiFare>> {
// keyed, managed state
private ValueState<TaxiRide> rideState;
private ValueState<TaxiFare> fareState;
@Override
public void open(Configuration config) {
rideState = getRuntimeContext().getState(new ValueStateDescriptor<>("saved ride", TaxiRide.class));
fareState = getRuntimeContext().getState(new ValueStateDescriptor<>("saved fare", TaxiFare.class));
}
@Override
public void flatMap1(TaxiRide ride, Collector<Tuple2<TaxiRide, TaxiFare>> out) throws Exception {
TaxiFare fare = fareState.value();
if (fare != null) {
fareState.clear();
out.collect(new Tuple2(ride, fare));
} else {
rideState.update(ride);
}
}
@Override
public void flatMap2(TaxiFare fare, Collector<Tuple2<TaxiRide, TaxiFare>> out) throws Exception {
TaxiRide ride = rideState.value();
if (ride != null) {
rideState.clear();
out.collect(new Tuple2(ride, fare));
} else {
fareState.update(fare);
}
}
}
}
答案 0 :(得分:4)
在此特定的训练练习中,每个rideId值都有三个事件-TaxiRide开始事件,TaxiRide结束事件和TaxiFare。本练习的目的是将每个TaxiRide启动事件与具有相同rideId的一个TaxiFare事件相关联-换句话说,在rideId上加入乘车流和票价流,同时知道每个将只有一个。
此练习演示了Flink中的键控状态如何工作。键控状态实际上是分片键值存储。当我们有一个ValueState
项,例如ValueState<TaxiRide> rideState
时,Flink将在状态后端为密钥的每个不同值(rideId
)存储一个单独的记录。
每次调用flatMap1
和flatMap2
时,都会在上下文中隐含一个键(一个rideId
),当我们调用rideState.update(ride)
或rideState.value()
时,不是访问单个变量,而是使用rideId
作为键在键值存储中设置和获取条目。
在此练习中,两个流都由rideId
键控,因此每个不同的rideState
可能都有fareState
的一个元素和rideId
的一个元素。因此,提供的解决方案是缓冲大量的游乐设施和票价,但是每个rideId
仅缓冲一个(这足以满足条件,因为游乐设施和票价在此数据集中是完美配对的。)
所以,你问:
EnrichmentFunction如何又可以将两个流加入。它怎么知道哪种票价和哪种搭车?
答案是
它加入具有相同
rideId
的票价。
您询问过的这个特定练习显示了如何实现简单的扩充连接,以了解键控状态和连接的流的思想。但是使用Flink当然可以进行更复杂的连接。有关更多信息,请参见the docs on joining,joins with Flink's Table API,joins with Flink SQL和exercise on time-based enrichment joins。