kafka spout 无法连接到kafka主题

kafka spout can't connect to kafka topic

我正在尝试在 LocalCluster 对象上连接属于风暴拓扑 运行ning 的 KafkaSpout。我根据在 https://github.com/apache/storm/tree/master/external/storm-kafka.

上找到的文档编写了这段代码
 private static final String brokerZkStr = "localhost:2181";
private static final String topic = "/test-topic-multi";

public void startTopology()
{

    BrokerHosts hosts = new ZkHosts(brokerZkStr);
    SpoutConfig conf = new SpoutConfig(hosts, topic, "localhost:2181", UUID
            .randomUUID().toString());


    KafkaSpout kafkaSput = new KafkaSpout(conf);

    TopologyBuilder builder = new TopologyBuilder();

    builder.setSpout("kafka-spout", kafkaSput);

    Config topConfig = new Config();
    topConfig.setDebug(true);

    LocalCluster cluster = new LocalCluster();
    cluster.submitTopology("HelloStorm", topConfig , builder.createTopology());
}

我想在 localhost:2181 使用动物园管理员实例 运行ning 但是当尝试 运行 代码时我得到以下错误:

  java.lang.RuntimeException: java.lang.RuntimeException: java.lang.IllegalArgumentException: Invalid path string "/brokers/topics//test-topic-multi/partitions" caused by empty node name specified @16
at storm.kafka.DynamicBrokersReader.getBrokerInfo(DynamicBrokersReader.java:81)
at storm.kafka.trident.ZkBrokerReader.<init>(ZkBrokerReader.java:42)
at storm.kafka.KafkaUtils.makeBrokerReader(KafkaUtils.java:57)
at storm.kafka.KafkaSpout.open(KafkaSpout.java:87)

好像只是设置错误的问题,但我无法解决

PS Kafka 配置如下:1 个 Zookeeper 实例和 2 个代理 运行ning localhost:9092localhost:9093

您的 kafka 主题名称无效。您为什么要尝试连接到不存在的主题?

sql@injection:~$ kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic /test-topic
Error while executing topic command topic name /test-topic is illegal, contains a character other than ASCII alphanumerics, '.', '_' and '-'
kafka.common.InvalidTopicException: topic name /test-topic is illegal, contains a character other than ASCII alphanumerics, '.', '_' and '-'
    at kafka.common.Topic$.validate(Topic.scala:42)
    at kafka.admin.AdminUtils$.createOrUpdateTopicPartitionAssignmentPathInZK(AdminUtils.scala:181)
    at kafka.admin.AdminUtils$.createTopic(AdminUtils.scala:172)
    at kafka.admin.TopicCommand$.createTopic(TopicCommand.scala:93)
    at kafka.admin.TopicCommand$.main(TopicCommand.scala:55)
    at kafka.admin.TopicCommand.main(TopicCommand.scala)

您确定前斜杠属于主题名称吗?

我想我解决了。我只是弄乱了配置代码。正确的是:

private static final String topic = "test-topic-multi";
....
SpoutConfig conf = new SpoutConfig(hosts, topic, "/" + topic, UUID
            .randomUUID().toString());