https://github.com/miguno/kafka-storm-starter provides such example code.
See, for instance, AvroDecoderBolt. From its javadocs:
This bolt expects incoming data in Avro-encoded binary format, serialized according to the Avro schema of T
. It will deserialize the incoming data into a T
pojo, and emit this pojo to downstream consumers. As such this bolt can be considered the Storm equivalent of Twitter Bijection's Injection.invert[T, Array[Byte]](bytes)
for Avro data.
where
T
: The type of the Avro record (e.g. a Tweet
) based on the underlying Avro schema being used. Must be a subclass of Avro's SpecificRecordBase
.
The key part of the code is (I collapsed the code into this snippet):
// With T <: SpecificRecordBase
implicit val specificAvroBinaryInjection: Injection[T, Array[Byte]] =
SpecificAvroCodecs.toBinary[T]
val bytes: Array[Byte] = ...; // the Avro-encoded data
val decodeTry: Try[T] = Injection.invert(bytes)
decodeTry match {
case Success(pojo) =>
System.out.println("Binary data decoded into pojo: " + pojo)
case Failure(e) => log.error("Could not decode binary data: " + Throwables.getStackTraceAsString(e))
}