
40:21
Twitter Data Pipeline using Airflow for Beginners | Data Engineering Project
Darshil Parmar
Overview
This video guides beginners through building a data pipeline to extract data from Twitter, transform it, and store it in Amazon S3 using Apache Airflow. It covers prerequisites like Python and AWS accounts, explains core Airflow concepts such as DAGs and operators, and walks through the practical steps of setting up an EC2 instance, installing Airflow, writing the Python ETL script, creating the Airflow DAG, and configuring AWS permissions for S3 integration. The project culminates in a successfully running data pipeline orchestrated by Airflow.
How was this?
Save this permanently with flashcards, quizzes, and AI chat
Chapters
- The project involves extracting data from Twitter using Python, transforming it, and storing it in Amazon S3, orchestrated by Apache Airflow on an AWS EC2 instance.
- Essential prerequisites include a laptop with internet, Python (version 3.5+), basic Python knowledge, and an AWS account (free trial available).
- A crucial non-technical prerequisite is discipline, as encountering and troubleshooting errors is expected during the project.
- Resources like Google, Stack Overflow, and a Discord channel are recommended for resolving errors.
Understanding the project scope and necessary tools upfront ensures learners are prepared and can gather the required resources before diving into the technical implementation.
The speaker mentions using the 'tweepy' Python package to extract data from Twitter and 'pandas' for data transformation.
- Apache Airflow is an open-source platform for managing data engineering workflows, allowing users to build, schedule, and monitor data pipelines.
- A workflow in Airflow is represented as a Directed Acyclic Graph (DAG), which is a sequence of tasks.
- A task is the basic unit of operation within a DAG, such as extracting, transforming, or loading data.
- Operators are pre-defined templates used to create tasks, with examples including BashOperator and PythonOperator.
Grasping Airflow's core concepts like DAGs, tasks, and operators is fundamental to understanding how data pipelines are structured, scheduled, and executed within the platform.
The speaker shows the Airflow UI, highlighting example DAGs and explaining that individual components within a DAG are called tasks.
- To extract data from Twitter, users need to sign up for a Twitter developer account to obtain API credentials (API key, API secret key, access token, access token secret).
- The 'tweepy' Python library is used to authenticate with the Twitter API using the obtained credentials.
- The `user_timeline` function in 'tweepy' can fetch tweets for a specific user, with parameters to control the number of tweets and whether to include retweets.
- The extracted raw JSON data can be transformed into a structured Pandas DataFrame, including details like username, tweet text, favorite count, and creation date.
Successfully accessing the Twitter API and extracting raw data is the crucial first step in the data pipeline, enabling subsequent transformation and loading.
The speaker demonstrates obtaining API keys and tokens from the Twitter developer dashboard and then uses `tweepy` to fetch tweets from Elon Musk's timeline, saving the transformed data to a CSV file locally.
- An AWS EC2 instance needs to be launched, preferably in a region close to the user for better performance.
- A key pair is essential for securely connecting to the EC2 instance via SSH.
- Necessary packages like Python 3, Pandas, and s3fs must be installed on the EC2 instance.
- The Airflow server is installed and started using the `airflow standalone` command.
- Security group settings must be adjusted to allow access to the Airflow UI on port 8080.
Deploying Airflow on a cloud server like EC2 makes the data pipeline accessible and runnable remotely, which is a standard practice in data engineering.
The speaker guides the user through launching an EC2 instance, selecting Ubuntu, configuring security groups, connecting via SSH, and installing Airflow using `sudo apt install apache-airflow`.
- The Python ETL script for Twitter data extraction and transformation is encapsulated within a function (`run_etl`).
- A DAG file (`twitter_dag.py`) is created, importing necessary Airflow modules and the `run_etl` function.
- Default arguments for the DAG, such as owner and start date, are defined.
- A PythonOperator is used within the DAG to call the `run_etl` function, defining the task.
- The DAG needs to be placed in the correct directory on the EC2 instance, as specified in the `airflow.cfg` file, for Airflow to recognize it.
Defining and deploying the DAG is the core of orchestrating the data pipeline, allowing Airflow to schedule and manage the execution of the ETL tasks.
The speaker shows how to write the `twitter_dag.py` file, including importing `DAG` and `PythonOperator`, defining default arguments, and calling the `run_twitter_etl` function.
- An S3 bucket is created in AWS to serve as the destination for the transformed data.
- The `s3fs` Python library is used to interact with S3, allowing the Pandas DataFrame to be written directly to the bucket.
- The EC2 instance needs appropriate IAM permissions to write data to the S3 bucket.
- An IAM role with 'S3 Full Access' and 'EC2 Full Access' is created and attached to the EC2 instance to grant these permissions.
- After configuring permissions and restarting the Airflow server, the DAG can be triggered, and the data should appear in the S3 bucket.
Integrating with cloud storage like S3 is essential for scalable and persistent data storage, and correctly configuring permissions ensures the pipeline can securely write data.
The speaker demonstrates creating an S3 bucket, modifying the `twitter_dag.py` to write to `s3://your-bucket-name/twitter_data.csv`, creating an IAM role for the EC2 instance, and verifying the data appears in the S3 bucket after the DAG runs successfully.
Key takeaways
- Data pipelines can be effectively built and automated using Apache Airflow, which provides a robust framework for scheduling and monitoring tasks.
- Python libraries like 'tweepy' and 'pandas' are powerful tools for interacting with APIs and manipulating data, respectively.
- Cloud platforms like AWS offer essential services (EC2 for compute, S3 for storage) that are fundamental for modern data engineering projects.
- Understanding and configuring IAM roles and security groups is critical for enabling secure communication and data transfer between cloud services.
- The process of data engineering often involves troubleshooting errors, making persistence and resourcefulness key to success.
- Encapsulating ETL logic into functions within Python scripts makes them reusable and easier to integrate into Airflow DAGs.
- Airflow's UI provides valuable tools for visualizing DAG structure, monitoring task status, and debugging issues through logs.
Key terms
Apache AirflowData PipelineDAG (Directed Acyclic Graph)TaskOperatorEC2 InstanceAWS S3PythonTweepyPandasIAM RoleSecurity GroupETL (Extract, Transform, Load)
Test your understanding
- What is the primary purpose of Apache Airflow in a data engineering project?
- How does Airflow represent a sequence of tasks, and what is the term for an individual unit of work?
- What are the essential prerequisites for building the Twitter data pipeline as demonstrated in the video?
- Why is it necessary to configure IAM roles and security groups when integrating EC2 instances with S3 buckets?
- How can you ensure that your Python ETL script is properly executed and monitored within the Airflow framework?