我正面临
从Scala编写的命令行运行以下kafka消费者API时出现org.apache.kafka.common.errors.InvalidGroupIdException:要使用 组管理或偏移提交API,您必须提供有效的 使用者配置中的group.id。
错误。可能是什么问题?
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()
}
}
答案 0 :(得分:0)
在source code中,我看到InvalidGroupIdException
为groupId
时抛出了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")