我正在尝试将文件中的数据写入kafka主题。我的代码如下所示:
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();
}
数据大小超过100万条记录。
当我使用以下命令检查代理的数据偏移时,只有一半的消息(大约5,00,000)写在主题上:
./kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list <broker_list> --time -1 --topic <topic_name>
输出上述命令:
topic_name:1:292954
topic_name:0:296787
我应该采取哪些措施来确保所有内容都写在主题上。
答案 0 :(得分:0)
发送消息是异步的。在处理所有消息之前,您可能正在检查偏移量。