KafkaStreams在拓扑中添加多个处理器不起作用

时间:2019-05-26 05:46:46

标签: apache-kafka apache-kafka-streams

我在拓扑中添加了多个处理器。只有1个处理器在工作。我能够接收来自topic1,topic2的消息,并使用Processor1处理它们,并将产品输出到topic3-sink。我的代码未调用Processor2。

@Component
public class CustomTopology {
    @PostConstruct
    public void createTopology() {
        Topology topology = new Topology();
        topology.addSource("source", new KeyDeserializer(), new ValueDeserializer(), "topic1", "topic2");
        topology.addProcessor("processor1", new CustomProcessorSupplier1(), "source");
        topology.addProcessor("processor2", new CustomProcessorSupplier2(), "processor1");
        topology.addSink("sink", "topic3-sink", "source", "processor2");
        Properties p = new Properties();
        p.put(APPLICATION_ID_CONFIG, "stream1");
        p.put(BOOTSTRAP_SERVERS_CONFIG, KafkaUtil.getBootStrapServers());
        p.put(DEFAULT_KEY_SERDE_CLASS_CONFIG, KeySerde.class);
        p.put(DEFAULT_VALUE_SERDE_CLASS_CONFIG, ValueSerde.class);
        KafkaStreams streams = new KafkaStreams(topology, p);
        streams.start();
    }
}

class CustomProcessorSupplier1 implements ProcessorSupplier<Integer, Message> {

    @Override
    public Processor<Integer, Message> get() {
        Processor<Integer, Message> processor = new Processor<Integer, Message>() {
            ...

            @Override
            public void process(Integer key, Message value) {
                value.setName(value.getName() + "ProcessB");
            }

            ...
        };
        return processor;
    }
}

class CustomProcessorSupplier2 implements ProcessorSupplier<Integer, Message> {

    @Override
    public Processor<Integer, Message> get() {
        Processor<Integer, Message> processor = new Processor<Integer, Message>() {
           ...

            @Override
            public void process(Integer key, Message value) {
                value.setName(value.getName() + "ansalProcess");
            }

            ...
        };
        return processor;
    }
}

1 个答案:

答案 0 :(得分:1)

要在处理机中转发记录,您必须致电ProcessorContext::forward。 此方法已重载。您可以将所有邮件转发到所有后续节点,但也可以选择要将邮件转发到的节点子集。

您必须添加Processor的抵押(CustomProcessorSupplier1CustomProcessorSupplier1context().forward(someKey, someValue);

您的处理器不会向topic3-sink发送消息。由于父节点的一个节点是SourceNode(输入主题),因此出现消息:

topology.addSink("sink", "topic3-sink", "source", "processor2");