# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + editable=true
import pandas as pd

from cassandra.cluster import Cluster
from cql_queries import *

# + editable=true
try:
    cluster = Cluster(['127.0.0.1'])
    session = cluster.connect()
except Exception as e:
    print(e)

# + editable=true
try:
    session.set_keyspace('sparkify')
except Exception as e:
    print(e)

# + [markdown] editable=true
# #### Query 1. Give me the artist, song title and song's length in the music app history that was heard during sessionId = 338, and itemInSession = 4

# + editable=true
try:
    query1_result = session.execute(song_info_by_session_table_select, (338, 4))
    
    query1_result_data = []
    for row in query1_result:
        query1_result_data.append(row)
        
    query1_result_data_df = pd.DataFrame(query1_result_data, columns=query1_result.column_names)
    display(query1_result_data_df)
        
except Exception as e:
    print(e)

# + [markdown] editable=true
# #### Query 2. Give me only the following: name of artist, song (sorted by itemInSession) and user (first and last name) for userid = 10, sessionid = 182

# + editable=true
try:
    query2_result = session.execute(song_user_info_by_user_session_table_select, (10, 182))
    
    query2_result_data = []
    for row in query2_result:
        query2_result_data.append(row)
    
    query2_resul_data_df = pd.DataFrame(query2_result_data, columns=query2_result.column_names)
    display(query2_resul_data_df)
    
except Exception as e:
    print(e)

# + [markdown] editable=true
# #### Query 3. Give me every user name (first and last) in my music app history who listened to the song 'All Hands Against His Own'

# + editable=true
try:
    query3_result = session.execute(user_info_by_given_song_table_select, ('All Hands Against His Own',))
    
    query3_result_data = []
    for row in query3_result:
        query3_result_data.append(row)
        
    query3_result_data_df = pd.DataFrame(query3_result_data, columns=query3_result.column_names)
    display(query3_result_data_df)
    
except Exception as e:
    print(e)

# + editable=true
try:
    session.shutdown()
    cluster.shutdown()
except Exception as e:
    print(e)

# + editable=true


StarcoderdataJupyterScriptsDedupFiltered