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屬性是自己封裝的,所以要設置什么屬性就自己通過索引器設置。

 

大家千萬不要想着偷懶吶。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM