简体   繁体   English

Kafka生产者跳过消息

[英]Kafka producer skipping messages

I am trying to write data from a file to a kafka topic. 我正在尝试将数据从文件写入kafka主题。 My code looks like this: 我的代码如下所示:

 Properties properties = new Properties();
    properties.put("bootstrap.servers", <bootstrapServers>);
    properties.put("key.serializer", StringSerializer.class.getCanonicalName());
    properties.put("value.serializer", StringSerializer.class.getCanonicalName());
    properties.put("retries",100);
    properties.put("linger.ms",5);
    properties.put("acks", "all");

    KafkaProducer<Object, String> producer = new KafkaProducer<>(properties);

    try (BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"))) {
        String line;
        int count = 0;
        while ((line = bf.readLine()) != null) {
            count++;
            producer.send(new ProducerRecord<>(topicName, line));
        }
  producer.flush();
        Logger.log("Done producing data messages. Total no of records produced:" + count);
    } catch (InterruptedException | ExecutionException | IOException e) {
        Throwables.propagate(e);
    } finally {
        producer.close();
    }

The size of the data is above 1 million records. 数据大小超过一百万条记录。

When I check the offset of data on brokers using following command, there are only half of the messages (around 5,00,000) are written on the topic: 当我使用以下命令检查代理上的数据偏移时,只有一半的消息(大约5,00,000)写在该主题上:

./kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list <broker_list> --time -1 --topic <topic_name>

Output of the above command: 上面命令的输出:

topic_name:1:292954
topic_name:0:296787

What changes should I do in approach to make sure that all the are written on the topic. 我应该在方法上进行哪些更改以确保所有内容都写在该主题上。

The send message is asynchronous. 发送消息是异步的。 You may be checking the offsets before all the messages are processed. 在处理所有消息之前,您可能正在检查偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM