RDD Operations (Scala)

Launch Spark

$ $SPARK_HOME/bin/spark-shell

Working with textual data

Create a directory in HDFS /user/cloudera/Shakespeare.

Download complete works of Shakespeare from gutenberg.org and upload to the above directory.

val shakespeare = sc.textFile(“/user/cloudera/Shakespeare”)


Load stop words

import scala.io.Source

val stopWords = Source.fromURL("http://www.textfixer.com/tutorials/common-english-words.txt").mkString.split(",")

stopWords: Array[String] = Array(a, able, about, across, after, all, almost, also, am, among, an, and, any, are, as, at, be, because, been, but, by, can, cannot, could, dear, did, do, does, either, else, ever, every, for, from, get, got, had, has, have, he, her, hers, him, his, how, however, i, if, in, into, is, it, its, just, least, let, like, likely, may, me, might, most, must, my, neither, no, nor, not, of, off, often, on, only, or, other, our, own, rather, said, say, says, she, should, since, so, some, than, that, the, their, them, then, there, these, they, this, tis, to, too, twas, us, wants, was, we, were, what, when, where, which, while, who, whom, why, will, with, would, yet, you, your)

Show top 100 most frequently used words. Ignore cases and remove stop words and words having single letter.


.flatMap{_.toLowerCase.split("\\W+").filter(word => word.length > 1 && !stopWords.contains(word))}

.map(word => (word, 1))


.sortBy(p => p._2, false)























Stocks Datasets

Download the dataset from here and unzip it and load it to HDFS location /user/cloudera/stocks

Find top 10 most traded stocks by volume in 2016.

val stocks = sc.textFile("/user/cloudera/stocks")

stocks.sample(false, 0.00001, 100).take(10).foreach(println)














.map(tokens => (tokens(7), tokens(5).toDouble))


.mapValues(values => values.sum/values.size)

.sortBy(_._2, false)













Which top 10 stocks have given highest returns as of July 1st, 2016 calculated over a year.

val stocks2016 = stocks


.map{line =>

val tokens = line.split(",")

(tokens(7), tokens(6).toDouble)


val stocks2015 = stocks


.map{line =>

val tokens = line.split(",")

(tokens(7), tokens(6).toDouble)


val stocksJoined = stocks2015.join(stocks2016)


.map(t => (t._1, (t._2._2 - t._2._1) / t._2._1))

.sortBy(_._2, false)














MovieLens Dataset

Download the dataset from https://grouplens.org/datasets/movielens/

Find top 10 movies based on average rating. Consider only those movies that have received 100 or more ratings. Include movie title in the output.

  • Load movies.csv as movies and ratings.csv as ratings RDD

  • Extract the title field from movies RDD. Note that some title contains comma in them. You can use a univocity library to parse.

  • Find average rating and rating count for each movieId from ratings RDD

  • Filter out all records that have fewer than 100 rating counts

  • Sort the data by average rating descending order and take top 10 records.

  • Join this with movies to include title in this dataset.

  • Finally save the output to HDFS. Include movieId, title, average rating

val ratings = sc




.map(tokens => (tokens(1), tokens(2).toFloat))












val ratingAvg = ratings


.mapValues(values => (values.sum / values.size, values.size))

.filter(_._2._2 >= 100)

.map(p => (p._1, p._2._1))












val movies = sc




.map(tokens => (tokens(0), tokens(1)))


(1,Toy Story (1995))

(2,Jumanji (1995))

(3,Grumpier Old Men (1995))

(4,Waiting to Exhale (1995))

(5,Father of the Bride Part II (1995))

(6,Heat (1995))

(7,Sabrina (1995))

(8,Tom and Huck (1995))

(9,Sudden Death (1995))

(10,GoldenEye (1995))


(1968,((3.7584746,118),"Breakfast Club))

(1580,((3.627907,172),Men in Black (a.k.a. MIB) (1997)))

ratingAvg.join(movies).sortBy(_._2._1, false).take(10).foreach(println)

(318,(4.4545455,"Shawshank Redemption))


(50,(4.3289475,"Usual Suspects))

(1136,(4.301948,Monty Python and the Holy Grail (1975)))

(527,(4.296371,Schindler's List (1993)))

(1193,(4.2727275,One Flew Over the Cuckoo's Nest (1975)))

(608,(4.2711444,Fargo (1996)))


(1221,(4.260714,"Godfather: Part II))

(1213,(4.259259,Goodfellas (1990)))