[ Skip to the content ]

Institute of Formal and Applied Linguistics Wiki


[ Back to the navigation ]

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
spark:spark-introduction [2022/12/14 12:27]
straka [Spark Introduction]
spark:spark-introduction [2022/12/14 12:34]
straka [Word Count Example]
Line 6: Line 6:
  
 To run interactive Python shell in local Spark mode, run (on your local workstation or on cluster using ''qrsh'' from ''lrc1'') To run interactive Python shell in local Spark mode, run (on your local workstation or on cluster using ''qrsh'' from ''lrc1'')
-  IPYTHON=pyspark +  PYSPARK_DRIVER_PYTHON=ipython3 pyspark 
-The IPYTHON=parameter instructs Spark to use ''ipython'' instead of ''python'' (the ''ipython'' is an enhanced interactive shell than Python). If you do not want ''ipython'' or you do not have it installed (it is installed everywhere on the cluster, but maybe not on your local workstations -- ask our IT if you want it), use only ''pyspark'', but note that it has some issues when copy-pasting examples from this wiki.+The PYSPARK_DRIVER_PYTHON=ipython3 parameter instructs Spark to use ''ipython3'' instead of ''python3''.
  
-After a local Spark executor is started, the Python shell starts. Severel lines above +After a local Spark executor is started, the Python shell starts. Several lines above 
-the prompt line, the SparkUI address is listed in the following format: +the prompt line, the Spark UI address is listed in the following format: 
-  14/10/03 10:54:35 INFO SparkUI: Started SparkUI at http://tauri4.ufal.hide.ms.mff.cuni.cz:4040 +  Spark context Web UI available at http://hyperion7.ufal.hide.ms.mff.cuni.cz:4040 
-The SparkUI is an HTML interface which displays the state of the application -- if a distributed computation is taking place, how many workers are part of it, how many tasks are left to be processed, any error logs, also cached datasets and their properties (cached on disk / memory, their size) are displayed.+The Spark UI is an HTML interfacewhich displays the state of the application -- whether a distributed computation is taking place, how many workers are part of it, how many tasks are left to be processed, any error logs, also cached datasets and their properties (cached on disk / memory, their size) are displayed.
  
 ==== Running Spark Shell in Scala ==== ==== Running Spark Shell in Scala ====
Line 27: Line 27:
 We start by simple word count example. 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. Copy the following to the opened Python shell: We start by simple word count example. 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. Copy the following to the opened Python shell:
 <file python> <file python>
-wiki = sc.textFile("/net/projects/spark-example-data/wiki-cs")+wiki = sc.textFile("/lnet/troja/data/npfl118/wiki/cs/wiki.txt")
 words = wiki.flatMap(lambda line: line.split()) words = wiki.flatMap(lambda line: line.split())
-counts = words.map(lambda word: (word, 1)).reduceByKey(lambda c1,c2: c1+c2) +counts = words.map(lambda word: (word, 1)).reduceByKey(lambda c1, c2: c1+c2) 
-sorted = counts.sortBy(lambda (word,count)count, ascending=False) +sorted = counts.sortBy(lambda word_countword_count[1], ascending=False) 
-sorted.saveAsTextFile('output')+sorted.saveAsTextFile("output")
  
 # Alternatively, we can avoid variables: # Alternatively, we can avoid variables:
-(sc.textFile("/net/projects/spark-example-data/wiki-cs")+(sc.textFile("/lnet/troja/data/npfl118/wiki/cs/wiki.txt")
    .flatMap(lambda line: line.split())    .flatMap(lambda line: line.split())
    .map(lambda word: (word, 1))    .map(lambda word: (word, 1))
-   .reduceByKey(lambda c1,c2: c1+c2) +   .reduceByKey(lambda c1, c2: c1+c2) 
-   .sortBy(lambda (word,count)count, ascending=False)+   .sortBy(lambda word_countword_count[1], ascending=False)
    .take(10)) # Instead of saveAsTextFile, we only print 10 most frequent words    .take(10)) # Instead of saveAsTextFile, we only print 10 most frequent words
 </file> </file>

[ Back to the navigation ] [ Back to the content ]