com.giladam:kafka-jackson-serde

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

License

License

Categories

Categories

Jackson Data JSON
GroupId

GroupId

com.giladam
ArtifactId

ArtifactId

kafka-jackson-serde
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/
Source Code Management

Source Code Management

https://github.com/giladam/kafka-jackson-serde.git

Download kafka-jackson-serde

How to add to project

<!-- https://jarcasting.com/artifacts/com.giladam/kafka-jackson-serde/ -->
<dependency>
    <groupId>com.giladam</groupId>
    <artifactId>kafka-jackson-serde</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.giladam/kafka-jackson-serde/
implementation 'com.giladam:kafka-jackson-serde:1.0.0'
// https://jarcasting.com/artifacts/com.giladam/kafka-jackson-serde/
implementation ("com.giladam:kafka-jackson-serde:1.0.0")
'com.giladam:kafka-jackson-serde:jar:1.0.0'
<dependency org="com.giladam" name="kafka-jackson-serde" rev="1.0.0">
  <artifact name="kafka-jackson-serde" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.giladam', module='kafka-jackson-serde', version='1.0.0')
)
libraryDependencies += "com.giladam" % "kafka-jackson-serde" % "1.0.0"
[com.giladam/kafka-jackson-serde "1.0.0"]

Dependencies

compile (1)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-databind jar 2.9.5

provided (1)

Group / Artifact Type Version
org.apache.kafka : kafka-clients jar 1.0.0

test (7)

Group / Artifact Type Version
junit : junit jar 4.12
ch.qos.logback : logback-classic jar 1.2.3
com.fasterxml.jackson.datatype : jackson-datatype-jsr310 jar 2.9.5
com.fasterxml.jackson.dataformat : jackson-dataformat-smile jar 2.9.5
org.apache.kafka : kafka-streams jar 1.0.0
org.apache.commons : commons-lang3 jar 3.7
com.google.guava : guava jar 24.0-jre

Project Modules

There are no modules declared in this project.

Generic Jackson Serde for Kafka

For when you are working with Kafka and you just want to set your own instance of a configured Jackson ObjectMapper for your Serializers/Deserializers. (Useful when you are using Jackson binary dataformats or special Jackson ObjectMapper configurations.)

Or you don't feel like creating your own version of this even though there are code examples out there because you likely need something like this in your Kafka consumer/producer/streams projects that need to work with JSON.

It's not anything revolutionary, but I got sick of seeing people creating customized versions of this type of thing for every project or worse for every type they deal with. Maybe it's useful for you to have a premade version of this type of thing, too.

NOTE: Spring Kafka's JsonSerde pretty much does the same thing, so you should probably use it instead: spring-kafka's JsonSerializer/JsonDeserializer/JsonSerde I didn't know it existed at the time I wrote this library. (But I kind of find Spring Kafka's dependency requirements to not always work for my projects, so maybe this is still useful. I don't know; I'm still deciding. I just know I can't always rely on Spring-Kafka.)

Maven Dependency

<dependency>
  <groupId>com.giladam</groupId>
  <artifactId>kafka-jackson-serde</artifactId>
  <version>1.0.0</version>
</dependency>

Example Usage in Java Code

Example usage for Kafka producer/consumer:

    //An instance of a Jackson ObjectMapper configured to your liking: 
    ObjectMapper OBJECT_MAPPER = new ObjectMapper()
            .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .enable(SerializationFeature.INDENT_OUTPUT)
            .findAndRegisterModules();

    //Using the serializer with a specific instance of an ObjectMapper:
    Producer<String,Sample> producer = new KafkaProducer<>(producerConfig,
                                                           Serdes.String().serializer(),
                                                           new Jackson2Serializer<>(OBJECT_MAPPER));
                                                           
    //Using the deserializer with a specific instance of an ObjectMapper and configuring to deserialize a particular type:
    KafkaConsumer<String,Sample> consumer = new KafkaConsumer<>(consumerConfig, 
                                                                Serdes.String().deserializer(),
                                                                new Jackson2Deserializer<>(OBJECT_MAPPER, Sample.class));

Example usage for Kafka Streams application:

    //An instance of a Jackson ObjectMapper configured to your liking:
    ObjectMapper OBJECT_MAPPER = new ObjectMapper()
            .enable(SerializationFeature.INDENT_OUTPUT)
            .findAndRegisterModules();

    StreamsBuilder builder = new StreamsBuilder();

    //Using Serde with a specific instance of an ObjectMapper and configuring to handle a particular type:
    Serde<Sample> sampleSerde = new Jackson2Serde<>(OBJECT_MAPPER, Sample.class);
    Consumed<String, Sample> consumedWith = Consumed.with(Serdes.String(), sampleSerde);

    KStream<String, Sample> sampleMessages = builder.stream(TEST_TOPIC_NAME, consumedWith);
    
    ... the rest of your application...

Running the Integration Tests

The integration tests require an actual Kafka Cluster to run. They assume that this cluster is available at testing.kafka.host:9029 (which I configure by /etc/hosts/properties on my build machine.)

Versions

Version
1.0.0