Akka Kafka Custom Serializer

时间:2017-07-31 16:13:40

标签: scala serialization apache-kafka akka

我正在使用Akka Kafka(Scala)并希望发送自定义对象。

class TweetsSerializer extends Serializer[Seq[MyCustomType]] {

override def configure(configs: util.Map[String, _], isKey: Boolean):   Unit = ???

override def serialize(topic: String, data: Seq[MyCustomType]): Array[Byte] = ???

override def close(): Unit = ???

}

如何正确编写自己的序列化程序?而且,我应该如何处理字段config

1 个答案:

答案 0 :(得分:0)

我会使用StringSerializer,我的意思是,在生成它们之前,我会将所有类型转换为字符串。无论如何:

case class MyCustomType(a: Int)

  class TweetsSerializer extends Serializer[Seq[MyCustomType]] {

    private var encoding = "UTF8"

    override def configure(configs: java.util.Map[String, _], isKey: Boolean):   Unit = {
      val propertyName = if (isKey) "key.serializer.encoding"
      else "value.serializer.encoding"
      var encodingValue = configs.get(propertyName)
      if (encodingValue == null) encodingValue = configs.get("serializer.encoding")
      if (encodingValue != null && encodingValue.isInstanceOf[String]) encoding = encodingValue.asInstanceOf[String]
    }

    override def serialize(topic: String, data: Seq[MyCustomType]): Array[Byte] =
      try
          if (data == null) return null
          else return {
            data.map { v =>
              v.a.toString
            }
            .mkString("").getBytes("UTF8")
          }
      catch {
        case e: UnsupportedEncodingException =>
          throw new SerializationException("Error when serializing string to byte[] due to unsupported encoding " + encoding)
      }

    override def close(): Unit = Unit

  }

}

object testCustomKafkaSerializer extends App {


  implicit val producerConfig = {
    val props = new Properties()
    props.setProperty("bootstrap.servers", "localhost:9092")
    props.setProperty("key.serializer", classOf[StringSerializer].getName)
    props.setProperty("value.serializer", classOf[TweetsSerializer].getName)
    props
  }

  lazy val kafkaProducer = new KafkaProducer[String, Seq[MyCustomType]](producerConfig)

  // Create scala future from Java
  private def publishToKafka(id: String, data: Seq[MyCustomType]) = {
      kafkaProducer
        .send(new ProducerRecord("outTopic", id, data))
        .get()
  }

  val input = MyCustomType(1)

  publishToKafka("customSerializerTopic", Seq(input))


}