forked from snchit/Advanced-Data-Science-with-IBM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment2.1.py
91 lines (48 loc) · 3.76 KB
/
assignment2.1.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python
# coding: utf-8
# ### Assignment 2
# Welcome to Assignment 2. This will be fun. It is the first time you actually access external data from ApacheSpark.
#
# Just make sure you hit the play button on each cell from top to down. There are three functions you have to implement. Please also make sure than on each change on a function you hit the play button again on the corresponding cell to make it available to the rest of this notebook.
#
# ##### Please also make sure to only implement the function bodies and DON'T add any additional code outside functions since this might confuse the autograder.
# So the function below is used to make it easy for you to create a data frame from a Cloud Object Store data frame using the so called "DataSource" which is some sort of a plugin which allows ApacheSpark to use different data sources.
# This is the first function you have to implement. You are passed a dataframe object. We've also registered the dataframe in the ApacheSparkSQL catalog - so you can also issue queries against the "washing" table using "spark.sql()". Hint: To get an idea about the contents of the catalog you can use: spark.catalog.listTables().
# So now it's time to implement your first function. You are free to use the dataframe API, SQL or RDD API. In case you want to use the RDD API just obtain the encapsulated RDD using "df.rdd". You can test the function by running one of the three last cells of this notebook, but please make sure you run the cells from top to down since some are dependant of each other...
# In[1]:
#Please implement a function returning the number of rows in the dataframe
def count(df,spark):
x1 = df.count()
return x1
# Now it's time to implement the second function. Please return an integer containing the number of fields. The most easy way to get this is using the dataframe API. Hint: You might find the dataframe API documentation useful: http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.DataFrame
# In[2]:
def getNumberOfFields(df,spark):
x2 = len(df.columns)
return x2
# Finally, please implement a function which returns a (python) list of string values of the field names in this data frame. Hint: Just copy&past doesn't work because the auto-grader will create a random data frame for testing, so please use the data frame API as well. Again, this is the link to the documentation: http://spark.apache.org/docs/latest/api/python/pyspark.sql.html#pyspark.sql.DataFrame
# In[3]:
def getFieldNames(df,spark):
x3 = df.columns
return x3
# ### PLEASE DON'T REMOVE THIS BLOCK - THE FOLLOWING CODE IS NOT GRADED
# #axx
# ### PLEASE DON'T REMOVE THIS BLOCK - THE FOLLOWING CODE IS NOT GRADED
# So after this block you can basically do what you like since the auto-grader is not considering this part of the notebook
# Now it is time to grab a PARQUET file and create a dataframe out of it. Using SparkSQL you can handle it like a database.
# In[4]:
get_ipython().system('wget https://github.com/IBM/coursera/blob/master/coursera_ds/washing.parquet?raw=true')
get_ipython().system('mv washing.parquet?raw=true washing.parquet')
# In[5]:
df = spark.read.parquet('washing.parquet')
df.createOrReplaceTempView('washing')
df.show()
# The following cell can be used to test your count function
# In[6]:
count(df,spark)
# The following cell can be used to test your getNumberOfFields function
# In[7]:
getNumberOfFields(df,spark)
# The following cell can be used to test your getFieldNames function
# In[8]:
getFieldNames(df,spark)
# Congratulations, you are done. So please export this notebook as python script using the "File->Download as->Python (.py)" option in the menue of this notebook. This file should be named "assignment2.1.py" and uploaded to the autograder of week2.