簡體   English   中英

在不指定分區的情況下運行 kafka 消費者

[英]Run kafka consumer without specifying partition

我最近在學習Kafka,除非我指定了--parititon 0參數,否則我的消費者無法消費任何記錄。 換句話說,我不能使用以下記錄:

kafka-console-consumer --bootstrap-server 127.0.0.10:9092 --topic first-topic 

但工作原理如下:

kafka-console-consumer --bootstrap-server 127.0.0.10:9092 --topic first-topic --partition 0

主要問題是,當我轉到 java 代碼時,我的KafkaConsumer類無法獲取記錄,我需要知道如何在 java KafkaConsumer指定partition號?!

我目前的java代碼是:


public class ConsumerDemo {

    public static void main(String[] args) {

        Logger logger = LoggerFactory.getLogger((ConsumerDemo.class.getName()));

        String bootstrapServer = "127.0.0.10:9092";
        String groupId = "my-kafka-java-app";
        String topic = "first-topic";

        // create consumer configs
        Properties properties = new Properties();
        properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer);
        //properties.setProperty(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG, partition);
        properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
        properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, groupId);
        properties.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        // create consumer
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(properties);

        // subscribe consumer to our topic
        consumer.subscribe(Collections.singleton(topic)); //means subscribtion to one topic

        // poll for new data
        while(true){
            //consumer.poll(100); old way
            ConsumerRecords<String, String> records =
                    consumer.poll(Duration.ofMillis(100));

            for (ConsumerRecord<String, String> record : records){
                logger.info("Key: " + record.key() + ", Value: "+ record.value() );
                logger.info("Partition: " + record.partition() + ", Offset: "+ record.offset());
            }

        }

    }
}

經過大量檢查,我的解決方案是使用consumer.assignconsumer.seek而不是使用consumer.subscribe並且沒有指定 groupId

java代碼將是:

        // create consumer
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(properties);

        // subscribe consumer to our topic
        //consumer.subscribe(Collections.singleton(topic)); //means subscription to one topic
        // using assign and Seek, are mostly used to replay data or fetch a specific msg
        TopicPartition  partitionToReadFrom = new TopicPartition(topic, 0);
        long offsetToReadFrom = 15L;
        // assign
        consumer.assign(Arrays.asList(partitionToReadFrom));

        // seek: for a specific offset to read from
        consumer.seek(partitionToReadFrom, offsetToReadFrom);

你的做法是正確的。 訂閱主題時無需指定分區。 也許您的消費者組已經消費了主題中的所有消息並提交了最新的偏移量。

確保在運行應用程序或創建新的消費者組以從頭開始使用時正在生成新消息(如果將ConsumerConfig.AUTO_OFFSET_RESET_CONFIG設置為“最早”)

顧名思義, ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG屬性旨在配置分區分配策略,而不是按照命令行的指示設置固定分區。

使用的默認策略是RangeAssignor ,它可以更改,例如更改為StickyAssignor ,如下所示:

properties.setProperty(ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG,StickyAssignor.class.getName());

你可以閱讀更多關於Kafka Client Side Assignment Proposal 的信息

暫無
暫無

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

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