-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex_spark_sql.py
More file actions
51 lines (41 loc) · 1.4 KB
/
Copy pathindex_spark_sql.py
File metadata and controls
51 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from pyspark.sql import SparkSession
spark_session = SparkSession.builder.appName('Another App name').getOrCreate()
data_set = spark_session.read.json('data/sparkify_log_small.json')
data_set.createOrReplaceTempView('sparkify_log')
# Question 1: Which page did user id ""(empty string) NOT visit?
all_pages = spark_session.sql('''
SELECT DISTINCT page
FROM sparkify_log
''')
user_pages = spark_session.sql( '''
SELECT DISTINCT page
FROM sparkify_log
WHERE userId = ""
''' )
all_pages_list = all_pages.toPandas()['page'].tolist()
user_pages_list = user_pages.toPandas()['page'].tolist()
for page in all_pages_list:
if page not in user_pages_list:
print(page)
continue
# Question 2: How many female users do we have in the data set?
female_users = spark_session.sql( '''
SELECT gender, COUNT(gender) AS gender_count
FROM (
SELECT DISTINCT userId, gender
FROM sparkify_log
)
WHERE gender = 'F'
GROUP BY gender
''' )
female_users.show()
# Question 3: How many songs were played from the most played artist?
most_played_artist = spark_session.sql('''
SELECT artist, COUNT(artist) AS artist_count
FROM sparkify_log
GROUP BY artist
ORDER BY artist_count DESC
LIMIT 1
''')
most_played_artist.show()
# Question 4: How many songs do users listen to on average between visiting our home page? Please round your answer to the closest integer.