当从终端启动Kafka Consumer时,可以设置--from-beginning
,以便消费者从Kafka队列的开头读取消息。
~/kafka/bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic TutorialTopic --from-beginning
但是,如何在Scala(或Java)中设置此参数?这是我的样本ConsumerConfig
:
import kafka.consumer.ConsumerConfig
def createConsumerConfig(zookeeper: String, groupId: String): ConsumerConfig = {
val props = new Properties()
props.put("zookeeper.connect", zookeeper);
props.put("group.id", groupId);
props.put("auto.offset.reset", "largest");
props.put("zookeeper.session.timeout.ms", "400");
props.put("zookeeper.sync.time.ms", "200");
props.put("auto.commit.interval.ms", "1000");
val config = new ConsumerConfig(props)
config
}