|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.flink.datastream.impl.common; |
| 20 | + |
| 21 | +import org.apache.flink.api.java.functions.KeySelector; |
| 22 | +import org.apache.flink.util.ExceptionUtils; |
| 23 | + |
| 24 | +import java.util.function.Supplier; |
| 25 | + |
| 26 | +import static org.apache.flink.util.Preconditions.checkNotNull; |
| 27 | + |
| 28 | +/** |
| 29 | + * This output checks whether the current key of the output record and the key extracted with a |
| 30 | + * specific key selector are exactly the same. |
| 31 | + */ |
| 32 | +public class KeyCheckedOutputCollector<KEY, OUT> extends TimestampCollector<OUT> { |
| 33 | + private final TimestampCollector<OUT> collector; |
| 34 | + |
| 35 | + private final KeySelector<OUT, KEY> outKeySelector; |
| 36 | + |
| 37 | + private final Supplier<KEY> currentKeyGetter; |
| 38 | + |
| 39 | + public KeyCheckedOutputCollector( |
| 40 | + TimestampCollector<OUT> collector, |
| 41 | + KeySelector<OUT, KEY> outKeySelector, |
| 42 | + Supplier<KEY> currentKeyGetter) { |
| 43 | + this.collector = collector; |
| 44 | + this.outKeySelector = outKeySelector; |
| 45 | + this.currentKeyGetter = currentKeyGetter; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void collect(OUT outputRecord) { |
| 50 | + checkOutputKey(outputRecord); |
| 51 | + collector.collect(outputRecord); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void collectAndOverwriteTimestamp(OUT outputRecord, long timestamp) { |
| 56 | + checkOutputKey(outputRecord); |
| 57 | + collector.collectAndOverwriteTimestamp(outputRecord, timestamp); |
| 58 | + } |
| 59 | + |
| 60 | + private void checkOutputKey(OUT outputRecord) { |
| 61 | + try { |
| 62 | + KEY currentKey = currentKeyGetter.get(); |
| 63 | + KEY outputKey = checkNotNull(outKeySelector).getKey(outputRecord); |
| 64 | + if (!outputKey.equals(currentKey)) { |
| 65 | + throw new IllegalStateException( |
| 66 | + "Output key must equals to input key if the output key selector is not null."); |
| 67 | + } |
| 68 | + } catch (Exception e) { |
| 69 | + ExceptionUtils.rethrow(e); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments