Skip to content

Serde String[] in Kafka

I’m new in kafka. I am using the store builder and want to have a String[] of two elements as the value associated to the key. I set up the store like this:

StoreBuilder<KeyValueStore<String, String>> storeBuilder=Stores.keyValueStoreBuilder(
                Stores.persistentKeyValueStore(storeName),
                Serdes.String(),
                Serdes.String());
builder.addStateStore(storeBuilder);

When I call the method to have the data into the store :

String []oldValues = store.get(v.getVessel());

I receive this error:

Caused by: java.lang.ClassCastException: class java.lang.String cannot be cast to class [Ljava.lang.String; (java.lang.String and [Ljava.lang.String; are in module java.base of loader 'bootstrap')

I should set the store like this:

StoreBuilder<KeyValueStore<String, String[]>>

but I don’t know what to put instead of Serdes.String()

Answer

You would need to define your own Serializer, Deserializer and Serde subclasses to do this.

You can simply use a plain string with JSON deserialization to create a String array, or CSV