InvalidGroupIdException:即使在设置了group.id之后,在Kafka scala使用者程序中

时间:2019-05-18 21:28:37

标签: scala apache-kafka kafka-consumer-api

我正面临

  

org.apache.kafka.common.errors.InvalidGroupIdException:要使用   组管理或偏移提交API,您必须提供有效的   使用者配置中的group.id。

从Scala编写的命令行运行以下kafka消费者API时出现

错误。可能是什么问题?

    object KafkaAggregateConsumerApp extends App{
  try {
    val properties: Properties = new Properties()
    properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "0:9092") 
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer")
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.IntegerDeserializer")
    properties.put("group.id", "console-consumer-myapp")

    val consumerApp = new KafkaConsumer[String, Int](properties)
    consumerApp.subscribe(Pattern.compile("kafkaaggregationsource1"))

    try {
      while (true) {
        val consumerRecord: ConsumerRecords[String, Int] = consumerApp.poll(Duration.ofMinutes(10))
        consumerRecord.forEach((each) => println(each.key() + " " + each.value()))
      }
    } finally {
      consumerApp.close()
    }
  }
  catch{
    case e: Exception => e.printStackTrace()
  }
}

1 个答案:

答案 0 :(得分:0)

source code中,我看到InvalidGroupIdExceptiongroupId时抛出了null

private void maybeThrowInvalidGroupIdException() {
    if (groupId == null)
        throw new InvalidGroupIdException("To use the group management or offset commit APIs, you must " +
                "provide a valid " + ConsumerConfig.GROUP_ID_CONFIG + " in the consumer configuration.");
}

我建议使用ConsumerConfig.GROUP_ID_CONFIG而非"group.id"设置组ID:

properties.put(ConsumerConfig.GROUP_ID_CONFIG, "console-consumer-myapp")