简体   繁体   English

从 Kafka 主题获取多条消息

[英]Get multiple messages from Kafka topic

My use case is like from the producer side it will post one line of data(around 100 bytes) as one message to kafka topic, from consumer side I want to consume 5 messages at a time and give it to my consumer logic.我的用例就像从生产者端它将一行数据(大约 100 个字节)作为一条消息发布到 kafka 主题,从消费者端我想一次消费 5 条消息并将其提供给我的消费者逻辑。

@KafkaListener(id = "listener-batch", topics = "test", containerFactory = "concurrentKafkaListenerContainerFactory")
public void receive(@Payload List<String> messages,
                    @Header(KafkaHeaders.RECEIVED_PARTITION_ID) List<Integer> partitions,
                    @Header(KafkaHeaders.OFFSET) List<Long> offsets) {

    System.out.println("- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -");
    System.out.println("Starting the process to recieve batch messages :: " + messages);
    for (int i = 0; i < messages.size(); i++) {
        System.out.println("received message= "+ messages.get(i) +" with partition-offset= " + partitions.get(i) + "-" + offsets.get(i));
    }
    System.out.println("all the batch messages are consumed");
}

I did a sample example, it always gets one message and printing in the console.我做了一个示例,它总是收到一条消息并在控制台中打印。 Please suggest me any configuration changes required to achive this one.请向我建议实现这一目标所需的任何配置更改。

Please find the source code below.请在下面找到源代码。

@EnableKafka
@Configuration
public class KafkaConfig {

@Bean
public ConsumerFactory<String, String> consumerFactory(){
    Map<String, Object> config = new HashMap<>();
    config.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    config.put(ConsumerConfig.GROUP_ID_CONFIG, "batch");
    config.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    config.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,StringDeserializer.class);
    config.put(ConsumerConfig.MAX_POLL_RECORDS_CONFIG, "5");
    return new DefaultKafkaConsumerFactory<>(config);
}

@Bean
public ConcurrentKafkaListenerContainerFactory concurrentKafkaListenerContainerFactory() {
    ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
    factory.setConsumerFactory(consumerFactory());
    factory.setBatchListener(true);
    return factory;
}
}

Starting producer using below command使用以下命令启动生产者

./kafka-producer-perf-test --num-records 500 --topic test --throughput 10 --payload-file test.csv --producer-props bootstrap.servers=localhost:9092 key.serializer=org.apache.kafka.common.serialization.StringSerializer value.serializer=org.apache.kafka.common.serialization.StringSerializer ./kafka-producer-perf-test --num-records 500 --topic test --throughput 10 --payload-file test.csv --producer-props bootstrap.servers=localhost:9092 key.serializer=org.apache .kafka.common.serialization.StringSerializer value.serializer=org.apache.kafka.common.serialization.StringSerializer

test.csv file contents test.csv 文件内容

Batch-1 message
Batch-2 message
Batch-3 message
Batch-4 message
Batch-5 message
Batch-6 message
Batch-7 message
Batch-8 message
Batch-9 message
Batch-10 message
Batch-11 message

Output is showing like below.输出如下所示。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Starting the process to recieve batch messages :: [Batch-3 message]
received message= Batch-3 message with partition-offset= 0-839501
all the batch messages are consumed
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Starting the process to recieve batch messages :: [Batch-7 message]
received message= Batch-7 message with partition-offset= 0-839502
all the batch messages are consumed
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Starting the process to recieve batch messages :: [Batch-3 message]
received message= Batch-3 message with partition-offset= 0-839503
all the batch messages are consumed
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Starting the process to recieve batch messages :: [Batch-1 message]
received message= Batch-1 message with partition-offset= 0-839504
all the batch messages are consumed
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Thanks in advance.提前致谢。

You should configure a Batch Listener , then you can set the max.poll.records property to specify your batch size.您应该配置一个Batch Listener ,然后您可以设置max.poll.records属性来指定您的批量大小。

Note that setting this value too low might decrease overall performance, since you'll need to make more polls to the broker to fetch the same amount of records.请注意,将此值设置得太低可能会降低整体性能,因为您需要对代理进行更多轮询以获取相同数量的记录。

The requirement provided here is on the very high level.这里提供的要求非常高。 It would be great if you can tell us your actual requirement from the business logic implementation perspective.如果您能从业务逻辑实现的角度告诉我们您的实际需求,那就太好了。 You low level coding and other configuration parameters can be fine tuned based your requirement.您的低级编码和其他配置参数可以根据您的要求进行微调。

For the sake of giving you a suggestion, if you want to just console out messages (5) one by one, then you can poll the 5 records at a time through max.poll.records = 5 and iterate over the consumer records through.为了给您一个建议,如果您只想将消息(5)一个一个地控制台出来,那么您可以通过 max.poll.records = 5 一次轮询 5 条记录并遍历消费者记录。 It's pretty simple.这很简单。

Venkata Krishna, This can be done only via using a keying mechanism implemented in the producer side. Venkata Krishna,这只能通过使用在生产者端实现的键控机制来完成。 Each line of input produced from the source system, must be having a unique key associated with it and using a better partitioning strategy to publish the events into a specific partition having a unique key.从源系统生成的每一行输入都必须具有与之关联的唯一键,并使用更好的分区策略将事件发布到具有唯一键的特定分区中。 Based on the unique key, you can group them using one of the available statefull operations or using the one of the Windowing Aggregations.根据唯一键,您可以使用可用的全状态操作之一或使用窗口聚合之一对它们进行分组。 So if you use a window, you can implement something like, group number of events received per key for a given duration and publish them all in a batch to an intermediate topic, and make your consumer to poll <> number records and iterate through the consumer records.因此,如果您使用窗口,您可以实现类似的东西,在给定的持续时间内对每个键接收的事件进行分组,并将它们全部发布到一个中间主题,并让您的消费者轮询 <> 个记录并遍历消费者记录。

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

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