|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package io.floodplain.reactive.source.topology; |
| 20 | + |
| 21 | +import io.floodplain.reactive.source.topology.api.TopologyPipeComponent; |
| 22 | +import io.floodplain.streams.api.TopologyContext; |
| 23 | +import io.floodplain.streams.remotejoin.KeyProcessor; |
| 24 | +import io.floodplain.streams.remotejoin.ReplicationTopologyParser; |
| 25 | +import io.floodplain.streams.remotejoin.TopologyConstructor; |
| 26 | +import org.apache.kafka.streams.Topology; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +import java.util.Stack; |
| 31 | +import java.util.function.Function; |
| 32 | + |
| 33 | +public class KeyTransformer implements TopologyPipeComponent { |
| 34 | + |
| 35 | + private boolean materialize; |
| 36 | + |
| 37 | + // private final boolean fromEmpty; |
| 38 | + private final Function<String,String> keyTransformer; |
| 39 | + |
| 40 | + |
| 41 | + private final static Logger logger = LoggerFactory.getLogger(KeyTransformer.class); |
| 42 | + public KeyTransformer(Function<String, String> keyTransformer) { |
| 43 | + this.keyTransformer = keyTransformer; |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + @Override |
| 48 | + public void addToTopology(Stack<String> transformerNames, int currentPipeId, Topology topology, |
| 49 | + TopologyContext topologyContext, TopologyConstructor topologyConstructor) { |
| 50 | + KeyProcessor fp = new KeyProcessor(this.keyTransformer); |
| 51 | + String name = topologyContext.qualifiedName("keyTransform", transformerNames.size(), currentPipeId); |
| 52 | + logger.info("Adding processor: {} to parent: {} hash: {}", name, transformerNames, transformerNames.hashCode()); |
| 53 | + if (this.materialize) { |
| 54 | + topology.addProcessor(name + "_prematerialize", () -> fp, transformerNames.peek()); |
| 55 | + ReplicationTopologyParser.addMaterializeStore(topology, topologyContext, topologyConstructor, name, name + "_prematerialize"); |
| 56 | + } else { |
| 57 | + topology.addProcessor(name, () -> fp, transformerNames.peek()); |
| 58 | + } |
| 59 | + transformerNames.push(name); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public boolean materializeParent() { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public void setMaterialize() { |
| 69 | + this.materialize = true; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | +} |
0 commit comments