Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KafkaTestUtils.getRecords() returns only first sent record

Sending 4 records:

producer.send(new ProducerRecord<>("my-topic", 0, "key1", "value1"));
producer.send(new ProducerRecord<>("my-topic", 0, "key2", "value2"));
producer.send(new ProducerRecord<>("my-topic", 0, "key3", "value3"));
producer.send(new ProducerRecord<>("my-topic", 0, "key4", "value4"));

Using KafkaTestUtils.getRecords() get only first sent record;

final ConsumerRecords<String, OrderBookViewItem> records = KafkaTestUtils.getRecords(consumer, 10000);

Using consumer.poll() get all 4;

final Iterable<ConsumerRecord<String, OrderBookViewItem>> records = consumer.poll(1000).records("my-topic");

How to tune KafkaTestUtils to return all records or it is bug?

like image 382
GreenNun Avatar asked Dec 06 '25 22:12

GreenNun


2 Answers

It simply returns whatever Kafka gives us on a poll() the method doesn't know how many records to fetch.

You can call it multiple times, or you can try setting the consumer properties fetch.min.bytes and fetch.max.wait.ms so that the poll() waits for more data.

See the kafka documentation for more information about these properties.

like image 97
Gary Russell Avatar answered Dec 08 '25 15:12

Gary Russell


it works with 3 parameters (consumer, timeout, minRecords) for me:

final ConsumerRecords<String, OrderBookViewItem> records = KafkaTestUtils.getRecords(consumer, 10000, 4);

minRecords – wait until the timeout or at least this number of receords are

like image 31
Vitalij Reicherdt Avatar answered Dec 08 '25 15:12

Vitalij Reicherdt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!