Noufal Rijal
07/30/2025, 5:25 AMDian Fu
07/30/2025, 9:23 AMkey_by before writing data to sink.Noufal Rijal
07/30/2025, 11:12 AMsink = (
sink_builder.set_record_serializer(
KafkaRecordSerializationSchema.builder()
.set_topic(topic_name)
.set_key_serialization_schema(SimpleStringSchema())
# .set_key_serialization_schema(lambda row: row[0].encode('utf-8'))
.set_value_serialization_schema(SimpleStringSchema())
.build()
)
.set_delivery_guarantee(DeliveryGuarantee.AT_LEAST_ONCE)
.build()
)
and below is the code that i am using to construct my final message which is to be synced to kafka -
def setup_sink_pipeline(processed_stream):
"""
Set up the Kafka sink part of the pipeline.
Args:
processed_stream: Processed tracking results stream
Returns:
DataStream: Output stream connected to sink
"""
# Map the processed results to JSON strings
def serialize_final_message(x):
# Extract video_id for partitioning key
id = str(x[0]) if x[9] == "-source" else str(x[1])
json_message = json.dumps(
{
# values
}
)
logger.info(f"type of json_message -- {type(json_message)}")
# return json_message
return (id, json_message)
# Map processed data to key-value tuples
kafka_output_stream = processed_stream.map(
serialize_final_message,
output_type=Types.TUPLE([Types.STRING(), Types.STRING()]),
)
# Add this map to force element type (Python native tuple[str, str])
logger.info("Sink pipeline maintains partition consistency (no re-keying)")
return kafka_output_stream
below is the code that consumes the messages and performs the keying process -
output_stream = setup_sink_pipeline(detection_stream)
keyed_output_stream = output_stream.key_by(lambda x: str(x[0]), key_type=Types.STRING())
keyed_output_stream.sink_to(kafka_sink)
While doing so i am getting an error stating that java.lang.ClassCastException: class org.apache.flink.types.Row cannot be cast to class java.lang.String.
Can you please help me, if there is any issues with the way i am trying to implement the solution.Dian Fu
07/31/2025, 9:36 AMoutput_stream = setup_sink_pipeline(detection_stream)
keyed_output_stream = output_stream.key_by(lambda x: str(x[0]), key_type=Types.STRING())
keyed_output_stream = keyed_output_stream.map(lambda x: x[1], output_type=Types.STRING())
keyed_output_stream.sink_to(kafka_sink)
and changed the sink as following:
sink = (
sink_builder.set_record_serializer(
KafkaRecordSerializationSchema.builder()
.set_topic(topic_name)
# .set_key_serialization_schema(SimpleStringSchema())
# .set_key_serialization_schema(lambda row: row[0].encode('utf-8'))
.set_value_serialization_schema(SimpleStringSchema())
.build()
)
.set_delivery_guarantee(DeliveryGuarantee.AT_LEAST_ONCE)
.build()
)Ridvan Duran
08/04/2025, 1:33 PMRidvan Duran
08/04/2025, 1:34 PMRidvan Duran
08/04/2025, 1:36 PMNoufal Rijal
08/04/2025, 1:41 PMDian Fu
08/05/2025, 6:37 AMNoufal Rijal
08/05/2025, 6:38 AMDian Fu
08/05/2025, 9:39 AMNoufal Rijal
08/05/2025, 9:40 AMDian Fu
08/05/2025, 9:53 AMNoufal Rijal
08/06/2025, 3:15 AMNoufal Rijal
12/20/2025, 11:03 AM