Giter Club home page Giter Club logo

kafka-serde-scala's Introduction

kafka-serde-scala

Join the chat at https://gitter.im/azhur/kafka-serde-scala Build Status

kafka-serde-scala provides implicit conversions from different type class Encoder/Decoder to kafka Serializer, Deserializer, Serde.

Following target libraries are supported:

Inspired by https://github.com/hseeberger/akka-http-json.

Installation

Maven Central

Add dependencies for the selected integration:

  • for avro4s:
libraryDependencies ++= List(
  "io.github.azhur" %% "kafka-serde-avro4s" % version
)
  • for circe:
libraryDependencies ++= List(
  "io.github.azhur" %% "kafka-serde-circe" % version
)
  • for jackson:
libraryDependencies ++= List(
  "io.github.azhur" %% "kafka-serde-jackson" % version
)
  • for json4s:
libraryDependencies ++= List(
  "io.github.azhur" %% "kafka-serde-json4s" % version
)
  • for jsoniter-scala:
libraryDependencies ++= List(
  "io.github.azhur" %% "kafka-serde-jsoniter-scala" % version,
  "com.github.plokhotnyuk.jsoniter-scala" %% "jsoniter-scala-macros" % jsoniter_scala_version % Provided // required only in compile-time
)
  • for play-json:
libraryDependencies ++= List(
  "io.github.azhur" %% "kafka-serde-play-json" % version
)
  • for upickle:
libraryDependencies ++= List(
  "io.github.azhur" %% "kafka-serde-upickle" % version
)
  • for scalapb:
libraryDependencies ++= List(
  "io.github.azhur" %% "kafka-serde-scalapb" % version
)
  • for zio-json:
libraryDependencies ++= List(
  "io.github.azhur" %% "kafka-serde-zio-json" % version
)

Usage

Mix xxxSupport into your code which requires implicit Kafka Serde, Serializer or Deserializer, where xxx is the target library used for serialization, i.e: CirceSupport.

Provide your implicit type class instances and the magic will convert them to Kafka serializers:

  • for avro4s: com.sksamuel.avro4s.SchemaFor[T], com.sksamuel.avro4s.ToRecord[T], com.sksamuel.avro4s.FromRecord[T]
  • for circe: io.circe.Encoder[T], io.circe.Decoder[T]
  • for jackson json: com.fasterxml.jackson.databind.ObjectMapper
  • for jackson binary: com.fasterxml.jackson.databind.ObjectMapper, org.codehaus.jackson.FormatSchema
  • for json4s: org.json4s.DefaultFormats, org.json4s.Serialization
  • for jsoniter-scala: com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec[T], (and optionally com.github.plokhotnyuk.jsoniter_scala.core.WriterConfig or/and com.github.plokhotnyuk.jsoniter_scala.core.ReaderConfig)
  • for play-json: play.api.libs.json.Reads, play.api.libs.json.Writes
  • for upickle: upickle.default.Reader, upickle.default.Writer
  • for scalapb: scalapb.GeneratedMessageCompanion
  • for zio-json: zio.json.JsonEncoder, zio.json.JsonDecoder

For more info, please, take a look at unit tests and at kafka-serde-scala-example which is a kafka-streams (2.x) application with kafka-serde-scala usage.

Contribution

Feel free to contribute with creating PR or opening issues.

License

This code is open source software licensed under the Apache 2.0 License.

kafka-serde-scala's People

Contributors

azhur avatar codacy-badger avatar jiminhsieh avatar plokhotnyuk avatar scala-steward avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

kafka-serde-scala's Issues

Support for serdes in windows?

I'm not sure if I'm missing something but would really appreciate some help. If I use no windows the implicits are found, for example:

// All good
object CustomerStream extends App with PlayJsonSupport {

  val totalPagesCounts: KTable[String, Long] = inputStream
    .filter((_ , ev) => ev.eventData.evData.pageType.isDefined && ev.eventData.custData.customerUid.isDefined)
    .groupBy((_, ev) => ev.eventData.custData.customerUid.get)
    //.windowedBy(SessionWindows.with(Duration.ofHours(3))
    .count()
  totalPagesCounts.toStream.to("total_pageviews")

}

but if I use windows, the produced implicits are not found (Produced[Windowed[String], Long]):

object CustomerStream extends App with PlayJsonSupport {
 // Error
  val totalPagesCounts: KTable[Windowed[String], Long] = inputStream
    .filter((_ , ev) => ev.eventData.evData.pageType.isDefined && ev.eventData.custData.customerUid.isDefined)
    .groupBy((_, ev) => ev.eventData.custData.customerUid.get)
    .windowedBy(TimeWindows.of(Duration.ofSeconds(5)))
    .count()
  totalPagesCounts.toStream.to("total_pageviews") \\ -> No implicits found for parameter produced: Produced[Windowed[String], Long]

}

Any idea why?

Thanks!

adding support to scalapb

Hello, I think this project is not JSON only specific, right? If possible, can we add support to scalapb? Thanks!

How to use this with a Producer/Consumer (not KafkaStreams)

Hi,

I have been using the implicit Serdes for a KafkaStreams app very nicely, but for the integration tests, I also need to manually produce some messages.
As the producer actually requires a string I have been wondering how to achieve this.

The code snippet below shows the use case (which errors with a runtime error - not a build error)
Error: Invalid value com.example.reading_filter.models.JsonSerde$$anon$1@3aaf4f07 for configuration key.serializer: Expected a Class instance or class name

object KafkaHelper {
  private def fooSerializer()(implicit serde: Serde[Foo]) = serde.deserializer()


  def getProducer: KafkaProducer[Foo, Foo] = {
    val props = new Properties
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, appConfig.bootstrapServers)
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, fooSerializer)
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, fooSerializer)
    new KafkaProducer(props)
}

...
KafkaHelper.getProducer

I also tried fooSerializer.getClass, but this results in Could not find a public no-argument constructor for com.example.reading_filter.models.JsonSerde$$anon$1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.