You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* This will become a table of contents (this text will be scraped).
7
7
{:toc}
8
8
9
-
This tutorial provides a quick introduction to using Spark. We will first introduce the API through Spark's interactive Scala shell (don't worry if you don't know Scala -- you will not need much for this), then show how to write standalone applications in Scala, Java, and Python.
9
+
This tutorial provides a quick introduction to using Spark. We will first introduce the API through Spark's
10
+
interactive shell (in Python or Scala),
11
+
then show how to write standalone applications in Java, Scala, and Python.
10
12
See the [programming guide](scala-programming-guide.html) for a more complete reference.
11
13
12
14
To follow along with this guide, first download a packaged release of Spark from the
@@ -17,8 +19,12 @@ you can download a package for any version of Hadoop.
17
19
18
20
## Basics
19
21
20
-
Spark's interactive shell provides a simple way to learn the API, as well as a powerful tool to analyze datasets interactively.
21
-
Start the shell by running the following in the Spark directory.
22
+
Spark's shell provides a simple way to learn the API, as well as a powerful tool to analyze data interactively.
23
+
It is available in either Scala (which runs on the Java VM and is thus a good way to use existing Java libraries)
24
+
or Python. Start it by running the following in the Spark directory:
25
+
26
+
<divclass="codetabs">
27
+
<divdata-lang="scala"markdown="1">
22
28
23
29
./bin/spark-shell
24
30
@@ -33,7 +39,7 @@ RDDs have _[actions](scala-programming-guide.html#actions)_, which return values
33
39
34
40
{% highlight scala %}
35
41
scala> textFile.count() // Number of items in this RDD
36
-
res0: Long = 74
42
+
res0: Long = 126
37
43
38
44
scala> textFile.first() // First item in this RDD
39
45
res1: String = # Apache Spark
@@ -53,12 +59,53 @@ scala> textFile.filter(line => line.contains("Spark")).count() // How many lines
53
59
res3: Long = 15
54
60
{% endhighlight %}
55
61
62
+
</div>
63
+
<divdata-lang="python"markdown="1">
64
+
65
+
./bin/pyspark
66
+
67
+
Spark's primary abstraction is a distributed collection of items called a Resilient Distributed Dataset (RDD). RDDs can be created from Hadoop InputFormats (such as HDFS files) or by transforming other RDDs. Let's make a new RDD from the text of the README file in the Spark source directory:
68
+
69
+
{% highlight python %}
70
+
>>> textFile = sc.textFile("README.md")
71
+
{% endhighlight %}
72
+
73
+
RDDs have _[actions](scala-programming-guide.html#actions)_, which return values, and _[transformations](scala-programming-guide.html#transformations)_, which return pointers to new RDDs. Let's start with a few actions:
74
+
75
+
{% highlight python %}
76
+
>>> textFile.count() # Number of items in this RDD
77
+
126
78
+
79
+
>>> textFile.first() # First item in this RDD
80
+
u'# Apache Spark'
81
+
{% endhighlight %}
82
+
83
+
Now let's use a transformation. We will use the [`filter`](scala-programming-guide.html#transformations) transformation to return a new RDD with a subset of the items in the file.
84
+
85
+
{% highlight python %}
86
+
>>> linesWithSpark = textFile.filter(lambda line: "Spark" in line)
87
+
{% endhighlight %}
88
+
89
+
We can chain together transformations and actions:
90
+
91
+
{% highlight python %}
92
+
>>> textFile.filter(lambda line: "Spark" in line).count() # How many lines contain "Spark"?
93
+
15
94
+
{% endhighlight %}
95
+
96
+
</div>
97
+
</div>
98
+
99
+
56
100
## More on RDD Operations
57
101
RDD actions and transformations can be used for more complex computations. Let's say we want to find the line with the most words:
58
102
103
+
<divclass="codetabs">
104
+
<divdata-lang="scala"markdown="1">
105
+
59
106
{% highlight scala %}
60
107
scala> textFile.map(line => line.split(" ").size).reduce((a, b) => if (a > b) a else b)
61
-
res4: Long = 16
108
+
res4: Long = 15
62
109
{% endhighlight %}
63
110
64
111
This first maps a line to an integer value, creating a new RDD. `reduce` is called on that RDD to find the largest line count. The arguments to `map` and `reduce` are Scala function literals (closures), and can use any language feature or Scala/Java library. For example, we can easily call functions declared elsewhere. We'll use `Math.max()` function to make this code easier to understand:
@@ -68,26 +115,69 @@ scala> import java.lang.Math
68
115
import java.lang.Math
69
116
70
117
scala> textFile.map(line => line.split(" ").size).reduce((a, b) => Math.max(a, b))
71
-
res5: Int = 16
118
+
res5: Int = 15
72
119
{% endhighlight %}
73
120
74
121
One common data flow pattern is MapReduce, as popularized by Hadoop. Spark can implement MapReduce flows easily:
75
122
76
123
{% highlight scala %}
77
124
scala> val wordCounts = textFile.flatMap(line => line.split(" ")).map(word => (word, 1)).reduceByKey((a, b) => a + b)
Here, we combined the [`flatMap`](scala-programming-guide.html#transformations), [`map`](scala-programming-guide.html#transformations) and [`reduceByKey`](scala-programming-guide.html#transformations) transformations to compute the per-word counts in the file as an RDD of (String, Int) pairs. To collect the word counts in our shell, we can use the [`collect`](scala-programming-guide.html#actions) action:
>>> textFile.map(lambda line: len(line.split())).reduce(lambda a, b: a if (a > b) else b)
140
+
15
141
+
{% endhighlight %}
142
+
143
+
This first maps a line to an integer value, creating a new RDD. `reduce` is called on that RDD to find the largest line count. The arguments to `map` and `reduce` are Python [anonymous functions (lambdas)](https://docs.python.org/2/reference/expressions.html#lambda),
144
+
but we can also pass any top-level Python function we want.
145
+
For example, we'll define a `max` function to make this code easier to understand:
Here, we combined the [`flatMap`](scala-programming-guide.html#transformations), [`map`](scala-programming-guide.html#transformations) and [`reduceByKey`](scala-programming-guide.html#transformations) transformations to compute the per-word counts in the file as an RDD of (string, int) pairs. To collect the word counts in our shell, we can use the [`collect`](scala-programming-guide.html#actions) action:
Spark also supports pulling data sets into a cluster-wide in-memory cache. This is very useful when data is accessed repeatedly, such as when querying a small "hot" dataset or when running an iterative algorithm like PageRank. As a simple example, let's mark our `linesWithSpark` dataset to be cached:
0 commit comments