簡體   English   中英

從 Kafka 主題獲取多條消息

[英]Get multiple messages from Kafka topic

我的用例就像從生產者端它將一行數據(大約 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");
}

我做了一個示例,它總是收到一條消息並在控制台中打印。 請向我建議實現這一目標所需的任何配置更改。

請在下面找到源代碼。

@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;
}
}

使用以下命令啟動生產者

./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 文件內容

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

輸出如下所示。

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

提前致謝。

您應該配置一個Batch Listener ,然后您可以設置max.poll.records屬性來指定您的批量大小。

請注意,將此值設置得太低可能會降低整體性能,因為您需要對代理進行更多輪詢以獲取相同數量的記錄。

這里提供的要求非常高。 如果您能從業務邏輯實現的角度告訴我們您的實際需求,那就太好了。 您的低級編碼和其他配置參數可以根據您的要求進行微調。

為了給您一個建議,如果您只想將消息(5)一個一個地控制台出來,那么您可以通過 max.poll.records = 5 一次輪詢 5 條記錄並遍歷消費者記錄。 這很簡單。

Venkata Krishna,這只能通過使用在生產者端實現的鍵控機制來完成。 從源系統生成的每一行輸入都必須具有與之關聯的唯一鍵,並使用更好的分區策略將事件發布到具有唯一鍵的特定分區中。 根據唯一鍵,您可以使用可用的全狀態操作之一或使用窗口聚合之一對它們進行分組。 因此,如果您使用窗口,您可以實現類似的東西,在給定的持續時間內對每個鍵接收的事件進行分組,並將它們全部發布到一個中間主題,並讓您的消費者輪詢 <> 個記錄並遍歷消費者記錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM