From 2147a1529cdc1e1fde1a2024caca7a2185b2d5b4 Mon Sep 17 00:00:00 2001 From: Thuillier Benjamin Date: Thu, 9 Dec 2021 17:49:40 +0100 Subject: [PATCH] add protobuf example (#8) * update gitignore * add a protobuf example of a custom deserializer --- .gitignore | 7 ++--- build.sbt | 11 ++++++++ project/plugins.sbt | 2 ++ project/scalapb.sbt | 3 +++ src/main/protobuf/addressbook.proto | 26 +++++++++++++++++++ .../MyCustomProtobufDeserializer.scala | 14 ++++++++++ 6 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 project/scalapb.sbt create mode 100644 src/main/protobuf/addressbook.proto create mode 100644 src/main/scala/io/example/conduktor/custom/deserializers/MyCustomProtobufDeserializer.scala diff --git a/.gitignore b/.gitignore index 2e992c4..ee38a81 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,6 @@ ### VisualStudioCode template .vscode/* -!.vscode/settings.json -!.vscode/tasks.json -!.vscode/launch.json -!.vscode/extensions.json -*.code-workspace + # Local History for Visual Studio Code .history/ @@ -97,6 +93,7 @@ target/ lib_managed/ src_managed/ project/boot/ +project/project project/plugins/project/ .history .cache diff --git a/build.sbt b/build.sbt index 322e133..ff76046 100644 --- a/build.sbt +++ b/build.sbt @@ -2,12 +2,23 @@ name := "my_custom_deserializers" version := sys.env.getOrElse("CREATED_TAG", "0.1") scalaVersion := "2.13.7" libraryDependencies += "org.apache.kafka" % "kafka-clients" % "2.8.1" +libraryDependencies ++= Seq( + "com.thesamet.scalapb" %% "scalapb-runtime" % scalapb.compiler.Version.scalapbVersion % "protobuf", + "com.thesamet.scalapb.common-protos" %% "proto-google-common-protos-scalapb_0.11" % "2.5.0-2" % "protobuf", + "com.thesamet.scalapb.common-protos" %% "proto-google-common-protos-scalapb_0.11" % "2.5.0-2" +) + +assembly / assemblyJarName := "plugins.jar" // ## Github Packages publish configs // More info, see: https://gist.github.com/guizmaii/2ca47b74ad8e26c772d7df6ada8ddb00 val GITHUB_OWNER = "conduktor" val GITHUB_PROJECT = "my_custom_deserializers" +Compile / PB.targets := Seq( + scalapb.gen() -> (Compile / sourceManaged).value / "scalapb" +) + ThisBuild / publishTo := Some( s"GitHub $GITHUB_OWNER Apache Maven Packages" at s"https://maven.pkg.github.com/$GITHUB_OWNER/$GITHUB_PROJECT" ) diff --git a/project/plugins.sbt b/project/plugins.sbt index f338c2a..9357bf8 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1 +1,3 @@ addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.1.20") + +addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.1.0") \ No newline at end of file diff --git a/project/scalapb.sbt b/project/scalapb.sbt new file mode 100644 index 0000000..4225151 --- /dev/null +++ b/project/scalapb.sbt @@ -0,0 +1,3 @@ +addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.3") + +libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.11.1" \ No newline at end of file diff --git a/src/main/protobuf/addressbook.proto b/src/main/protobuf/addressbook.proto new file mode 100644 index 0000000..16547ad --- /dev/null +++ b/src/main/protobuf/addressbook.proto @@ -0,0 +1,26 @@ +syntax = "proto2"; + +package io.example.conduktor.custom.deserializers; + +message Person { + required string name = 1; + required int32 id = 2; + optional string email = 3; + + enum PhoneType { + MOBILE = 0; + HOME = 1; + WORK = 2; + } + + message PhoneNumber { + required string number = 1; + optional PhoneType type = 2 [default = HOME]; + } + + repeated PhoneNumber phones = 4; +} + +message AddressBook { + repeated Person people = 1; +} \ No newline at end of file diff --git a/src/main/scala/io/example/conduktor/custom/deserializers/MyCustomProtobufDeserializer.scala b/src/main/scala/io/example/conduktor/custom/deserializers/MyCustomProtobufDeserializer.scala new file mode 100644 index 0000000..74dcef9 --- /dev/null +++ b/src/main/scala/io/example/conduktor/custom/deserializers/MyCustomProtobufDeserializer.scala @@ -0,0 +1,14 @@ +package io.example.conduktor.custom.deserializers + +import org.apache.kafka.common.serialization.{Deserializer, Serializer} +import io.example.conduktor.custom.deserializers.addressbook._ + +final class MyCustomProtobufSerializer extends Serializer[Person] { + override def serialize(topic: String, data: Person): Array[Byte] = data.toByteArray +} + +final class MyCustomProtobufDeserializer extends Deserializer[Person] { + override def deserialize(topic: String, data: Array[Byte]): Person = { + Person.parseFrom(data) + } +} \ No newline at end of file