This project is part of the Udacity Data Analyst Nanodegree program and involves designing a database schema and ETL pipeline for a music streaming startup called Sparkify.
- Purpose and Analytical Goals
- Project Overview
- Project Datasets
- ETL Pipeline
- Project Steps
- Files
- Software and Tools
- Queries
- Conclusion
The purpose of the Sparkify database is to provide insight into user behavior on the startup music streaming platform. The database schema consists of a fact table and several dimension tables to make querying and summarizing data easy. The ETL process extracts and transforms JSON data into a format that can be loaded into the database using Python and SQL. The goal of this project is to create a database schema and ETL pipeline that will enable Sparkify to analyze user behavior and improve the overall user experience on their music streaming platform.
A startup called Sparkify wants to analyze the data they've been collecting on songs and user activity on their new music streaming app. The analytics team is particularly interested in understanding what songs users are listening to. Currently, they don't have an easy way to query their data, which resides in a directory of JSON logs on user activity on the app, as well as a directory with JSON metadata on the songs in their app.
They'd like a data engineer to create a Postgres database with tables designed to optimize queries on song play analysis, and bring you on the project. Your role is to create a database schema and ETL pipeline for this analysis. You'll be able to test your database and ETL pipeline by running queries given to you by the analytics team from Sparkify and compare your results with their expected results.
In this project, you'll apply what you've learned on data modeling with Postgres and build an ETL pipeline using Python. To complete the project, you will need todefine fact and dimension tables for a star schema for a particular analytic focus, and write an ETL pipeline that transfers data from files in two local directories into these tables in Postgres using Python and SQL.
The first dataset is a subset of real data from the Million Song Dataset. Each file is in JSON format and contains metadata about a song and the artist of that song. The files are partitioned by the first three letters of each song's track ID.
The database schema is designed as a star schema with a fact table (songplays) and several dimension tables (users, songs, artists, and time). The fact table contains information about each song play, while the dimension tables contain information about the users, songs, artists, and time periods associated with each song play. This schema allows for easy querying and aggregation of data, as well as quick access to the most relevant information for analysis.
Using the song and log datasets, you'll need to create a star schema optimized for queries on song play analysis. This includes the following tables.
- songplays - records in log data associated with song plays i.e. records with page
NextSong- songplay_id, start_time, user_id, level, song_id, artist_id, session_id, location, user_agent
- users - users of the streaming platform
- user_id, first_name, last_name, gender, level
- songs - songs in music database
- song_id, title, artist_id, year, duration
- artists - artists in music database
- artist_id, name, location, latitude, longitude
- time - timestamps of records in songplays broken down into specific units
- start_time, hour, day, week, month, year, weekday
The ETL pipeline consists of two main functions: process_song_file and process_log_file. The process_song_filefunction extracts data on songs and artists from the JSON files and inserts it into thesongsandartiststables, while theprocess_log_filefunction extracts data on user activity and inserts it into thesongplays,users, and time` tables. The ETL pipeline is designed to extract data from JSON files, transform it into the appropriate format, and load it into the database using SQL queries. The pipeline is designed to be scalable and can handle large amounts of data.
Below are steps you can follow to complete the project:
- Write
CREATEstatements insql_queries.pyto create each table. - Write
DROPstatementsinsql_queries.pyto drop each table if it exists. - Run
create_tables.pyto create the database and tables.
- Implement
process_song_filefunction inetl.pyto process song files. - Implement
process_log_filefunction inetl.pyto process log files. - Test
process_song_fileandprocess_log_filefunctions by runningetl.ipynb.
- Implement
mainfunction inetl.pyto perform ETL pipeline. - Run
etl.pyto extract data from JSON files, transform it, and load it into the database.
Do the following steps in your README.md file.
- Discuss the purpose of this database in the context of the startup, Sparkify, and their analytical goals.
- State and justify your database schema design and ETL pipeline.
- [Optional] Provide example queries and results for song play analysis.
==NOTE: You will not be able to run test.ipynb, etl.ipynb, or etl.py until you have run create_tables.py at least once to create the sparkifydb database, which these other files connect to.==
To run the ETL pipeline, follow these steps:
- Run
create_tables.pyto create the database and tables. - Run
etl.pyto extract data from JSON files, transform it, and load it into the database.
The project includes the following files:
| File Name | Description |
|---|---|
create_tables.py |
This script creates the database tables. It drops any existing tables and recreates them fresh. |
etl.ipynb |
This Jupyter notebook is used for prototyping the ETL pipeline. It allows testing extracting data from the JSON files and transforming it before loading into the database. |
test.ipynb |
This Jupyter notebook is used for testing the ETL pipeline. It connects to the database and runs queries to compare the results to expected values. |
etl.py |
This script contains the ETL pipeline that extracts data from the JSON files, transforms it, and loads it into the database. It contains the main functions to process the song and log JSON files. |
sql_queries.py |
This file contains the SQL queries used to create the database tables and insert data into them. The queries are imported and used in the ETL scripts. |
README.md |
This file provides an overview of the project, instructions on how to run the ETL pipeline, and descriptions of the other files. |
| The song and log JSON files | These contain the source data that the ETL pipeline extracts from and loads into the database tables. |
In this project, the following software and tools were used:
- Python 3.6
- PyCharm
- PostgreSQL 15.3
- Jupyter Notebook
- PGAdmin 4
- Anaconda
psycopg2- PostgreSQL database adapter for Pythonpandas- data manipulation libraryos- operating system interfaceglob- filename pattern matchingjson- read and write JSON data
The project was completed on a local machine with the above software and libraries installed. However, the project can also be completed Udacity workspace, which has all the necessary software and tools pre-installed.
- Find the top 10 users who have the most song plays:
SELECT u.first_name || ' ' || u.last_name AS user_name, count(*) AS plays FROM songplays sp JOIN users u ON sp.user_id = u.user_id GROUP BY 1 ORDER BY plays DESC LIMIT 10;
- View total song plays by day:
SELECT time.start_time::date AS date, COUNT(songplays.songplay_id) AS num_songplays FROM songplays JOIN time ON songplays.start_time = time.start_time GROUP BY date ORDER BY date ASC;
- Select the first 5 rows from the artists table.
- Select the first 5 rows from the songs table.
- Select the first 5 rows from the time table.
- Select the first 5 rows from the user table.
In this project, I have designed a database schema and implemented an ETL pipeline for a music streaming startup called Sparkify. I have created a star schema with a fact table and dimension tables, and used Python and SQL to extract, transform, and load data from JSON files into the database. The resulting database and ETL pipeline enable Sparkify to analyze user behavior and improve the overall user experience on their music streaming platform.





