Search code examples
spring-bootapache-kafkaapache-kafka-streamsspring-kafkaspring-cloud-stream

Reducing boilerplate in Spring Cloud Kafka Streams app


In my Spring Cloud Kafka Streams app, I want to join a KTable<String, Foo> to a KTable<String, Bar>. The inputs to my app have been encoded using MessagePack, so I've defined Serdes for Foo and Bar using the code below.

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Foo {
  String a;
  String b;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Bar {
  String c;
  String d;
}
// ObjectMapper mapper = new ObjectMapper(new MessagePackFactory());

@Bean
public Serde<Foo> fooSerde() {
  return new JsonSerde<Foo>(Foo.class, mapper);
}

@Bean
public Serde<Bar> barSerde() {
  return new JsonSerde<Bar>(Bar.class, mapper);
}

My question is, to specify the value type of the KTable after the join, do I have to now define:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class FooBar {
  String a;
  String b;
  String c;
  String d;
}
@Bean
public Serde<FooBar> fooBarSerde() {
  return new JsonSerde<FooBar>(FooBar.class, mapper);
}

It just seems like quite a lot of boilerplate? Especially if Foo and Bar have many fields. Is there anything I could do shorten my code?


Solution

  • Kafka newbie here.

    what I've seen so far, is to have a FooBar class that has an instance of Foo and Bar, so you don't need to touch the 'joined' class. For example

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class FooBar {
      Foo foo;
      Bar bar;
    }