Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
spark:spark-introduction [2014/10/03 10:56] straka |
spark:spark-introduction [2022/12/14 13:28] straka [Running Spark Shell in Python] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Spark Introduction ====== | ====== Spark Introduction ====== | ||
- | ===== Spark Introduction | + | This introduction shows several simple examples to give you an idea what programming |
- | To run interactive Python shell in local Spark mode, run (on your local workstation or on cluster) | + | ===== Running |
- | IPYSPARK=1 pyspark | + | |
- | The IPYSPARK=1 parameter instructs | + | |
- | After a local Spark executor is started, the Python shell starts. | + | To run interactive Python shell in local Spark mode, run (on your local workstation or on cluster using '' |
- | | + | |
+ | The PYSPARK_DRIVER_PYTHON=ipython3 parameter instructs Spark to use '' | ||
+ | After a local Spark executor is started, the Python shell starts. Several lines above | ||
+ | the prompt line, the Spark UI address is listed in the following format: | ||
+ | Spark context Web UI available at http:// | ||
+ | The Spark UI is an HTML interface, which 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. | ||
- | ===== Spark Introduction | + | ==== Running Spark Shell in Scala ==== |
+ | |||
+ | To run interactive Scala shell in local Spark mode, run (on your local workstation or on cluster) | ||
+ | spark-shell | ||
+ | Once again, the SparkUI address is listed several lines above the shell prompt line. | ||
+ | |||
+ | |||
+ | ===== Word Count Example ===== | ||
+ | |||
+ | The central object of Spark framework is RDD -- resilient distributed dataset. It contains ordered sequence of items, which may be distributed | ||
+ | |||
+ | 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> | ||
+ | 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_count: word_count[1], | ||
+ | sorted.saveAsTextFile(" | ||
+ | |||
+ | # Alternatively, | ||
+ | (sc.textFile("/ | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | The output of ' | ||
+ | |||
+ | Note that '' | ||
+ | |||
+ | The Scala versions is quite similar: | ||
+ | <file scala> | ||
+ | val wiki = sc.textFile("/ | ||
+ | val words = wiki.flatMap(line => line.split(" | ||
+ | val counts = words.map(word => (word, 1)).reduceByKey((c1, | ||
+ | val sorted = counts.sortBy({case (word, count) => count}, ascending=false) | ||
+ | sorted.saveAsTextFile(" | ||
+ | |||
+ | // Alternatively without variables and using placeholders in lambda parameters: | ||
+ | (sc.textFile("/ | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== K-Means Example ===== | ||
+ | An example implementing [[http:// | ||
+ | <file python> | ||
+ | import numpy as np | ||
+ | |||
+ | def closestPoint(point, | ||
+ | return min((np.sum((point - centers[i]) ** 2), i) for i in range(len(centers)))[1] | ||
+ | |||
+ | lines = sc.textFile("/ | ||
+ | data = lines.map(lambda line: np.array(map(float, | ||
+ | |||
+ | K = 100 | ||
+ | epsilon = 1e-3 | ||
+ | |||
+ | centers = data.takeSample(False, | ||
+ | for i in range(5): | ||
+ | old_centers = sc.broadcast(centers) | ||
+ | centers = (data | ||
+ | # For each point, find its closest center index. | ||
+ | | ||
+ | # Sum points and counts in each cluster. | ||
+ | | ||
+ | # Sort by cluster index. | ||
+ | | ||
+ | # Compute the new centers by averaging points in clusters. | ||
+ | | ||
+ | | ||
+ | # If the change in center positions is less than epsilon, stop. | ||
+ | centers_change = sum(np.sqrt(np.sum((a - b)**2)) for (a, b) in zip(centers, | ||
+ | old_centers.unpersist() | ||
+ | if centers_change < epsilon: | ||
+ | break | ||
+ | |||
+ | print "Final centers: " + str(centers) | ||
+ | </ | ||
+ | The implementation starts by loading the data points and caching them in memory using '' | ||
+ | |||
+ | Note that explicit broadcasting used for '' | ||
+ | |||
+ | For illustration, | ||
+ | <file scala> | ||
+ | import breeze.linalg.Vector | ||
+ | |||
+ | type Vector = breeze.linalg.Vector[Double] | ||
+ | type Vectors = Array[Vector] | ||
+ | |||
+ | def closestPoint(point : Vector, centers : Vectors) : Double = | ||
+ | centers.map(center => (center-point).norm(2)).zipWithIndex.min._2 | ||
+ | |||
+ | val lines = sc.textFile("/ | ||
+ | val data = lines.map(line => Vector(line.split(" | ||
+ | |||
+ | val K = 100 | ||
+ | val epsilon = 1e-3 | ||
+ | |||
+ | var i = 0 | ||
+ | var centers_change = Double.PositiveInfinity | ||
+ | var centers = data.takeSample(false, | ||
+ | while (i < 10 && centers_change > epsilon) { | ||
+ | val old_centers = sc.broadcast(centers) | ||
+ | centers = (data | ||
+ | // For each point, find its closes center index. | ||
+ | | ||
+ | // Sum points and counts in each cluster. | ||
+ | | ||
+ | // Sort by cluster index. | ||
+ | | ||
+ | // Compute the new centers by averaging corresponding points. | ||
+ | | ||
+ | | ||
+ | |||
+ | // Compute change in center positions. | ||
+ | centers_change | ||
+ | old_centers.unpersist() | ||
+ | i += 1 | ||
+ | } | ||
+ | |||
+ | print(centers.deep) | ||
+ | </ | ||