【问题标题】:Commit cannot be completed due to rebalance由于重新平衡,提交无法完成
【发布时间】:2017-08-19 23:47:28
【问题描述】:

我的Kafka消费者代码如下图,我只有一个消费者!

Properties consumerConfig = new Properties();
        consumerConfig.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "sandbox.hortonworks.com:6667");
        consumerConfig.put(ConsumerConfig.GROUP_ID_CONFIG, "my-group");
        consumerConfig.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        consumerConfig.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
                "org.apache.kafka.common.serialization.StringDeserializer");
        consumerConfig.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
                "org.apache.kafka.common.serialization.StringDeserializer");
        @SuppressWarnings("resource")
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(consumerConfig);
        TestConsumerRebalanceListener rebalanceListener = new TestConsumerRebalanceListener();
        consumer.subscribe(Collections.singletonList("test1"), rebalanceListener);
        HDFSAppendTrial example = new HDFSAppendTrial();
        String coreSite = "/usr/hdp/2.6.0.3-8/hadoop/etc/hadoop/core-site.xml";
        String hdfsSite = "/usr/hdp/2.6.0.3-8/hadoop/etc/hadoop/hdfs-site.xml";
        String hdfsFilePath = "/appendTo/Trial.csv";

        while (true) {
            ConsumerRecords<String, String> records = consumer.poll(999999999);
            for (ConsumerRecord<String, String> record : records) {
                FileSystem fileSystem = example.configureFileSystem(coreSite, hdfsSite);
                String res = example.appendToFile(fileSystem, record.value(), hdfsFilePath);
                System.out.printf("%s\n", record.value());
                if (res.equalsIgnoreCase( "success")) {
                    System.out.println("Successfully appended to file");
                }
                else
                    System.out.println("couldn't append to file");
                example.closeFileSystem(fileSystem);

            }
            consumer.commitSync();


        }

流式传输一些记录后出现以下错误

如何解决上述问题并解释问题的原因,如何克服!有人可以帮我编写一个有更多要求的消费者代码吗?

在此先感谢可以帮助我的人以及至少尝试过的人

【问题讨论】:

    标签: java hadoop apache-kafka kafka-consumer-api


    【解决方案1】:

    consumerConfig.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
    consumerConfig.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"True");
    consumerConfig.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG,"99998");
    consumerConfig.put(ConsumerConfig.HEARTBEAT_INTERVAL_MS_CONFIG,"10000");
    consumerConfig.put(ConsumerConfig.REQUEST_TIMEOUT_MS_CONFIG,"99999");
    

    我可以通过在属性对象 consumerConfig

    中添加上述属性来解决此问题

    【讨论】:

      猜你喜欢
      • 2017-02-06
      • 2018-07-04
      • 2019-01-08
      • 1970-01-01
      • 2018-01-15
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 2022-06-11
      相关资源
      最近更新 更多