I am trying to publish the data which was originally a list
but I converted it to the string
and then tried to push it to the Kafka
topic as per this official documentation I tried the below code:
sink = KafkaSink.builder() \
.set_bootstrap_servers("localhost:9092") \
.set_record_serializer(
KafkaRecordSerializationSchema.builder()
.set_topic("test-topic45")
.set_value_serialization_schema(SimpleStringSchema())
.build()
) \
.build()
datastream.sink_to(sink)
but it threw the below error:
Caused by: java.lang.ClassCastException: class [B cannot be cast to class java.lang.String ([B and java.lang.String are in module java.base of loader 'bootstrap')
I even tried setting up the key
serializer to SimpleStringSchema
(which I don't think was needed) but same result.
Also, I don't need to convert explicitly since SimpleStringSchema
will handle it for me also I have ensured my upstream layers are working fine. Absolutely no problem with that.
The upstream layer to this is the process function
which returns a list of tuples of tuples
and I haven't mentioned the output_type
parameter for the process function
. Should I mention it or will it be handled by SimpleStringSchema
?
Is there anything else I am missing here?
Any hints are appreciated.
Yes, you're right. The error indicates the stream record doesn't match to what's been specified in the Kafka sink builder.
You would find JSON to be more useful than STRING and the Flink document has an example about how to serialise Kafka key and value with JSON - see link
Below shows a quick working example. Note you should have the connector JAR file in (
./jars/flink-sql-connector-kafka-1.17.1.jar
).If successful, you should be able see the following Kafka message.
Also the message value can be parsed as shown below. Note it only works with Python and wouldn't be recommended if the messages are shared by other data types.