Apache Kafka with High Level Consumer: Skip corrupted messages

馋奶兔 提交于 2019-12-05 08:08:47

问题


I'm facing an issue with high level kafka consumer (0.8.2.0) - after consuming some amount of data one of our consumers stops. After restart it consumes some messages and stops again with no error/exception or warning.

After some investigation I found that the problem with consumer was this exception:

ERROR c.u.u.e.impl.kafka.KafkaConsumer  - Error consuming message stream:
 kafka.message.InvalidMessageException: Message is corrupt (stored crc = 3801080313, computed crc = 2728178222)

Any ideas how can I simple skip such messages at all?


回答1:


So, answering my own question. After some debugging of Kafka Consumer, I found one possible solution:

  1. Create a subclass of kafka.consumer.ConsumerIterator
  2. Override makeNext-method. In this method catch InvalidMessageException and return some dummy-placeholder.
  3. In your while-loop you have to convert the kafka.consumer.ConsumerIterator to your implementation. Unfortunately all fields of kafka.consumer.ConsumerIterator are private, so you have to use reflection.

So this is the code example:

val skipIt = createKafkaSkippingIterator(ks.iterator())

while(skipIt.hasNext()) {
  val messageAndTopic = skipIt.next()

  if (messageNotCorrupt(messageAndTopic)) {
    consumeFn(messageAndTopic)
  }
}

The messageNotCorrupt-method simply checks if the argument is equal to the dummy-message.




回答2:


another solution, possibly easier, using Kafka 0.8.2 client.

try {
  val m = it.next()
  //...
} catch {
  case e: kafka.message.InvalidMessageException ⇒
    log.warn("Corrupted message. Skipping.", e)
    resetIteratorState(it)
}

//...

def resetIteratorState(it: ConsumerIterator[Array[Byte], Array[Byte]]): Unit = {
  val method = classOf[IteratorTemplate[_]].getDeclaredMethod("resetState")
  method.setAccessible(true)
  method.invoke(it)
}


来源:https://stackoverflow.com/questions/32904383/apache-kafka-with-high-level-consumer-skip-corrupted-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!