参考资料:this
librdkafka: 如何设置Kafka消费者订阅消息的起始偏移位置spa
领导要求kafka消费者端消费最新的数据。.net
不知道怎么设置偏移量,查了资料。code
用惯了封装好的东西,都不知道怎么设置了,看了源代码以及参考资料,才知道本身动手写。blog
var config = new Config { GroupId = UtilityConfig._groupId, EnableAutoCommit = true, StatisticsInterval = TimeSpan.FromSeconds(6), ["auto.offset.reset"] = "lastest" //设置获取最新的消息,要设置什么去官方文档找,直接经过key-value方式设置便可 }; using (var consumer = new EventConsumer(config, UtilityConfig._brokerlist)) { //代码逻辑
config部分源码索引
public string this[string name] { set { this.handle.Set(name, value); } get { return this.handle.Get(name); } } public string GroupId { set { this["group.id"] = value; } get { return this["group.id"]; } } public bool EnableAutoCommit { set { this["enable.auto.commit"] = value ? "true" : "false"; } get { return this["enable.auto.commit"] == "true"; } }
从源码能够看出config里面的EnableAutoCommit属性是本身封装的,因此要设置什么属性就本身经过索引器设置。文档
你们千万不要想着偷懒呐。get