Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
spark:spark-introduction [2014/10/03 12:56] straka |
spark:spark-introduction [2014/10/03 15:02] straka |
||
---|---|---|---|
Line 6: | Line 6: | ||
To run interactive Python shell in local Spark mode, run (on your local workstation or on cluster) | To run interactive Python shell in local Spark mode, run (on your local workstation or on cluster) | ||
- | | + | |
- | The IPYSPARK=1 parameter instructs Spark to use '' | + | The IPYTHON=1 parameter instructs Spark to use '' |
After a local Spark executor is started, the Python shell starts. | After a local Spark executor is started, the Python shell starts. | ||
Line 15: | Line 15: | ||
To run interactive Scala shell in local Spark mode, run (on your local workstation or on cluster) | To run interactive Scala shell in local Spark mode, run (on your local workstation or on cluster) | ||
- | | + | |
Once again, the SparkUI address is listed several lines above the shell prompt line. | Once again, the SparkUI address is listed several lines above the shell prompt line. | ||
===== Word Count Example ===== | ===== Word Count Example ===== | ||
- | The central object of Spark is RDD -- resilient distributed dataset. It contains ordered sequence of items. | + | |
+ | The central object of Spark framework | ||
+ | |||
+ | Here we load the RDD from text file, every line of the input file becoming an element of RDD. We then split every line into words, count every word occurrence and sort the words by the occurrences. Try the following in the opened Python shell: | ||
+ | wiki = sc.textFile("/ | ||
+ | words = wiki.flatMap(lambda line: line.split()) | ||
+ | counts = words.map(lambda word: (word, 1)).reduceByKey(lambda c1,c2: c1+c2) | ||
+ | sorted = counts.sortBy(lambda (word, | ||
+ | sorted.saveAsTextFile(' | ||
+ | |||
+ | # Alternatively, | ||
+ | (sc.textFile("/ | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | The output of ' | ||
+ | |||
+ | The Scala versions is quite similar: | ||
+ | val wiki = sc.textFile("/ | ||
+ | val words = wiki.flatMap(line => line.split(" | ||
+ | val counts = words.map(word => (word, | ||
+ | val sorted = counts.sortBy({case (word, count) => count}, false) | ||
+ | sorted.saveAsTextFile(' | ||
+ | |||
+ | // Alternatively without variables and using placeholders in lambda parameters: | ||
+ | (sc.textFile("/ | ||
+ | | ||
+ | | ||
+ | | ||
+ | .take(100)) |