- Learn how to extract data from CSV file, get column, conver to dictionary.
- Write simple query to create database.
- Insert data into database
- Fact table: songsplays table
- Dimension table: artists, songs, time, users
In this first part, you'll perform ETL on the first dataset, song_data, to create the songs and artists dimensional tables.
Let's perform ETL on a single song file and load a single record into each table to start.
- Use the
get_filesfunction to get a list of all song JSON files indata/song_data - Select the first song in this list
- Read the song file and view the data
- Select columns for song ID, title, artist ID, year, and duration
- Use
df.valuesto select just the values from the dataframe - Index to select the first (only) record in the dataframe
- Convert the array to a list and set it to
song_data
Implement the song_table_insert query in sql_queries.py and run the cell below to insert a record for this song into the songs table. Remember to run create_tables.py before running the cell below to ensure you've created/resetted the songs table in the sparkify database.
- Select columns for artist ID, name, location, latitude, and longitude
- Use
df.valuesto select just the values from the dataframe - Index to select the first (only) record in the dataframe
- Convert the array to a list and set it to
artist_data
Implement the artist_table_insert query in sql_queries.py and run the cell below to insert a record for this song's artist into the artists table. Remember to run create_tables.py before running the cell below to ensure you've created/resetted the artists table in the sparkify database.
In this part, you'll perform ETL on the second dataset, log_data, to create the time and users dimensional tables, as well as the songplays fact table.
Let's perform ETL on a single log file and load a single record into each table.
- Use the
get_filesfunction provided above to get a list of all log JSON files indata/log_data - Select the first log file in this list
- Read the log file and view the data
- Filter records by
NextSongaction - Convert the
tstimestamp column to datetime - Hint: the current timestamp is in milliseconds
- Extract the timestamp, hour, day, week of year, month, year, and weekday from the
tscolumn and settime_datato a list containing these values in order- Hint: use pandas'
dtattribute to access easily datetimelike properties.
- Hint: use pandas'
- Specify labels for these columns and set to
column_labels - Create a dataframe,
time_df,containing the time data for this file by combiningcolumn_labelsandtime_datainto a dictionary and converting this into a dataframe
Implement the time_table_insert query in sql_queries.py and run the cell below to insert records for the timestamps in this log file into the time table. Remember to run create_tables.py before running the cell below to ensure you've created/resetted the time table in the sparkify database.
Select columns for user ID, first name, last name, gender and level and set to user_df
Implement the user_table_insert query in sql_queries.py and run the cell below to insert records for the users in this log file into the users table. Remember to run create_tables.py before running the cell below to ensure you've created/resetted the users table in the sparkify database.
This one is a little more complicated since information from the songs table, artists table, and original log file are all needed for the songplays table. Since the log file does not specify an ID for either the song or the artist, you'll need to get the song ID and artist ID by querying the songs and artists tables to find matches based on song title, artist name, and song duration time.
- Implement the
song_selectquery insql_queries.pyto find the song ID and artist ID based on the title, artist name, and duration of a song. - Select the timestamp, user ID, level, song ID, artist ID, session ID, location, and user agent and set to
songplay_data
- Implement the
songplay_table_insertquery and run the cell below to insert records for the songplay actions in this log file into thesongplaystable. Remember to runcreate_tables.pybefore running the cell below to ensure you've created/resetted thesongplaystable in the sparkify database.
- data/log_data: CSV log data of songs.
- data/song_data: CSV song data
- run create_tables.py to drop and create new database.
- run etl.py to extract data from csv file.