Skip to content

Yukei7/Data-Modeling-with-Postgres

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Data-Modeling-with-Postgres

Project Description

The goal of this project is to build an ETL pipeline using Python. To complete the project, you will need to define 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 tables in Postgres using Python and SQL.

Dataset

A startup called Sparkify wants to analyze the data they've been collecting on songs and user activity on their new music streaming app. They are 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.

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.

data/song_data/A/B/C/TRABCEI128F424C983.json
data/song_data/A/A/B/TRAABJL12903CDCF1A.json

And the data looks like

{
    "num_songs": 1, 
    "artist_id": "ARD7TVE1187B99BFB1", 
    "artist_latitude": null, 
    "artist_longitude": null, 
    "artist_location": "California - LA", 
    "artist_name": "Casual", 
    "song_id": "SOQLGFP12A58A7800E", 
    "title": "OAKtown", 
    "duration": 259.44771, 
    "year": 0
}

Log dataset

The dataset consists of log files in JSON format generated by this event simulator based on the songs in the dataset above. These simulate activity logs from a music streaming app based on specified configurations.

The log files in the dataset you'll be working with are partitioned by year and month.

data/log_data/2018/11/2018-11-12-events.json
data/log_data/2018/11/2018-11-13-events.json

And the data looks like

{
    "artist":null,
    "auth":"Logged In",
    "firstName":"Walter",
    "gender":"M",
    "itemInSession":0,
    "lastName":"Frye",
    "length":null,
    "level":"free",
    "location":"San Francisco-Oakland-Hayward, CA",
    "method":"GET",
    "page":"Home",
    "registration":1540919166796.0,
    "sessionId":38,
    "song":null,
    "status":200,
    "ts":1541105830796,
    "userAgent":"\"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/36.0.1985.143 Safari\/537.36\"",
    "userId":"39"
}

Schema for Song Play Analysis

Using the song and log datasets, we create a star schema optimized for queries on song play analysis. The fact and dimension tables are designed as bellow:

erd

Requirements

  • psycopg2
  • pandas

How to run the script

1. Run create_tables.py to create the database and tables

Optional arguments:

-h, --help       show this help message and exit
--host HOST      host address of the database
--dbname DBNAME  name of the databse
--user USER      user you're logging in with
--pw PW          password of the user

Example:

python create_tables.py --host "127.0.0.1" --dbname "studentdb" --user "student" --pw "student"

2. Run etl.py to load JSON datasets into our database

If all the data are loaded with no errors, you will see "Success!" in the prompt.

3. Run the SQL queries to analyse the data

Example Query 1

Find the top 5 heat songs information among paid users.

SELECT songs.title, COUNT(songplays.songplay_id) as counts
FROM (songplays JOIN songs ON songplays.song_id = songs.song_id)
JOIN artists ON songplays.artist_id = artists.artist_id
WHERE songplays.level = "paid"
GROUP BY songs.title
ORDER BY counts DESC
LIMIT 5

For examples, see test.ipynb.

About

Data modeling with Postgres

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published