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 15:02] straka |
spark:spark-introduction [2014/10/06 11:28] straka |
||
---|---|---|---|
Line 7: | Line 7: | ||
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) | ||
IPYTHON=1 pyspark | IPYTHON=1 pyspark | ||
- | The IPYTHON=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 24: | Line 24: | ||
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: | 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: | ||
- | | + | <file python> |
- | words = wiki.flatMap(lambda line: line.split()) | + | wiki = sc.textFile("/ |
- | counts = words.map(lambda word: (word, 1)).reduceByKey(lambda c1,c2: c1+c2) | + | words = wiki.flatMap(lambda line: line.split()) |
- | sorted = counts.sortBy(lambda (word, | + | counts = words.map(lambda word: (word, 1)).reduceByKey(lambda c1,c2: c1+c2) |
- | sorted.saveAsTextFile(' | + | sorted = counts.sortBy(lambda (word, |
- | + | sorted.saveAsTextFile(' | |
- | # Alternatively, | + | |
- | (sc.textFile("/ | + | # Alternatively, |
- | | + | (sc.textFile("/ |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
+ | | ||
+ | </ | ||
The output of ' | The output of ' | ||
The Scala versions is quite similar: | The Scala versions is quite similar: | ||
- | | + | <file scala> |
- | val words = wiki.flatMap(line => line.split(" | + | val wiki = sc.textFile("/ |
- | val counts = words.map(word => (word, | + | val words = wiki.flatMap(line => line.split(" |
- | val sorted = counts.sortBy({case (word, count) => count}, false) | + | val counts = words.map(word => (word, |
- | sorted.saveAsTextFile(' | + | val sorted = counts.sortBy({case (word, count) => count}, |
- | + | sorted.saveAsTextFile(' | |
- | // Alternatively without variables and using placeholders in lambda parameters: | + | |
- | (sc.textFile("/ | + | // Alternatively without variables and using placeholders in lambda parameters: |
- | | + | (sc.textFile("/ |
- | | + | |
- | | + | |
- | | + | |
+ | | ||
+ | </ | ||