0
0
Kafkadevops~30 mins

Schema validation in producers in Kafka - Mini Project: Build & Apply

Choose your learning style9 modes available
Schema validation in producers
📖 Scenario: You are building a Kafka producer that sends user data messages to a topic. To ensure data quality, you want to validate each message against a predefined schema before sending it.
🎯 Goal: Create a Kafka producer that validates messages against a schema before sending them to the Kafka topic.
📋 What You'll Learn
Create a dictionary called user_data with keys "name", "age", and "email" and exact values "Alice", 30, and "alice@example.com"
Create a dictionary called schema that defines the expected types for "name" as str, "age" as int, and "email" as str
Write a function called validate_schema that takes data and schema and returns True if all keys exist and types match, otherwise False
Print "Valid message sent" if the data passes validation, otherwise print "Invalid message, not sent"
💡 Why This Matters
🌍 Real World
Validating data before sending it to Kafka ensures that consumers receive consistent and expected message formats, reducing errors downstream.
💼 Career
Many jobs require working with Kafka producers and ensuring data quality through schema validation to maintain reliable data pipelines.
Progress0 / 4 steps
1
Create the user data dictionary
Create a dictionary called user_data with these exact entries: "name": "Alice", "age": 30, and "email": "alice@example.com"
Kafka
Need a hint?

Use curly braces {} to create a dictionary with the exact keys and values.

2
Define the schema dictionary
Create a dictionary called schema with keys "name", "age", and "email" and values str, int, and str respectively
Kafka
Need a hint?

Use the Python type names str and int as values in the dictionary.

3
Write the schema validation function
Write a function called validate_schema that takes parameters data and schema and returns True if all keys in schema exist in data and their types match, otherwise returns False
Kafka
Need a hint?

Use a for loop to check each key and isinstance() to check the type.

4
Validate and print the result
Use an if statement to call validate_schema(user_data, schema). If it returns True, print "Valid message sent". Otherwise, print "Invalid message, not sent"
Kafka
Need a hint?

Use if to check the function result and print() to show the message.