c# rdkafka 设置偏移量(offset)

参考资料:

  librdkafka: 如何设置Kafka消费者订阅消息的起始偏移位置

领导要求kafka消费者端消费最新的数据。

不知道怎么设置偏移量,查了资料。

用惯了封装好的东西,都不知道怎么设置了,看了源代码以及参考资料,才知道自己动手写。

                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属性是自己封装的,所以要设置什么属性就自己通过索引器设置。

 

大家千万不要想着偷懒呐。

猜你喜欢

转载自www.cnblogs.com/dawenyang/p/12172620.html