-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
113 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Holder Kafka/Avro til en egen modul, da den har en del avhengigheter som sannsynligvis ikke er nødvendig for alle. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
dependencies { | ||
implementation(project(":common:domain")) | ||
implementation(project(":common:infrastructure")) | ||
|
||
implementation("org.apache.kafka:kafka-clients:3.6.1") { | ||
exclude("org.apache.kafka", "kafka-raft") | ||
exclude("org.apache.kafka", "kafka-server-common") | ||
exclude("org.apache.kafka", "kafka-storage") | ||
exclude("org.apache.kafka", "kafka-storage-api") | ||
exclude("org.apache.kafka", "kafka-streams") | ||
} | ||
implementation("org.apache.avro:avro:1.11.3") | ||
} | ||
|
||
tasks.named<Jar>("jar") { | ||
archiveBaseName.set("common-infrastructure-avro") | ||
} |
32 changes: 32 additions & 0 deletions
32
common/infrastructure/kafka/src/main/kotlin/common/infrastructure/kafka/AvroDeserializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package common.infrastructure.kafka | ||
|
||
import arrow.core.Either | ||
import arrow.core.getOrElse | ||
import org.apache.avro.generic.GenericRecord | ||
import org.apache.avro.io.DecoderFactory | ||
import org.apache.kafka.common.serialization.Deserializer | ||
import java.nio.ByteBuffer | ||
|
||
class AvroDeserializer<T : GenericRecord>( | ||
private val mapper: (ByteBuffer) -> T, | ||
) : Deserializer<T> { | ||
|
||
override fun deserialize(topic: String, data: ByteArray): T { | ||
return Either.catch { | ||
val binaryDecoder = DecoderFactory.get().binaryDecoder(data, null) | ||
/* | ||
KafkaAvroSerializer legger på 5 bytes, 1 magic byte og 4 som sier noe om hvilke entry i schema registeret som | ||
brukes. Siden vi ikke ønsker å ha et dependency til schema registryet har vi en egen deserializer og skipper de | ||
5 første bytene | ||
https://docs.confluent.io/3.2.0/schema-registry/docs/serializer-formatter.html#wire-format | ||
*/ | ||
binaryDecoder.skipFixed(5) | ||
mapper(binaryDecoder.readBytes(null)) | ||
}.getOrElse { | ||
throw RuntimeException( | ||
"Kunne ikke deserialisere avromelding fra topic: $topic. Antall bytes: ${data.size}.", | ||
it, | ||
) | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
common/infrastructure/kafka/src/main/kotlin/common/infrastructure/kafka/AvroSerializer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package common.infrastructure.kafka | ||
|
||
import arrow.core.Either | ||
import arrow.core.getOrElse | ||
import org.apache.avro.generic.GenericDatumWriter | ||
import org.apache.avro.generic.GenericRecord | ||
import org.apache.avro.io.BinaryEncoder | ||
import org.apache.avro.io.EncoderFactory | ||
import org.apache.kafka.common.serialization.Serializer | ||
import java.io.ByteArrayOutputStream | ||
|
||
class AvroSerializer<T : GenericRecord> : Serializer<T> { | ||
private val encoderFactory = EncoderFactory.get() | ||
|
||
override fun serialize(topic: String?, data: T): ByteArray { | ||
return Either.catch { | ||
val writer = GenericDatumWriter<GenericRecord>(data.schema) | ||
val bytesOut = ByteArrayOutputStream() | ||
val encoder: BinaryEncoder = encoderFactory.binaryEncoder(bytesOut, null) | ||
writer.write(data, encoder) | ||
encoder.flush() | ||
// // KafkaAvroSerializer legger på fem bytes, 1 magic byte og 4 som sier noe om hvilke entry i schema registeret, | ||
// // https://docs.confluent.io/3.2.0/schema-registry/docs/serializer-formatter.html#wire-format | ||
byteArrayOf(0, 0, 0, 0, 0) + bytesOut.toByteArray() | ||
}.getOrElse { | ||
throw RuntimeException( | ||
"Kunne ikke serialisere avromelding til topic: $topic. Skjema: ${data.schema}", | ||
it, | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters