⤵️ Quick shortcut: Select the module you’re currently studying ⤵️
Courses Module 1 Module 2 Module 3 Module 4 Module 5 Module 6
Here you will find materials that cover this topic, which we believe are suitable for completing the activity at the end of the lesson. The choice is left to the student's discretion.
Here are some recommended Udemy courses to learn about automating data ingestion and writing scripts for database queries and S3 uploads using Python:
Below you will find some suggested materials on these topics, which we believe may help you complete the final activity in the lesson. Please note that using these resources is entirely optional, and you are welcome to explore any other sources or courses at your own discretion.
Here are some recommended courses to explore:
Introduces how to use Python to automate operational tasks on AWS, including scripts with AWS Systems Manager and S3.
A course focused on automating tasks on AWS using Lambda and Python, including interaction with S3 and other services.
A tutorial detailing how to upload and download files from S3 using Python, with practical examples.
A tutorial detailing how to upload and download files from S3 using Python, with practical examples.
A detailed guide on how to interact with AWS S3 using Python and Boto3, covering common operations.
A guided project teaching the basic functions of AWS S3, including the use of Python to automate tasks.
An introductory course teaching how to work with S3 and Python, including creating buckets and uploading or downloading files.
ng S3.
This course guides you in implementing solutions with S3 using Python and Boto3, focusing on secure, reliable, and scalable cloud applications in AWS.
Learn to work with S3, including uploading large files and automating tasks, using Python and Boto3.
A hands-on course teaching how to develop Python applications in AWS using Boto3, covering services like S3, IAM, and EC2.
✅ Automating data ingestion.
✅ Writing scripts for database queries and S3 uploads.
Build a Python script to automate data ingestion:
Query data from a SQLite database.
Save the queried data as a CSV file locally.
Upload the CSV file to an AWS S3 bucket.
Objective:
Build a Python script to automate data ingestion:
Query data from a SQLite database.
Save the queried data as a CSV file locally.
Upload the CSV file to an AWS S3 bucket.
Prepare the Environment
Install Required Libraries:
bash
Copy code
pip install pandas boto3 sqlite3
Download the Sample Database:
Use the provided employees.db SQLite database or download a similar dataset from Kaggle (e.g., Employee Data).
Set Up AWS S3:
Create an S3 bucket if you haven’t already (e.g., my-data-bucket).
Configure your AWS credentials as described in Lesson 2.
2. Write the Data Ingestion Script
python
Copy code
import sqlite3
import pandas as pd
import boto3
from botocore.exceptions import NoCredentialsError
# Step 1: Query Data from SQLite Database
def fetch_data_from_db(db_path, query):
try:
# Connect to the database
connection = sqlite3.connect(db_path)
print("Connected to SQLite database.")
# Execute query and fetch data
data = pd.read_sql_query(query, connection)
# Close connection
connection.close()
print("Data fetched successfully.")
return data
except Exception as e:
print(f"Error querying the database: {e}")
return None
# Step 2: Save Data Locally as a CSV File
def save_data_as_csv(data, file_path):
try:
data.to_csv(file_path, index=False)
print(f"Data saved locally as '{file_path}'.")
except Exception as e:
print(f"Error saving data as CSV: {e}")
# Step 3: Upload the CSV File to AWS S3
def upload_to_s3(bucket_name, file_name, s3_key):
s3 = boto3.client('s3')
try:
s3.upload_file(file_name, bucket_name, s3_key)
print(f"File '{file_name}' uploaded to S3 bucket '{bucket_name}' as '{s3_key}'.")
except FileNotFoundError:
print(f"File '{file_name}' not found.")
except NoCredentialsError:
print("AWS credentials not found.")
except Exception as e:
print(f"Error uploading to S3: {e}")
# Main Function
if __name__ == "__main__":
# Database and S3 configuration
db_path = "employees.db"
query = "SELECT * FROM employees"
local_csv = "employees_data.csv"
bucket_name = "my-data-bucket"
s3_key = "ingested_data/employees_data.csv"
# Fetch data from database
data = fetch_data_from_db(db_path, query)
# Save data locally as CSV
if data is not None:
save_data_as_csv(data, local_csv)
# Upload CSV to S3
upload_to_s3(bucket_name, local_csv, s3_key)
Query Data from SQLite:
Connects to the SQLite database.
Fetches data using a SQL query.
Converts the result into a Pandas DataFrame for easy manipulation.
Save as CSV:
Saves the DataFrame as a CSV file locally for review or future use.
Upload to AWS S3:
Uses boto3 to upload the CSV file to the specified S3 bucket and key.
Task:
Modify the script to:
Query a different table or dataset.
Save the file with a different name (e.g., new_employees_data.csv).
Upload it to a new folder in the same S3 bucket (e.g., new_ingested_data/).
Deliverable:
A Python script that successfully ingests data and uploads it to S3.
Courses:
Udemy: Python Programming Masterclass
Udemy: Data Analysis with Pandas
Datasets:
Kaggle Employee Data
Sample SQLite database provided in the course.
Documentation:
SQLite Official Documentation
Pandas Documentation
AWS boto3 Documentation
A local CSV file (e.g., employees_data.csv) saved with the queried data.
The CSV file uploaded to the S3 bucket in the specified folder.
Automate the process of querying data from a database.
Save data in a structured format (CSV) for further analysis.
Seamlessly upload data to AWS S3 for cloud-based storage and workflows.
Use your script to upload a new dataset to your bucket. This assignment builds on the concepts covered in Lesson 3 and focuses on applying them to a new dataset. The goal is to automate the ingestion and upload of a different dataset to your AWS S3 bucket.
Modify your existing script to:
Query a new dataset from a database.
Save the queried data as a CSV file.
Upload the CSV file to a specified folder in your S3 bucket.
1. Select or Create a New Dataset
Option 1: Use a Provided Dataset:
Download a sample dataset, such as the Kaggle Employee Data.
If the dataset is in CSV format, import it into a SQLite database for consistency with the activity.
Example: Importing a CSV into SQLite:
bash
Copy code
sqlite3 new_employees.db
.mode csv
.import new_employees.csv employees
.quit
Option 2: Create Your Own Dataset:
Use any dataset with similar fields (e.g., name, role, department).
Add the data to a new table in the SQLite database.
2. Modify the Script
Update the query to extract data from the new table.
Change the file name for the CSV and the destination key in the S3 bucket.
Example Script
python
Copy code
import sqlite3
import pandas as pd
import boto3
from botocore.exceptions import NoCredentialsError
# Function to fetch data from the new dataset
def fetch_new_data_from_db(db_path, query):
try:
connection = sqlite3.connect(db_path)
print("Connected to SQLite database.")
# Execute the query
data = pd.read_sql_query(query, connection)
connection.close()
print("New data fetched successfully.")
return data
except Exception as e:
print(f"Error querying the database: {e}")
return None
# Function to save data locally as a CSV file
def save_new_data_as_csv(data, file_path):
try:
data.to_csv(file_path, index=False)
print(f"New data saved locally as '{file_path}'.")
except Exception as e:
print(f"Error saving data as CSV: {e}")
# Function to upload the new CSV file to S3
def upload_new_csv_to_s3(bucket_name, file_name, s3_key):
s3 = boto3.client('s3')
try:
s3.upload_file(file_name, bucket_name, s3_key)
print(f"File '{file_name}' uploaded to S3 bucket '{bucket_name}' as '{s3_key}'.")
except FileNotFoundError:
print(f"File '{file_name}' not found.")
except NoCredentialsError:
print("AWS credentials not found.")
except Exception as e:
print(f"Error uploading to S3: {e}")
# Main function
if __name__ == "__main__":
# Database and S3 configuration
db_path = "new_employees.db" # New database file
query = "SELECT * FROM employees" # New query
local_csv = "new_employees_data.csv" # New CSV file name
bucket_name = "my-data-bucket"
s3_key = "new_ingested_data/new_employees_data.csv" # New folder/key in S3
# Fetch new data from the database
new_data = fetch_new_data_from_db(db_path, query)
# Save the new data locally
if new_data is not None:
save_new_data_as_csv(new_data, local_csv)
# Upload the new CSV to S3
upload_new_csv_to_s3(bucket_name, local_csv, s3_key)
1. Run the Script
Execute the script to ensure that the new dataset is successfully queried, saved, and uploaded to S3:
bash
Copy code
python upload_new_dataset.py
2. Verify the Output
Local File:
Check that the new CSV file (new_employees_data.csv) is created locally with the correct data.
S3 Bucket:
Log in to the AWS Management Console.
Navigate to your S3 bucket and locate the new_ingested_data/ folder.
Verify that the new file (new_employees_data.csv) is present.
Script:
Submit the updated Python script.
CSV File:
Provide the name of the locally saved CSV file.
S3 Confirmation:
Screenshot or log showing successful upload to S3.
Implement logging to track each step of the script execution.
Add error handling to manage cases like:
Missing data.
Invalid queries.
Connection issues with S3.
Modify and adapt Python scripts for different datasets.
Save and upload data to S3 programmatically.
Ensure automation of data ingestion workflows for scalability.