Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache kafka Kafka消费java代码不工作_Apache Kafka_Kafka Consumer Api_Apache Zookeeper_Kafka Producer Api - Fatal编程技术网

Apache kafka Kafka消费java代码不工作

Apache kafka Kafka消费java代码不工作,apache-kafka,kafka-consumer-api,apache-zookeeper,kafka-producer-api,Apache Kafka,Kafka Consumer Api,Apache Zookeeper,Kafka Producer Api,我刚刚开始使用Kafka,我可以通过命令提示符生成和使用数据,甚至可以通过Java代码从远程服务器生成数据 但我正在尝试这个简单的消费Java代码,它不起作用 public class Simpleconsumer { private final ConsumerConnector consumer; private final String topic; public Simpleconsumer(String topic) { Properties

我刚刚开始使用Kafka,我可以通过命令提示符生成和使用数据,甚至可以通过Java代码从远程服务器生成数据

但我正在尝试这个简单的消费Java代码,它不起作用

public class Simpleconsumer {

    private final ConsumerConnector consumer;
    private final String topic;

    public Simpleconsumer(String topic) {
        Properties props = new Properties();
        props.put("zookeeper.connect", "127.0.0.1:2181");
        props.put("group.id", "topic1");
        props.put("auto.offset.reset", "smallest");

        consumer = Consumer.createJavaConsumerConnector(new ConsumerConfig(props));
        this.topic = topic;
    }

    public void testConsumer() {
        try{
        Map<String, Integer> topicCount = new HashMap<String, Integer>();
        topicCount.put(topic, 1);

        Map<String, List<KafkaStream<byte[], byte[]>>> consumerStreams = consumer.createMessageStreams(topicCount);
        List<KafkaStream<byte[], byte[]>> streams = consumerStreams.get(topic);
        System.out.println("start.......");
        for (final KafkaStream stream : streams) {
            ConsumerIterator<byte[], byte[]> it = stream.iterator();
            System.out.println("iterate.......");
            while (it.hasNext()) {
                System.out.println("Message from Single Topic: " + new String(it.next().message()));
            }
        }
        System.out.println("end.......");
        if (consumer != null) {
            consumer.shutdown();
        }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }

    public static void main(String[] args) {
        // String topic = args[0];
        Simpleconsumer simpleHLConsumer = new Simpleconsumer("topic1");
        simpleHLConsumer.testConsumer();
    }

}
没有错误,程序不会终止或给出任何输出

动物园管理员日志

2016-02-18 17:31:31,790 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@197] - Accepted socket connection from /127.0.0.1:33338
2016-02-18 17:31:31,793 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:ZooKeeperServer@868] - Client attempting to establish new session at /127.0.0.1:33338
2016-02-18 17:31:31,821 [myid:] - INFO  [SyncThread:0:ZooKeeperServer@617] - Established session 0x152f4265b0b0009 with negotiated timeout 6000 for client /127.0.0.1:33338
2016-02-18 17:31:31,891 [myid:] - INFO  [ProcessThread(sid:0 cport:-1)::PrepRequestProcessor@645] - Got user-level KeeperException when processing sessionid:0x152f4265b0b0009 type:create cxid:0x1 zxid:0x718 txntype:-1 reqpath:n/a Error Path:/consumers Error:KeeperErrorCode = NodeExists for /consumers
2016-02-18 17:31:31,892 [myid:] - INFO  [ProcessThread(sid:0 cport:-1)::PrepRequestProcessor@645] - Got user-level KeeperException when processing sessionid:0x152f4265b0b0009 type:create cxid:0x2 zxid:0x719 txntype:-1 reqpath:n/a Error Path:/consumers/artinew Error:KeeperErrorCode = NodeExists for /consumers/artinew
2016-02-18 17:31:31,892 [myid:] - INFO  [ProcessThread(sid:0 cport:-1)::PrepRequestProcessor@645] - Got user-level KeeperException when processing sessionid:0x152f4265b0b0009 type:create cxid:0x3 zxid:0x71a txntype:-1 reqpath:n/a Error Path:/consumers/artinew/ids Error:KeeperErrorCode = NodeExists for /consumers/artinew/ids
2016-02-18 17:31:32,000 [myid:] - INFO  [SessionTracker:ZooKeeperServer@347] - Expiring session 0x152f4265b0b0008, timeout of 6000ms exceeded
2016-02-18 17:31:32,000 [myid:] - INFO  [ProcessThread(sid:0 cport:-1)::PrepRequestProcessor@494] - Processed session termination for sessionid: 0x152f4265b0b0008
2016-02-18 17:31:32,002 [myid:] - INFO  [SyncThread:0:NIOServerCnxn@1007] - Closed socket connection for client /127.0.0.1:33337 which had sessionid 0x152f4265b0b0
我在无限循环的卡夫卡控制台中得到这个。请解释一下

[2016-02-17 20:50:08,594] INFO Closing socket connection to /xx.xx.xx.xx. (kafka.network.Processor)
[2016-02-17 20:50:08,174] INFO Closing socket connection to /xx.xx.xx.xx. (kafka.network.Processor)
[2016-02-17 20:50:08,385] INFO Closing socket connection to /xx.xx.xx.xx. (kafka.network.Processor)
[2016-02-17 20:50:08,760] INFO Closing socket connection to /xx.xx.xx.xx. (kafka.network.Processor)
我用以下方式创建了这个主题

bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 5 --topic topic1
我可以在命令提示符下使用

bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic topic1

无法理解问题所在。

请尝试使用localhost而不是代码中的127.0.0.1,以确保本地分辨率正常工作。

请投赞成票或至少有人回答。
bin/kafka-console-consumer.sh --zookeeper localhost:2181 --from-beginning --topic topic1