📖 Scenario: You are working on a Kafka project where you need to define a data schema using Avro. This schema will describe the structure of messages sent to a Kafka topic about user information.
🎯 Goal: Create an Avro schema that defines a record for user data with specific fields and types.
📋 What You'll Learn
Define an Avro schema as a JSON string
Include a record named User
Add fields name (string), age (int), and email (string)
Set the namespace to com.example.kafka
💡 Why This Matters
🌍 Real World
Avro schemas are used to define the structure of data sent through Kafka topics, ensuring producers and consumers agree on the data format.
💼 Career
Understanding Avro schema definition is important for roles involving data engineering, streaming data pipelines, and Kafka-based system development.
Progress0 / 4 steps
1
Create the basic Avro schema structure
Create a variable called user_schema and assign it a JSON string that starts with { and includes "type": "record" and "name": "User".
Kafka
Hint
Start by creating a string that looks like a JSON object with type and name keys.
2
Add the namespace to the schema
Update the user_schema string to include the "namespace": "com.example.kafka" key-value pair inside the JSON object.
Kafka
Hint
Add the namespace key with the exact value inside the JSON string.
3
Add fields to the Avro schema
Modify the user_schema string to include a "fields" array with three fields: {"name": "name", "type": "string"}, {"name": "age", "type": "int"}, and {"name": "email", "type": "string"}.
Kafka
Hint
Remember to format the fields as a JSON array inside the string.
4
Print the complete Avro schema
Write a print statement to display the user_schema variable.
Kafka
Hint
Use print(user_schema) to show the schema string.
Practice
(1/5)
1. What is the main purpose of an Avro schema in Kafka?
easy
A. To route messages to different topics
B. To define the structure and data types of messages
C. To compress messages for faster transmission
D. To encrypt messages for security
Solution
Step 1: Understand Avro schema role
An Avro schema describes how data is organized and typed in messages.
Step 2: Compare options with schema purpose
Only defining structure and data types matches the schema's role.
Final Answer:
To define the structure and data types of messages -> Option B
Quick Check:
Avro schema = data structure definition [OK]
Hint: Avro schema describes message format, not security or routing [OK]
Common Mistakes:
Confusing schema with encryption or compression
Thinking schema controls message routing
Assuming schema is for message storage
2. Which of the following is the correct way to start an Avro record schema definition?
easy
A. {"type": "enum", "symbols": ["A", "B"]}
B. {"type": "array", "items": "string"}
C. {"type": "map", "values": "int"}
D. {"type": "record", "name": "User", "fields": []}
Solution
Step 1: Identify record schema syntax
An Avro record schema starts with type "record", a name, and a fields array.
Step 2: Match options to record schema
Only {"type": "record", "name": "User", "fields": []} matches the record schema format correctly.
Final Answer:
{"type": "record", "name": "User", "fields": []} -> Option D
Quick Check:
Record schema = type "record" with name and fields [OK]
Hint: Record schemas always start with type "record" and have fields [OK]