storm kafkaSpout 踩坑問題記錄! offset問題!

來源:互聯網
上載者:User

標籤:system   using   指定   官網   keep   fse   metrics   effect   fine   

整合kafka和storm例子網上很多,自行尋找

 

問題描述:

  kafka是之前早就搭建好的,建立的storm叢集要消費kafka的主題,由於kafka中已經記錄了很多訊息,storm消費時從最開始消費

 

問題解決:

  下面是摘自官網的一段話:

How KafkaSpout stores offsets of a Kafka topic and recovers in case of failures

As shown in the above KafkaConfig properties, you can control from where in the Kafka topic the spout begins to read by setting KafkaConfig.startOffsetTime as follows:

  1. kafka.api.OffsetRequest.EarliestTime(): read from the beginning of the topic (i.e. from the oldest messages onwards)
  2. kafka.api.OffsetRequest.LatestTime(): read from the end of the topic (i.e. any new messsages that are being written to the topic)
  3. A Unix timestamp aka seconds since the epoch (e.g. via System.currentTimeMillis()): see How do I accurately get offsets of messages for a certain timestamp using OffsetRequest? in the Kafka FAQ

As the topology runs the Kafka spout keeps track of the offsets it has read and emitted by storing state information under the ZooKeeper path SpoutConfig.zkRoot+ "/" + SpoutConfig.id. In the case of failures it recovers from the last written offset in ZooKeeper.

Important: When re-deploying a topology make sure that the settings for SpoutConfig.zkRoot and SpoutConfig.id were not modified, otherwise the spout will not be able to read its previous consumer state information (i.e. the offsets) from ZooKeeper -- which may lead to unexpected behavior and/or to data loss, depending on your use case.

This means that when a topology has run once the setting KafkaConfig.startOffsetTime will not have an effect for subsequent runs of the topology because now the topology will rely on the consumer state information (offsets) in ZooKeeper to determine from where it should begin (more precisely: resume) reading. If you want to force the spout to ignore any consumer state information stored in ZooKeeper, then you should set the parameter KafkaConfig.ignoreZkOffsets to true. If true, the spout will always begin reading from the offset defined by KafkaConfig.startOffsetTime as described above.

 

  這段話的包含的內容大概有,通過SpoutConfig對象的startOffsetTime欄位設定消費進度,預設值是kafka.api.OffsetRequest.EarliestTime(),也就是從最早的訊息開始消費,如果想從最新的訊息開始消費需要手動設定成kafka.api.OffsetRequest.LatestTime()。另外還有一個問題是,這個欄位只會在第一次消費訊息時起作用,之後消費的offset是從zookeeper中記錄的offset開始的

  如果想要當前的topology的消費進度接著上一個topology的消費進度繼續消費,那麼不要修改SpoutConfig對象的id。換言之,如果你第一次已經從最早的訊息開始消費了,那麼如果不換id的話,它就要從最早的訊息一直消費到最新的訊息,這個時候如果想要跳過中間的訊息直接從最新的訊息開始消費,那麼修改SpoutConfig對象的id就可以了

 

  下面是SpoutConfig對象的一些欄位的含義,其實是繼承的KafkaConfig的欄位,可看源碼

  public int fetchSizeBytes = 1024 * 1024; //發給Kafka的每個FetchRequest中,用此指定想要的response中總的訊息的大小    public int socketTimeoutMs = 10000;//與Kafka broker的串連的socket逾時時間    public int fetchMaxWait = 10000;   //當伺服器沒有新訊息時,消費者會等待這些時間    public int bufferSizeBytes = 1024 * 1024;//SimpleConsumer所使用的SocketChannel的讀緩衝區大小    public MultiScheme scheme = new RawMultiScheme();//從Kafka中取出的byte[],該如何還原序列化    public boolean forceFromStart = false;//是否強制從Kafka中offset最小的開始讀起    public long startOffsetTime = kafka.api.OffsetRequest.EarliestTime();//從何時的offset時間開始讀,預設為最舊的offset    public long maxOffsetBehind = Long.MAX_VALUE;//KafkaSpout讀取的進度與目標進度相差多少,相差太多,Spout會丟棄中間的訊息 
   public boolean useStartOffsetTimeIfOffsetOutOfRange = true;//如果所請求的offset對應的訊息在Kafka中不存在,是否使用startOffsetTime
   public int metricsTimeBucketSizeInSecs = 60;//多長時間統計一次metrics

 

storm kafkaSpout 踩坑問題記錄! offset問題!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.