-
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
16 changed files
with
158 additions
and
62 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") | ||
} |
41 changes: 41 additions & 0 deletions
41
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,41 @@ | ||
package common.infrastructure.kafka | ||
|
||
import arrow.core.Either | ||
import arrow.core.getOrElse | ||
import org.apache.avro.Schema | ||
import org.apache.avro.io.DecoderFactory | ||
import org.apache.avro.specific.SpecificDatumReader | ||
import org.apache.avro.specific.SpecificRecord | ||
import org.apache.kafka.common.serialization.Deserializer | ||
|
||
class AvroDeserializer<T : SpecificRecord>( | ||
private val schema: Schema, | ||
) : Deserializer<T> { | ||
val reader = SpecificDatumReader<T>(schema) | ||
override fun deserialize(topic: String, data: ByteArray): T { | ||
return Either.catch { | ||
val decoder = 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 | ||
*/ | ||
// Check if the magic byte is present | ||
if (data[0].toInt() == 0) { | ||
// Skip the first 5 bytes (1 for magic byte, 4 for schema ID) | ||
// val payload = data.copyOfRange(5, data.size) | ||
decoder.skipFixed(5) | ||
// reader.read(null, payload) | ||
} else { | ||
// reader.read(null, data) | ||
} | ||
reader.read(null, decoder) | ||
}.getOrElse { | ||
throw RuntimeException( | ||
"Kunne ikke deserialisere avromelding fra topic: $topic. Antall bytes: ${data.size}. Første 10 bytes: ${data.take(10).joinToString(",")}. Skjema: ${schema.toString(true)}", | ||
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.io.BinaryEncoder | ||
import org.apache.avro.io.EncoderFactory | ||
import org.apache.avro.specific.SpecificDatumWriter | ||
import org.apache.avro.specific.SpecificRecord | ||
import org.apache.kafka.common.serialization.Serializer | ||
import java.io.ByteArrayOutputStream | ||
|
||
class AvroSerializer<T : SpecificRecord> : Serializer<T> { | ||
private val encoderFactory = EncoderFactory.get() | ||
|
||
override fun serialize(topic: String?, data: T): ByteArray { | ||
return Either.catch { | ||
val writer = SpecificDatumWriter<T>(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.toString(true)}", | ||
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
Oops, something went wrong.