Alpakka kafka consumer processes records until encounters record which it fails to deserialize and silently dies without leaving error message. How to force it to report error message?
1 Answers
According to the documentation,
When a stream fails, library internals will handle all underlying resources.
(de)serialization
If reading from Kafka failure is caused by other reasons, like deserialization problems, then the stage will fail immediately. If you expect such cases, consider consuming raw byte arrays and deserializing in a subsequent map stage where you can use supervision to skip failed elements.
It is recommended to (de)serialize messages with the use of byte arrays as value and do the (de)serialization in a map operation in the Akka Stream instead of implementing it directly in Kafka (de)serializers.
Example with Spray JSON (This example uses resuming to react on data which can’t be parsed correctly and ignores faulty elements):
import spray.json._
final case class SampleData(name: String, value: Int)
object SampleDataSprayProtocol extends DefaultJsonProtocol {
implicit val sampleDataProtocol: RootJsonFormat[SampleData] = jsonFormat2(SampleData)
}
import SampleDataSprayProtocol._
val resumeOnParsingException = ActorAttributes.withSupervisionStrategy {
new akka.japi.function.Function[Throwable, Supervision.Directive] {
override def apply(t: Throwable): Supervision.Directive = t match {
case _: spray.json.JsonParser.ParsingException => Supervision.Resume
case _ => Supervision.stop
}
}
}
val consumer = Consumer
.plainSource(consumerSettings, Subscriptions.topics(topic))
.map { consumerRecord =>
val value = consumerRecord.value()
val sampleData = value.parseJson.convertTo[SampleData]
sampleData
}
.withAttributes(resumeOnParsingException)
.toMat(Sink.seq)(Keep.both)
.mapMaterializedValue(DrainingControl.apply)
.run()

- 36,235
- 20
- 134
- 156
-
Documentation definitely says what it says. However code does what it does:"consumer processes records until encounters record which it fails to deserialize and silently dies without leaving error message". I was wondering if there is a way to specify concrete deserializers (not generic ByteArrayDeserializer) and see exceptions instead of nothing. – Jeriho Aug 20 '19 at 13:07