kafka流API不会将消息生成新主题

时间:2018-11-04 09:30:13

标签: java apache-kafka apache-kafka-streams

我正在运行https://kafka.apache.org/20/documentation/streams/tutorial的流api示例中给出的Pipe.java。我正在按照链接中给出的进行操作。 Kafka代理也在本地主机和端口9092上运行。

我创建了主题streams-plaintext-input和streams-pipe-output。当我使用kafka-console-producer.sh实用程序将一些消息发送到主题流-纯文本输入时,我看不到任何消息进入主题流-管道输出。

有人可以告诉我我在做什么错吗?

package myapps;

import java.util.Properties;
import java.util.concurrent.CountDownLatch;

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.kstream.KStream;

public class MyPipe {

    public static void main(String[] args) {
        Properties props = new Properties();
        props.put(StreamsConfig.APPLICATION_ID_CONFIG, "mystreams-pipe");
        props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
        props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
        final StreamsBuilder builder = new StreamsBuilder();
        builder.stream("streams-plaintext-input").to("streams-pipe-output");
        final Topology topology = builder.build();
        System.out.println(topology.describe());
        final KafkaStreams streams = new KafkaStreams(topology, props);
        final CountDownLatch latch = new CountDownLatch(1);

        // attach shutdown handler to catch control-c
        Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
            @Override
            public void run() {
                streams.close();
                System.out.println("closing");
                latch.countDown();
            }
        });

        try {
            System.out.println("starting stream application.");
            streams.start();

            System.out.println("started");
            latch.await();
        } catch (Throwable e) {
            e.printStackTrace();
            System.exit(1);
        }

        System.out.println("exiting");
        System.exit(0);
    }

}

0 个答案:

没有答案