1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| public class JoinDataStreamJob {
public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
env.getConfig().setAutoWatermarkInterval(200L);
env.setParallelism(1);
final ParameterTool parameterTool = ParameterTool.fromArgs(args); String host1 = parameterTool.get("host1"); int port1 = parameterTool.getInt("port1");
final DataStreamSource<String> rideSource = env.socketTextStream(host1, port1);
final SingleOutputStreamOperator<TaxiRide> rideDataStream = rideSource.map(line -> { String[] fields = line.split(",");
return new TaxiRide(fields[0], fields[1], Long.parseLong(fields[2])); });
final WatermarkStrategy<TaxiRide> riderWatermarkStrategy = WatermarkStrategy .<TaxiRide>forBoundedOutOfOrderness(Duration.ofSeconds(0)) .withTimestampAssigner((event, timestamp) -> event.getTimestamp() * 1000) .withIdleness(Duration.ofSeconds(1));
final SingleOutputStreamOperator<TaxiRide> rideEventDataStream = rideDataStream.assignTimestampsAndWatermarks(riderWatermarkStrategy);
String host2 = parameterTool.get("host2"); int port2 = parameterTool.getInt("port2");
final DataStreamSource<String> fareSource = env.socketTextStream(host2, port2);
final SingleOutputStreamOperator<TaxiFare> fareDataStream = fareSource.map(line -> { String[] fields = line.split(",");
return new TaxiFare(fields[0], Double.parseDouble(fields[1]), Long.parseLong(fields[2])); });
final WatermarkStrategy<TaxiFare> fareWatermarkStrategy = WatermarkStrategy .<TaxiFare>forBoundedOutOfOrderness(Duration.ofSeconds(0)) .withTimestampAssigner((event, timestamp) -> event.getTimestamp() * 1000) .withIdleness(Duration.ofSeconds(1));
final SingleOutputStreamOperator<TaxiFare> fareEventDataStream = fareDataStream.assignTimestampsAndWatermarks(fareWatermarkStrategy);
final KeyedStream<TaxiRide, String> taxiRideStringKeyedStream = rideEventDataStream.keyBy(taxiRide -> taxiRide.getRideId()); final KeyedStream<TaxiFare, String> taxiFareStringKeyedStream = fareEventDataStream.keyBy(taxiFare -> taxiFare.getRideId());
final SingleOutputStreamOperator<RideFare> rideFareStream = taxiRideStringKeyedStream.connect(taxiFareStringKeyedStream) .process(new RiderFareProcessMapFuntion()); rideFareStream.print("ride-fare");
OutputTag<RideFare> outputTag = new OutputTag<RideFare>("ride-fare") { }; final DataStream<RideFare> sideOutputStream = rideFareStream.getSideOutput(outputTag); sideOutputStream.print("side-output");
env.execute("Flink Join Training Job"); }
private static class RiderFareProcessMapFuntion extends KeyedCoProcessFunction<String, TaxiRide, TaxiFare, RideFare> {
private static final int TIMET_OUT_MS = 60 * 1000;
private ValueState<TaxiRide> rideState;
private ValueState<TaxiFare> fareState;
private ValueState<Long> timeoutState;
@Override public void open(Configuration parameters) throws Exception { rideState = getRuntimeContext().getState(new ValueStateDescriptor<TaxiRide>("ride", TaxiRide.class)); fareState = getRuntimeContext().getState(new ValueStateDescriptor<TaxiFare>("fare", TaxiFare.class)); timeoutState = getRuntimeContext().getState(new ValueStateDescriptor<Long>("timeout", Long.class)); }
@Override public void processElement1(TaxiRide value, Context ctx, Collector<RideFare> out) throws Exception { TaxiFare fare = fareState.value(); if (fare != null) { fareState.clear(); out.collect(new RideFare(value, fare));
Long timeout = timeoutState.value();
ctx.timerService().deleteEventTimeTimer(timeout); System.out.println("del Timer:" + timeout);
timeoutState.clear();
} else { rideState.update(value);
Long timeout = ctx.timestamp() + TIMET_OUT_MS; timeoutState.update(timeout);
ctx.timerService().registerEventTimeTimer(timeout); System.out.println("reg Timer:" + timeout); }
}
@Override public void processElement2(TaxiFare value, Context ctx, Collector<RideFare> out) throws Exception { TaxiRide ride = rideState.value(); if (ride != null) { rideState.clear(); out.collect(new RideFare(ride, value));
Long timeout = timeoutState.value();
ctx.timerService().deleteEventTimeTimer(timeout); System.out.println("del Timer:" + timeout);
timeoutState.clear();
} else { fareState.update(value);
Long timeout = ctx.timestamp() + TIMET_OUT_MS; timeoutState.update(timeout);
ctx.timerService().registerEventTimeTimer(timeout); System.out.println("reg Timer:" + timeout); } }
@Override public void onTimer(long timestamp, OnTimerContext ctx, Collector<RideFare> out) throws Exception {
System.out.println("onTimer");
TaxiRide ride = rideState.value(); TaxiFare fare = fareState.value();
RideFare rideFare = new RideFare(); rideFare.setFare(fare); rideFare.setRide(ride);
OutputTag<RideFare> outputTag = new OutputTag<RideFare>("ride-fare") { }; ctx.output(outputTag, rideFare);
rideState.clear(); fareState.clear(); timeoutState.clear(); } } }
|