Skip to content

Commit c00d7a7

Browse files
committed
Improve readme
1 parent 793cd17 commit c00d7a7

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,45 @@ This functionality is backed by the following libraries:
1313
- [FS2 kafka](https://github.com/fd4s/fs2-kafka)
1414
- [Confluent Schema Registry](https://github.com/confluentinc/schema-registry)
1515

16-
### Usage ###
16+
### Usage
1717

1818
Add the following to your `build.sbt`
1919
```sbt
2020
resolvers ++= Seq("confluent" at "https://packages.confluent.io/maven")
2121
libraryDependencies += "io.kaizen-solutions" %% "fs2-kafka-jsonschema" % "<latest-version>"
2222
```
23+
24+
### Example
25+
26+
Define the datatype that you would like to send/receive over Kafka via the JSON + JSON Schema format. You do this by defining your datatype and providing a `Pickler` instance for it.
27+
The `Pickler` instance comes from the Tapir library.
28+
29+
```scala
30+
import sttp.tapir.Schema.annotations.*
31+
import sttp.tapir.json.pickler.*
32+
33+
final case class Book(
34+
@description("name of the book") name: String,
35+
@description("international standard book number") isbn: Int
36+
)
37+
object Book:
38+
given Pickler[Book] = Pickler.derived
39+
```
40+
41+
Next, you can create a fs2 Kafka `Serializer` and `Deserializer` for this datatype and use it when building your FS2 Kafka producer/consumer.
42+
43+
```scala
44+
import io.kaizensolutions.jsonschema.*
45+
import cats.effect.*
46+
import fs2.kafka.*
47+
48+
def bookSerializer[F[_]: Sync]: Resource[F, ValueSerializer[F, Book]] =
49+
JsonSchemaSerializerSettings.default
50+
.withSchemaRegistryUrl("http://localhost:8081")
51+
.forValue[F, Book]
52+
53+
def bookDeserializer[F[_]: Sync]: Resource[F, ValueDeserializer[F, Book]] =
54+
JsonSchemaDeserializerSettings.default
55+
.withSchemaRegistryUrl("http://localhost:8081")
56+
.forValue[F, Book]
57+
```

0 commit comments

Comments
 (0)