0
votes

I am new to Kafka Streams. I want to perform following KStream-GlobalKTable pure DSL based left-join operation, and not using map operation.

I have an input stream a.topic which is <String, A>, where value :

{
    "b_obj": {
        "b_value": "xyz",
        "c_list": [
            {
                "d_obj": {
                    "d_id1": "value1",
                    "d_id2": "value2",
                    "d_value": "some value"
                },
                "c_value": "jkl"
            },
            {
                "d_obj": {
                    "d_id1": "value3",
                    "d_id2": "value4",
                    "d_value": "some value 2"
                },
                "c_value": "pqr"
            }
        ]
    },
    "a_value": "abcd"
}

And another input topic e.topic which is <String, E>, where value :

{
    "e_id1": "value1",
    "e_id2": "value2",
    "e_value": "some value"
}

I want to perform left join operation a.topic is a stream, and master data e.topic is a global table to achieve result value as

{
    "b_obj": {
        "b_value": "xyz",
        "c_list": [
            {
                "d_obj": {
                    "d_id1": "value1",
                    "d_id2": "value2",
                    "d_value": "some value"
                },
                "e_obj": {
                    "e_id1": "value1",
                    "e_id2": "value2",
                    "e_value": "some value a"
                },
                "c_value": "jkl"
            },
            {
                "d_obj": {
                    "d_id1": "value3",
                    "d_id2": "value4",
                    "d_value": "some value 2"
                },
                "e_obj": {
                    "e_id1": "value3",
                    "e_id2": "value4",
                    "e_value": "some value b"
                },
                "c_value": "pqr"
            }
        ]
    },
    "a_value": "abcd"
}

and the join condition is a.b.c[i].d.d_id1 == e.e_id1 && a.b.c[i].d.d_id2 == e.e_id2

CODE:

public class ComplexBeanStream {

    public static void main(String[] args) {

        Serde<A> aSerde = new JsonSerde<>(A.class);

        Serde<E> eSerde = new JsonSerde<>(E.class);

        final Properties streamsConfiguration = new Properties();
        streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, "complex-bean-app");
        streamsConfiguration.put(StreamsConfig.CLIENT_ID_CONFIG, "complex-bean-client");
        streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");

        StreamsBuilder builder = new StreamsBuilder();

        final GlobalKTable<String, E> eGlobalTable =
                builder.globalTable(
                        "e.topic",
                        Materialized.<String, E, KeyValueStore<Bytes, byte[]>>
                                as("E-STORE")
                                .withKeySerde(Serdes.String())
                                .withValueSerde(eSerde)
                );

        final KStream<String, A> aStream =
                builder.stream(
                        "a.topic",
                        Consumed.with(Serdes.String(), aSerde));

        // perform left-join here

        Topology topology = builder.build();

        System.out.println("\n\nComplexBeanStream Topology: \n" + topology.describe());

        final KafkaStreams streams = new KafkaStreams(topology, streamsConfiguration);

        streams.cleanUp();

        streams.start();

        Runtime.getRuntime().addShutdownHook(new Thread(streams::close));
    }

}

class A {

    private B b_obj;

    private String a_value;
}

class B {

    private List<C> c_list;

    private String b_value;
}

class C {

    private D d_obj;

    private E e_obj;

    private String c_value;
}

class D {

    private String d_id1;

    private String d_id2;

    private String d_value;
}

class E {

    private String e_id1;

    private String e_id2;

    private String e_value;
}
1

1 Answers

1
votes

It is not possible to join without mapping in your case, as long as the join lookup is based on values and not keys.

Kafka-Streams can be joined only based on the same key on the both side. Which means, that you should map and select new keys (re-keying) for the both side of your join in oder to achieve a.b.c[i].d.d_id1 == e.e_id1 && a.b.c[i].d.d_id2 == e.e_id2.

In this case, one side could have [a.b.c[i].d.d_id1, a.b.c[i].d.d_id2] as its key and the other side [e.e_id1, e.e_id2]. If you have a match, the values could be joined to a new object. Probably you should flatmap your c_list before re-keying.

It is also helpful to read about Joining of Kafka-Streams.