|
| 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 | +package org.apache.beam.runners.dataflow; |
| 19 | + |
| 20 | +import java.util.Map; |
| 21 | +import org.apache.beam.sdk.io.kafka.KafkaIO; |
| 22 | +import org.apache.beam.sdk.io.kafka.KafkaRecord; |
| 23 | +import org.apache.beam.sdk.runners.AppliedPTransform; |
| 24 | +import org.apache.beam.sdk.runners.PTransformOverrideFactory; |
| 25 | +import org.apache.beam.sdk.transforms.PTransform; |
| 26 | +import org.apache.beam.sdk.util.construction.PTransformReplacements; |
| 27 | +import org.apache.beam.sdk.util.construction.ReplacementOutputs; |
| 28 | +import org.apache.beam.sdk.values.KV; |
| 29 | +import org.apache.beam.sdk.values.PCollection; |
| 30 | +import org.apache.beam.sdk.values.TupleTag; |
| 31 | + |
| 32 | +@SuppressWarnings({ |
| 33 | + "rawtypes" // TODO(https://github.com/apache/beam/issues/20447) |
| 34 | +}) |
| 35 | +public class KeyedByPartitionOverride { |
| 36 | + |
| 37 | + static class StreamingKeyedByPartitionOverrideFactory<K, V> |
| 38 | + implements PTransformOverrideFactory< |
| 39 | + PCollection<KafkaRecord<K, V>>, |
| 40 | + PCollection<KV<Integer, V>>, |
| 41 | + KafkaIO.Read.KeyedByPartition<K, V>> { |
| 42 | + |
| 43 | + private final DataflowRunner runner; |
| 44 | + |
| 45 | + StreamingKeyedByPartitionOverrideFactory(DataflowRunner runner) { |
| 46 | + this.runner = runner; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public PTransformReplacement<PCollection<KafkaRecord<K, V>>, PCollection<KV<Integer, V>>> |
| 51 | + getReplacementTransform( |
| 52 | + AppliedPTransform< |
| 53 | + PCollection<KafkaRecord<K, V>>, |
| 54 | + PCollection<KV<Integer, V>>, |
| 55 | + KafkaIO.Read.KeyedByPartition<K, V>> |
| 56 | + transform) { |
| 57 | + return PTransformReplacement.of( |
| 58 | + PTransformReplacements.getSingletonMainInput(transform), |
| 59 | + new StreamingKeyedByPartition<>( |
| 60 | + runner, |
| 61 | + transform.getTransform(), |
| 62 | + PTransformReplacements.getSingletonMainOutput(transform))); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public Map<PCollection<?>, ReplacementOutput> mapOutputs( |
| 67 | + Map<TupleTag<?>, PCollection<?>> outputs, PCollection<KV<Integer, V>> newOutput) { |
| 68 | + return ReplacementOutputs.singleton(outputs, newOutput); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + static class StreamingKeyedByPartition<K, V> |
| 73 | + extends PTransform<PCollection<KafkaRecord<K, V>>, PCollection<KV<Integer, V>>> { |
| 74 | + |
| 75 | + private final transient DataflowRunner runner; |
| 76 | + private final KafkaIO.Read.KeyedByPartition<K, V> originalTransform; |
| 77 | + private final transient PCollection<KV<Integer, V>> originalOutput; |
| 78 | + |
| 79 | + public StreamingKeyedByPartition( |
| 80 | + DataflowRunner runner, |
| 81 | + KafkaIO.Read.KeyedByPartition<K, V> original, |
| 82 | + PCollection<KV<Integer, V>> output) { |
| 83 | + this.runner = runner; |
| 84 | + this.originalTransform = original; |
| 85 | + this.originalOutput = output; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public PCollection<KV<Integer, V>> expand(PCollection<KafkaRecord<K, V>> input) { |
| 90 | + // Record the output PCollection of the original transform since the new output will be |
| 91 | + // replaced by the original one when the replacement transform is wired to other nodes in the |
| 92 | + // graph, although the old and the new outputs are effectively the same. |
| 93 | + runner.maybeRecordPCollectionPreservedKeys(originalOutput); |
| 94 | + System.out.println("StreamingKeyedByPartition override"); |
| 95 | + return input.apply(originalTransform); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments