📖 Scenario: You are working on a messaging system using Kafka. You want to send and receive messages with structured data. To ensure data consistency, you will use JSON Schema and Protobuf formats for your messages.This project will guide you through creating data, configuring schema types, serializing messages, and printing the serialized output.
🎯 Goal: Build a simple Kafka message producer that creates data, configures the schema type (JSON Schema or Protobuf), serializes the data accordingly, and prints the serialized message.
📋 What You'll Learn
Create a data dictionary with specific fields
Set a variable to choose the schema type ('json' or 'protobuf')
Serialize the data based on the chosen schema type
Print the serialized message output
💡 Why This Matters
🌍 Real World
Kafka is widely used for messaging in distributed systems. Using JSON Schema and Protobuf helps keep data consistent and easy to parse across different services.
💼 Career
Understanding how to serialize data with JSON Schema and Protobuf is important for backend developers, data engineers, and anyone working with event-driven architectures.
Progress0 / 4 steps
1
Create the data dictionary
Create a dictionary called message_data with these exact entries: 'id': 101, 'name': 'Alice', 'active': True
Kafka
Hint
Use curly braces to create a dictionary with keys and values exactly as shown.
2
Set the schema type variable
Create a variable called schema_type and set it to the string 'json'
Kafka
Hint
Assign the string 'json' to the variable schema_type.
3
Serialize the data based on schema type
Write code to create a variable serialized_message that stores the JSON string of message_data if schema_type is 'json'. Otherwise, set serialized_message to the string 'protobuf_serialized'.
Kafka
Hint
Use json.dumps() to convert the dictionary to a JSON string when schema_type is 'json'. Use an if-else statement.
4
Print the serialized message
Write a print statement to display the value of serialized_message
Kafka
Hint
Use print(serialized_message) to show the serialized data.
Practice
(1/5)
1. What is the main purpose of using JSON Schema or Protobuf with Kafka messages?
easy
A. To define the structure and format of messages so producers and consumers agree
B. To encrypt Kafka messages for security
C. To speed up Kafka message delivery by skipping validation
D. To store Kafka messages permanently in a database
Solution
Step 1: Understand message format agreement
JSON Schema and Protobuf define how messages look so both sender and receiver understand the data.
Step 2: Identify the main purpose
This agreement prevents errors and confusion when reading messages.
Final Answer:
To define the structure and format of messages so producers and consumers agree -> Option A
Quick Check:
Message format agreement = C [OK]
Hint: They define message format so systems agree on data [OK]
Common Mistakes:
Thinking JSON Schema encrypts messages
Believing Protobuf speeds delivery by skipping validation
Confusing schema with storage
2. Which of the following is the correct way to specify a Protobuf schema for a Kafka message?
easy
A. { 'name': 'string', 'id': 'int' }
B. schema { field name: string; field id: int; }
C. stringint
D. message Person { string name = 1; int32 id = 2; }
Solution
Step 1: Recognize Protobuf syntax
Protobuf uses the 'message' keyword with fields and their types and numbers.
Step 2: Match the correct syntax
message Person { string name = 1; int32 id = 2; } shows correct Protobuf message definition syntax.
Final Answer:
message Person { string name = 1; int32 id = 2; } -> Option D
Quick Check:
Protobuf syntax = B [OK]
Hint: Protobuf uses 'message' keyword and numbered fields [OK]
Common Mistakes:
Confusing JSON or XML syntax with Protobuf
Missing field numbers in Protobuf
Using invalid keywords like 'schema'
3. Given this JSON Schema snippet for a Kafka message: