|
22 | 22 | import org.apache.flink.api.common.state.ValueState;
|
23 | 23 | import org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer;
|
24 | 24 | import org.apache.flink.api.java.utils.ParameterTool;
|
| 25 | +import org.apache.flink.formats.avro.typeutils.AvroSerializer; |
25 | 26 | import org.apache.flink.streaming.api.datastream.DataStream;
|
26 | 27 | import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
|
27 | 28 | import org.apache.flink.streaming.api.functions.sink.PrintSinkFunction;
|
28 | 29 | import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
|
29 | 30 | import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
|
30 | 31 | import org.apache.flink.streaming.tests.artificialstate.ComplexPayload;
|
| 32 | +import org.apache.flink.streaming.tests.avro.ComplexPayloadAvro; |
| 33 | +import org.apache.flink.streaming.tests.avro.InnerPayLoadAvro; |
31 | 34 | import org.apache.flink.util.Collector;
|
32 | 35 |
|
| 36 | +import java.util.Arrays; |
33 | 37 | import java.util.Collections;
|
34 | 38 |
|
35 | 39 | import static org.apache.flink.streaming.tests.DataStreamAllroundTestJobFactory.applyTumblingWindows;
|
@@ -72,26 +76,53 @@ public static void main(String[] args) throws Exception {
|
72 | 76 |
|
73 | 77 | setupEnvironment(env, pt);
|
74 | 78 |
|
| 79 | + // add a keyed stateful map operator, which uses Kryo for state serialization |
75 | 80 | DataStream<Event> eventStream = env.addSource(createEventSource(pt))
|
76 | 81 | .assignTimestampsAndWatermarks(createTimestampExtractor(pt))
|
77 | 82 | .keyBy(Event::getKey)
|
78 | 83 | .map(createArtificialKeyedStateMapper(
|
79 | 84 | // map function simply forwards the inputs
|
80 | 85 | (MapFunction<Event, Event>) in -> in,
|
81 | 86 | // state is verified and updated per event as a wrapped ComplexPayload state object
|
82 |
| - (Event first, ComplexPayload second) -> { |
83 |
| - if (second != null && !second.getStrPayload().equals(KEYED_STATE_OPER_NAME)) { |
| 87 | + (Event event, ComplexPayload lastState) -> { |
| 88 | + if (lastState != null && !lastState.getStrPayload().equals(KEYED_STATE_OPER_NAME) |
| 89 | + && lastState.getInnerPayLoad().getSequenceNumber() == (event.getSequenceNumber() - 1)) { |
84 | 90 | System.out.println("State is set or restored incorrectly");
|
85 | 91 | }
|
86 |
| - return new ComplexPayload(first, KEYED_STATE_OPER_NAME); |
| 92 | + return new ComplexPayload(event, KEYED_STATE_OPER_NAME); |
87 | 93 | },
|
88 | 94 | Collections.singletonList(
|
89 | 95 | new KryoSerializer<>(ComplexPayload.class, env.getConfig())), // custom KryoSerializer
|
90 | 96 | Collections.singletonList(ComplexPayload.class) // KryoSerializer via type extraction
|
91 | 97 | )
|
92 |
| - ) |
93 |
| - .name(KEYED_STATE_OPER_NAME) |
94 |
| - .returns(Event.class); |
| 98 | + ).returns(Event.class).name(KEYED_STATE_OPER_NAME + "_Kryo"); |
| 99 | + |
| 100 | + // add a keyed stateful map operator, which uses Avro for state serialization |
| 101 | + eventStream = eventStream |
| 102 | + .keyBy(Event::getKey) |
| 103 | + .map(createArtificialKeyedStateMapper( |
| 104 | + // map function simply forwards the inputs |
| 105 | + (MapFunction<Event, Event>) in -> in, |
| 106 | + // state is verified and updated per event as a wrapped ComplexPayloadAvro state object |
| 107 | + (Event event, ComplexPayloadAvro lastState) -> { |
| 108 | + if (lastState != null && !lastState.getStrPayload().equals(KEYED_STATE_OPER_NAME) |
| 109 | + && lastState.getInnerPayLoad().getSequenceNumber() == (event.getSequenceNumber() - 1)) { |
| 110 | + System.out.println("State is set or restored incorrectly"); |
| 111 | + } |
| 112 | + |
| 113 | + ComplexPayloadAvro payload = new ComplexPayloadAvro(); |
| 114 | + payload.setEventTime(event.getEventTime()); |
| 115 | + payload.setInnerPayLoad(new InnerPayLoadAvro(event.getSequenceNumber())); |
| 116 | + payload.setStrPayload(KEYED_STATE_OPER_NAME); |
| 117 | + payload.setStringList(Arrays.asList(String.valueOf(event.getKey()), event.getPayload())); |
| 118 | + |
| 119 | + return payload; |
| 120 | + }, |
| 121 | + Collections.singletonList( |
| 122 | + new AvroSerializer<>(ComplexPayloadAvro.class)), // custom AvroSerializer |
| 123 | + Collections.singletonList(ComplexPayloadAvro.class) // AvroSerializer via type extraction |
| 124 | + ) |
| 125 | + ).returns(Event.class).name(KEYED_STATE_OPER_NAME + "_Avro"); |
95 | 126 |
|
96 | 127 | DataStream<Event> eventStream2 = eventStream
|
97 | 128 | .map(createArtificialOperatorStateMapper((MapFunction<Event, Event>) in -> in))
|
|
0 commit comments