Skip to content

Commit

Permalink
add protobuf example (#8)
Browse files Browse the repository at this point in the history
* update gitignore

* add a protobuf example of a custom deserializer
  • Loading branch information
bthuillier authored Dec 9, 2021
1 parent 0e1d39e commit 2147a15
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
7 changes: 2 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
Expand Down Expand Up @@ -97,6 +93,7 @@ target/
lib_managed/
src_managed/
project/boot/
project/project
project/plugins/project/
.history
.cache
Expand Down
11 changes: 11 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat" % "0.1.20")

addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "1.1.0")
3 changes: 3 additions & 0 deletions project/scalapb.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
addSbtPlugin("com.thesamet" % "sbt-protoc" % "1.0.3")

libraryDependencies += "com.thesamet.scalapb" %% "compilerplugin" % "0.11.1"
26 changes: 26 additions & 0 deletions src/main/protobuf/addressbook.proto
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 2147a15

Please sign in to comment.