-
Notifications
You must be signed in to change notification settings - Fork 21
/
AbstractMapStreamBundleOperatorDynamic.java
95 lines (77 loc) · 3.07 KB
/
AbstractMapStreamBundleOperatorDynamic.java
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
package org.sense.flink.examples.stream.operator;
import static org.apache.flink.util.Preconditions.checkNotNull;
import java.util.HashMap;
import java.util.Map;
import org.apache.flink.streaming.api.operators.AbstractUdfStreamOperator;
import org.apache.flink.streaming.api.operators.ChainingStrategy;
import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
import org.apache.flink.streaming.api.operators.StreamOperator;
import org.apache.flink.streaming.api.operators.TimestampedCollector;
import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
import org.sense.flink.examples.stream.trigger.BundleTriggerCallback;
import org.sense.flink.examples.stream.trigger.BundleTriggerDynamic;
import org.sense.flink.examples.stream.udf.MapBundleFunction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A {@link StreamOperator} for executing {@link MapBundleFunction
* MapFunctions}.
*/
public abstract class AbstractMapStreamBundleOperatorDynamic<K, V, IN, OUT>
extends AbstractUdfStreamOperator<OUT, MapBundleFunction<K, V, IN, OUT>>
implements OneInputStreamOperator<IN, OUT>, BundleTriggerCallback {
private static final long serialVersionUID = -6191200344708768482L;
private static final Logger logger = LoggerFactory.getLogger(AbstractMapStreamBundleOperatorDynamic.class);
/** The map in heap to store elements. */
private final Map<K, V> bundle;
/**
* The trigger that determines how many elements should be put into a bundle.
*/
private final BundleTriggerDynamic<K, IN> bundleTrigger;
/** Output for stream records. */
private transient TimestampedCollector<OUT> collector;
private transient int numOfElements = 0;
public AbstractMapStreamBundleOperatorDynamic(MapBundleFunction<K, V, IN, OUT> function,
BundleTriggerDynamic<K, IN> bundleTrigger) {
super(function);
chainingStrategy = ChainingStrategy.ALWAYS;
this.bundle = new HashMap<>();
this.bundleTrigger = checkNotNull(bundleTrigger, "bundleTrigger is null");
}
@Override
public void open() throws Exception {
super.open();
numOfElements = 0;
collector = new TimestampedCollector<>(output);
bundleTrigger.registerCallback(this);
// reset trigger
bundleTrigger.reset();
}
@Override
public void processElement(StreamRecord<IN> element) throws Exception {
// get the key and value for the map bundle
final IN input = element.getValue();
final K bundleKey = getKey(input);
final V bundleValue = this.bundle.get(bundleKey);
// get a new value after adding this element to bundle
final V newBundleValue = userFunction.addInput(bundleValue, input);
// update to map bundle
bundle.put(bundleKey, newBundleValue);
numOfElements++;
bundleTrigger.onElement(bundleKey, input);
}
/**
* Get the key for current processing element, which will be used as the map
* bundle's key.
*/
protected abstract K getKey(final IN input) throws Exception;
@Override
public void finishBundle() throws Exception {
if (!bundle.isEmpty()) {
numOfElements = 0;
userFunction.finishBundle(bundle, collector);
bundle.clear();
}
bundleTrigger.reset();
}
}