卡夫卡"主题是空的"生成器代码编译成功后在UI中显示

时间:2018-06-14 04:07:53

标签: apache-kafka kafka-producer-api

我正在尝试编写并运行我自己的生产者代码,所以我已经在IntelliJ中编写了代码,并使用退出代码0完成了编译过程。但是在UI中没有显示消息,它显示了该主题是空的,同时如果我试图在docker中使用终端产生一些消息,那么主题表现正常。 请帮我解决这个问题。谢谢你提前。

enter image description here

代码:

import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.IntegerDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

public class KafkaProducer {
    public static void main(String[] args) {
        Properties properties = new Properties();

        // kafka bootstrap server
        properties.setProperty("bootstrap.servers", "127.0.0.1:9092");
        properties.setProperty("key.serializer", StringSerializer.class.getName());
        properties.setProperty("value.serializer", StringSerializer.class.getName());
        // producer acks
        properties.setProperty("acks", "1");
        properties.setProperty("retries", "3");
        properties.setProperty("linger.ms", "1");

        Producer<String, String> producer = new org.apache.kafka.clients.producer.KafkaProducer<String, String>(properties);


        for (int key=0; key < 10; key++){
            ProducerRecord<String, String> producerRecord =
        new ProducerRecord<String, String>("second_topic", Integer.toString(key), "message that has key: " + Integer.toString(key));
            producer.send(producerRecord);
        }
        producer.close();
    }
}

的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.dataoverflow.kafka</groupId>
    <artifactId>kafka-dataoverflow</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>1.1.0</version>
        </dependency>
    </dependencies>


</project>

更新!! 我试过在我的代码中添加异步回调函数,但是没有将消息推送到Broker中。我该怎么办?

package com.dataoverflow.kafka;

import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.IntegerDeserializer;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.clients.producer.*;

import java.util.Properties;

public class KafkaProducer {
    public static void main(String[] args) {
        Properties properties = new Properties();

        // kafka bootstrap server
        properties.setProperty("bootstrap.servers", "127.0.0.1:9092");
        properties.setProperty("broker.list", "127.0.0.1:9092");
        properties.setProperty("key.serializer", StringSerializer.class.getName());
        properties.setProperty("value.serializer", StringSerializer.class.getName());
        // producer acks
        properties.setProperty("acks", "all");
        properties.setProperty("timeout.ms", "20000");

        properties.setProperty("retries", "3");
        properties.setProperty("linger.ms", "10");
        //Specify buffer size in config
        properties.put("batch.size", 16384); 
        properties.put("buffer.memory", 33554432);

        Producer<String, String> producer = new org.apache.kafka.clients.producer.KafkaProducer<String, String>(properties);


        for (int key=0; key < 10; key++){
            ProducerRecord<String, String> record =
        new ProducerRecord<String, String>("second_topic", Integer.toString(key), "message that has key: " + Integer.toString(key));
            producer.send(record, new MyProducerCallback());
            System.out.println("AsynchronousProducer call completed");
        }
        producer.close();
    }
}

class MyProducerCallback implements Callback{


    public  void onCompletion(RecordMetadata recordMetadata, Exception e) {
        if (e != null) {
            System.out.println("AsynchronousProducer failed with an exception");
            System.out.println(e.getStackTrace());
        }
        else
            System.out.println("AsynchronousProducer call Success:");
    }
}

输出:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
AsynchronousProducer failed with an exception
AsynchronousProducer call completed
AsynchronousProducer failed with an exception
AsynchronousProducer call completed
AsynchronousProducer failed with an exception
AsynchronousProducer call completed
AsynchronousProducer failed with an exception
AsynchronousProducer call completed
AsynchronousProducer failed with an exception
AsynchronousProducer call completed

1 个答案:

答案 0 :(得分:0)

我已成功修复它。 Here是主题。