4 "cell_type": "markdown",
7 "# Exercise 5 - Spark in Scala _[4 points]_\n",
9 "In this exercise you have to solve the tasks given below. \n"
13 "cell_type": "markdown",
16 "## a) Elementary RDD functions \n",
18 "(Brushing up on the basics of functional programming Scala)\n",
24 "cell_type": "markdown",
27 "#### You are given a list of the first 20 numbers of the Fibbonacci numbers. "
32 "execution_count": null,
36 "val fibs20 = sc.parallelize(List( 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181)) "
40 "cell_type": "markdown",
43 "#### Produce a list that contains all even number from the list 'fibs20':"
48 "execution_count": null,
52 "val evenFibs20 = fibs20.filter(x => (x % 2 == 0)).collect()"
56 "cell_type": "markdown",
59 "#### Compute the average value of the list 'fibs20':"
64 "execution_count": null,
68 "val avg1map = fibs20.map(x => (x, 1))\n",
69 "val avg1fold = avg1map.fold (0,0) ((x,y) => (x._1 + y._1, x._2 + y._2))\n",
70 "val avg1 = avg1fold._1 / avg1fold._2.toFloat"
74 "cell_type": "markdown",
77 "#### Produce a list of that shows for each element of the list 'fibs20' its absolute difference from the average:"
82 "execution_count": null,
86 "val avgDiff = fibs20.map(x => (x - avg1).abs).collect()"
90 "cell_type": "markdown",
93 "#### You are given a random list of words"
98 "execution_count": null,
102 "val words = sc.parallelize(List(\"automaton\", \"language\", \"logic\",\"closure\"))"
106 "cell_type": "markdown",
109 "#### Furthermore, we define a function that maps a word to its list of permutations"
114 "execution_count": null,
118 "def permutate (word:String) = word.permutations.toList"
122 "cell_type": "markdown",
125 "#### Produce a single list containing all permutations of elements from the list 'words':"
130 "execution_count": null,
134 "val wordlist = words.map(word => permutate(word)).flatMap(x => x).collect()"
138 "cell_type": "markdown",
141 "## b) From SQL to Dataframe (and back again)\n",
143 "#### Find for each of the Spark SQL queries an equivalent one that only uses the Dataframe API (or vice versa)\n"
148 "execution_count": null,
152 "val dataPath = \"file:///home/adbs/2019S/shared/diamonds.csv\"\n",
153 "val diamonds = spark.read.format(\"csv\")\n",
154 " .option(\"header\",\"true\")\n",
155 " .option(\"inferSchema\", \"true\")\n",
156 " .load(dataPath)\n",
157 "diamonds.createOrReplaceTempView(\"diamonds\")\n",
159 "val articlesDF = spark.read.format(\"json\").load(\"file:///home/adbs/2019S/shared/spark/nytarticles\")\n",
160 "val commentsDF = spark.read.json(\"file:///home/adbs/2019S/shared/spark/nytcomments\")\n",
161 "articlesDF.createOrReplaceTempView(\"articles\")\n",
162 "commentsDF.createOrReplaceTempView(\"comments\")\n",
163 "// Create RDD view into dataset\n",
164 "val articlesRDD = articlesDF.rdd\n",
165 "val commentsRDD = commentsDF.rdd"
169 "cell_type": "markdown",
172 "#### Query 1: Transform the given Spark SQL query into the Dataframe API"
177 "execution_count": null,
181 "val query1 = spark.sql(\"SELECT COUNT(*) FROM articles WHERE sectionName='Politics'\")\n",
188 "execution_count": null,
192 "//val query1df = query1.toDF()\n",
193 "//articlesDF.where(\"sectionName='Politics'\").count()\n",
194 "articlesDF.where(articlesDF.col(\"sectionName\") === \"Politics\").count()"
198 "cell_type": "markdown",
201 "#### Query 2: Transform the given Dataframe API query into Spark SQL"
206 "execution_count": null,
210 "val query2 = articlesDF.groupBy(\"sectionName\").count()\n",
211 "query2.show(false)\n",
217 "execution_count": null,
221 "val query2sql = spark.sql(\"SELECT sectionName,COUNT(*) FROM articles GROUP BY sectionName\")\n",
222 "query2sql.show(false)\n",
223 "query2sql.explain()"
227 "cell_type": "markdown",
230 "#### Query 3: Transform the given Spark SQL query into the Dataframe API"
235 "execution_count": null,
239 "val query3 = spark.sql(\n",
240 " \"SELECT a.headline, COUNT(c.commentID) AS numComments FROM articles a, comments c WHERE a.articleID = c.articleID GROUP BY a.headline\" )\n",
241 "query3.show(false) // 'false' turns of truncation of row entries\n",
247 "execution_count": null,
251 "val query3df = articlesDF.crossJoin(commentsDF).filter(articlesDF.col(\"articleID\") === commentsDF.col(\"articleID\")).groupBy(articlesDF.col(\"headline\")).agg(count(\"headline\")"
255 "cell_type": "markdown",
258 "#### Query 4: Transform the given Spark SQL query into the Dataframe API"
263 "execution_count": null,
267 "val query4 = spark.sql(\" SELECT headline, byline, pubDate FROM articles WHERE headline RLIKE \\\"2016\\\" \")\n",
268 "query4.show(false)\n",
274 "execution_count": null,
278 "val query4df = articlesDF.filter(articlesDF.col(\"headline\").rlike(\"2016\")).select(articlesDF.col(\"headline\"), articlesDF.col(\"byline\"), articlesDF.col(\"pubDate\"))"
282 "cell_type": "markdown",
285 "#### Query 5: Transform the given Dataframe API query into Spark SQL"
290 "execution_count": null,
294 "val query5 = articlesDF\n",
295 " .join(commentsDF, articlesDF(\"articleID\") === commentsDF(\"articleID\"))\n",
296 " .select(explode(articlesDF(\"keywords\")).as(\"singleKeyWords\"))\n",
297 " .groupBy(\"singleKeyWords\")\n",
298 " .agg(count(\"singleKeyWords\").as(\"number\"))\n",
299 " .orderBy(desc(\"number\"))\n",
300 "query5.show(false)\n",
305 "cell_type": "markdown",
308 "Note here that \"explode\" is a Spark SQL function that turns a tuple with column that contains a collection of objects into multiple tuples each with a single value from this collection. More information here: https://spark.apache.org/docs/2.3.0/api/sql/index.html#explode"
313 "execution_count": null,
317 "val query5sql = spark.sql(\"SELECT singleKeyWords, COUNT(*) AS number FROM (SELECT EXPLODE(keywords) AS singleKeyWords FROM articles JOIN comments ON articles.articleID = comments.articleID) GROUP BY singleKeyWords ORDER BY number DESC\")"
321 "cell_type": "markdown",
324 "### For All Queries Above: \n",
325 "#### Analyze the plans (.explain() ) and compare performance (using the Spark Web UI). Try to reason about any major differences in the logical plans (if there are any)."
329 "cell_type": "markdown",
332 "## c) Wide and Narrow Dependencies\n",
334 "#### Look at the Dataframe queries given as part of b) or for which you wrote the Dataframe version.\n",
336 "#### Use the Spark Internal Web UI to analyse the dependencies and stages of the queries, and try to determine which commands on which Dataframes are executed as wide dependencies and which as narrow dependencies. \n"
341 "execution_count": null,
349 "display_name": "Scala (spylon-kernel)",
351 "name": "spylon-kernel"
354 "codemirror_mode": "text/x-scala",
355 "file_extension": ".scala",
358 "text": "MetaKernel Magics",
359 "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
362 "mimetype": "text/x-scala",
364 "pygments_lexer": "scala",