📖 Scenario: You work at a company that collects real-time data streams from various sources. The data is sent through Kafka topics. Your task is to integrate Kafka with Hadoop to store and analyze this streaming data efficiently.
🎯 Goal: Build a simple pipeline that reads messages from a Kafka topic and writes them into Hadoop's HDFS for further analysis.
📋 What You'll Learn
Create a Kafka consumer configuration
Set up Hadoop file system path for data storage
Write code to consume messages from Kafka topic
Save consumed messages into HDFS files
💡 Why This Matters
🌍 Real World
Companies use Kafka to handle real-time data streams and Hadoop to store large volumes of data for analysis. Integrating them allows efficient data processing pipelines.
💼 Career
Data engineers and data scientists often build pipelines that connect streaming platforms like Kafka with big data storage systems like Hadoop.
Progress0 / 4 steps
1
Create Kafka consumer configuration
Create a dictionary called kafka_config with these exact entries: 'bootstrap.servers': 'localhost:9092', 'group.id': 'hadoop_integration_group', and 'auto.offset.reset': 'earliest'.
Hadoop
Hint
Use a Python dictionary with the exact keys and values as specified.
2
Set Hadoop HDFS output path
Create a string variable called hdfs_output_path and set it to '/user/hadoop/kafka_data/'.
Hadoop
Hint
Assign the exact string path to the variable hdfs_output_path.
3
Consume messages from Kafka topic
Write code to create a Kafka consumer using kafka_config and subscribe to the topic 'sensor_readings'. Use a for loop with variable message to consume messages from the consumer. Append each message's value decoded as UTF-8 to a list called messages. Initialize messages as an empty list before the loop.
Hadoop
Hint
Use KafkaConsumer with the config values and subscribe to 'sensor_readings'. Decode each message value and add to messages list.
4
Save consumed messages to HDFS
Import pyarrow.fs and create a Hadoop filesystem object called hdfs. Open a file at path hdfs_output_path + 'data.txt' in write mode. Write each message from the messages list to the file, each followed by a newline. Close the file after writing. Finally, print the number of messages saved using print(f"Saved {len(messages)} messages to HDFS").
Hadoop
Hint
Use pyarrow.fs.HadoopFileSystem to open a file stream and write each message with a newline. Then print the confirmation message.
Practice
(1/5)
1. What is the main purpose of integrating Kafka with Hadoop in data science?
easy
A. To move streaming data into Hadoop storage for analysis
B. To replace Hadoop's storage with Kafka's storage
C. To run machine learning models inside Kafka
D. To visualize data directly from Kafka without storage
Solution
Step 1: Understand Kafka and Hadoop roles
Kafka streams data in real-time, Hadoop stores large data for analysis.
Step 2: Identify integration purpose
Integration moves streaming data from Kafka into Hadoop for big data processing.
Final Answer:
To move streaming data into Hadoop storage for analysis -> Option A
What will be the HDFS directory where Kafka data is stored?
medium
A. /data/sensor
B. /kafka/sensor_data
C. /user/flume/kafka
D. /tmp/kafka_data
Solution
Step 1: Locate HDFS path in sink configuration
The sink hdfsSink has hdfs.path = /data/sensor.
Step 2: Confirm data storage directory
Kafka data consumed by Flume will be stored in /data/sensor on HDFS.
Final Answer:
/data/sensor -> Option A
Quick Check:
HDFS sink path = /data/sensor [OK]
Hint: Check hdfs.path in sink config for storage location [OK]
Common Mistakes:
Confusing Kafka topic name with HDFS path
Assuming default Flume user directory
Ignoring channel configuration
4. You set up Kafka to HDFS data transfer using Flume, but no data appears in HDFS. Which of the following is the most likely error in the Flume config?
medium
A. The memory channel type is set to file
B. The HDFS path is set correctly
C. The Flume agent name is missing
D. The Kafka topic name in kafka.topics is misspelled
Solution
Step 1: Check common config errors
Misspelling Kafka topic means Flume reads no data from Kafka.
Step 2: Evaluate options
Correct HDFS path and channel type do not cause no data; missing agent name causes startup error, not silent failure.
Final Answer:
The Kafka topic name in kafka.topics is misspelled -> Option D
Quick Check:
Wrong topic name = no data read [OK]
Hint: Check Kafka topic spelling first if no data in HDFS [OK]
Common Mistakes:
Assuming HDFS path causes no data
Changing channel type incorrectly
Ignoring agent name errors
5. You want to build a pipeline that streams sensor data from Kafka into Hadoop HDFS and then runs batch analysis daily. Which combination of tools and steps is best?
hard
A. Use Kafka producer to write directly to HDFS, then run Spark streaming
B. Use KafkaSource in Flume to ingest data into HDFS, then run Hadoop MapReduce daily
C. Use Hadoop fs commands to read Kafka topics, then store in local files
D. Use Kafka consumer to write data to local disk, then upload manually to HDFS
Solution
Step 1: Identify streaming ingestion method
Flume with KafkaSource is designed to stream Kafka data into HDFS efficiently.
Step 2: Choose batch analysis tool
Hadoop MapReduce runs batch jobs on HDFS data daily, fitting the requirement.
Final Answer:
Use KafkaSource in Flume to ingest data into HDFS, then run Hadoop MapReduce daily -> Option B