Dive into the implementation of stream data processing with Mage, using Kafka as source.
Outline
Introduction to Mage
Why is kafka a popular component of streaming applications?
Step by step guide to create streaming pipeline on Mage
Conclusion
Introduction to Mage
Mage is a powerful data processing tool allowing integration and synchronization of data from third-party sources. It supports building real-time and batch pipelines using Python, SQL, and R, making data transformation simple and efficient. Moreover, it enables running, monitoring, and orchestrating thousands of pipelines, ensuring a smooth data operation without the risk of data loss or interruption.
Why is kafka a popular component of streaming applications?
Apache Kafka is an open-source stream-processing software platform developed by LinkedIn and later donated to the Apache Software Foundation. It's built on the publish-subscribe messaging system and designed to handle real-time data feeds. Kafka is essentially a distributed event log service that is fault-tolerant, highly scalable, and provides high throughput for publishing and subscribing records.
Given its robust features, Kafka is a popular component of streaming applications due to the following reasons:
Performance and Scalability: Kafka can handle real-time data feeds on a large scale, processing millions of messages per second. Its distributed architecture allows for effortless scalability.
Durability and Reliability: Kafka's distributed commit log ensures robust data persistence, safeguarding against data loss. If a node fails, the data can still be retrieved from other nodes, hence ensuring reliability.
Fault Tolerance: Kafka can handle system failures without impacting the availability of data streams, which is crucial for applications that require constant, uninterrupted access to data.
Real-time Processing: Kafka supports both batch and real-time use cases, providing developers with flexibility when creating various applications.
Integration Capabilities: Kafka can integrate with a wide range of programming languages and data systems, making it versatile for differing application needs.
Kafka's popularity stems from its high performance, reliability, fault tolerance, real-time processing, and comprehensive integration capabilities.
Step by step guide to create streaming pipeline on Mage
Dive into a comparison of Flink and Spark based on their performance benchmarks and scalability. Discover how they handle processing speed, in-memory computing, resource management, and more.
Processing Speed: Flink excels in low-latency, high-throughput stream processing, while Spark is known for its fast batch processing capabilities. Both frameworks can process large volumes of data quickly, with Flink focusing on real-time analytics and Spark catering to batch data processing tasks.
In-Memory Computing: Both Flink and Spark leverage in-memory computing, which allows them to cache intermediate results during data processing tasks. This approach significantly reduces the time spent on disk I/O operations and improves overall performance.
Resource Management: Flink and Spark can efficiently manage resources by dynamically allocating and deallocating them according to workload requirements. This enables both frameworks to scale horizontally, handling large-scale data processing tasks across multiple nodes in a distributed environment.
Adaptive Query Execution: Spark's Adaptive Query Execution (AQE) feature optimizes query execution plans at runtime, allowing it to adapt to changing data and workload characteristics. This results in improved performance and resource utilization. Flink, on the other hand, does not currently have an equivalent feature.
Backpressure Handling: Flink is designed to handle backpressure, ensuring that the system remains stable even under high loads. This is achieved through its built-in flow control mechanisms, which prevent data processing bottlenecks. Spark Streaming, in contrast, may struggle to handle backpressure, leading to potential performance degradation.
Data Partitioning: Both Flink and Spark utilize data partitioning techniques to improve parallelism and optimize resource utilization during data processing tasks. While Spark employs RDDs and data partitioning strategies like Hash and Range partitioning, Flink uses operator chaining and pipelined execution to optimize data processing performance.
Recommendations for choosing the right tool for specific use cases
Set up Kafka
Here is a quick guide on how to run and use Kafka locally.
Clone repository: git clone https://github.com/wurstmeister/kafka-docker.git
Change directory into that repository: cd kafka-docker
Edit the docker-compose.yml
file to match this:
Start Docker:
docker-compose up
Start a terminal session in the running container:
Create a topic:
List all available topics in Kafka instance:
Start a producer on topic named
test
:
Send messages to the topic named
test
by typing the following in the terminal:
Open another terminal and start a consumer on the topic named
test
:
The output should look something like this:
Setup stream data ingestion in Mage
Run the following command to run Docker in network mode:
If the network named
kafka-docker_default
doesn’t exist, create a new network:
Check that it exists:
If not able to connect with Kafka locally in a Docker container using Mage, in a Docker container the follow these steps:
Clone Mage: git clone https://github.com/mage-ai/mage-ai.git
Change directory into Mage: cd mage-ai
Edit the docker-compose.yml file to match this:
Run the following script in terminal:
./scripts/dev.sh
This will run Mage in development mode, which runs it in a Docker container using docker compose instead of docker run.
Create streaming data pipeline
Open Mage in your browser.
Click + New pipeline, then select Streaming.
Add a data loader block, select Kafka, and paste the following:
By default, the
bootstrap_server
is set tolocalhost:9092
, If you’re running Mage in a container, thebootstrap_server
should bekafka:9093
Messages are consumed from source in micro batch mode for better efficiency. The default batch size is 100. You can adjust the batch size in the source config.
Add a transformer block and paste the following:
Add a data exporter block, select
OpenSearch
and paste the following:
Change the
host
to match your OpenSearch domain’s endpoint.Change the
index_name
to match the index you want to export data into.
Test pipeline
Open the streaming pipeline you just created, and in the right side panel near the bottom, click the button Execute pipeline to test the pipeline.
You should see an output like this:
Publish messages using Python
Open a terminal on your local workstation.
Install kafka-python
:
Open a Python shell and write the following code to publish messages:
Once you run the code snippet above, go back to your streaming pipeline in Mage and the output should look like this:
Consume messages using Python
If you want to programmatically consume messages from a Kafka topic, here is a code snippet:
Run in production
If you want to programmatically consume messages from a Kafka topic, here is a code snippet:
Once the trigger is created, click the Start trigger button at the top of the page to make the streaming pipeline active.
Conclusion
In conclusion, Mage is an exceptional tool for stream data processing, adept at managing data from various sources and transforming it through real-time and batch pipelines using Python, SQL, and R. It stands out in its capacity to efficiently handle thousands of pipelines simultaneously, ensuring smooth operations and data integrity. Given the increasing need for real-time data processing in today's data-driven world, Mage is positioned as a vital tool in the arsenal of data professionals. Its versatility and robust capabilities make it a reliable choice for handling complex and voluminous streaming data.
Shashank Mishra, Data Engineer @ Expedia
May 30, 2023