layout | title | type | navigation | ||||
---|---|---|---|---|---|---|---|
global |
Examples |
page singular |
|
These examples give a quick overview of the Spark API. Spark is built on the concept of distributed datasets, which contain arbitrary Java or Python objects. You create a dataset from external data, then apply parallel operations to it. The building block of the Spark API is its RDD API. In the RDD API, there are two types of operations: transformations, which define a new dataset based on previous ones, and actions, which kick off a job to execute on a cluster. On top of Spark’s RDD API, high level APIs are provided, e.g. DataFrame API and Machine Learning API. These high level APIs provide a concise way to conduct certain data operations. In this page, we will show examples using RDD API as well as examples using high level APIs.
In this example, we use a few transformations to build a dataset of (String, Int) pairs called counts
and then save it to a file.
Spark can also be used for compute-intensive tasks. This code estimates π by "throwing darts" at a circle. We pick random points in the unit square ((0, 0) to (1,1)) and see how many fall in the unit circle. The fraction should be π / 4, so we use this to get our estimate.
count = sc.parallelize(range(0, NUM_SAMPLES))
.filter(inside).count()
print("Pi is roughly %f" % (4.0 * count / NUM_SAMPLES))
{% endhighlight %}
long count = sc.parallelize(l).filter(i -> { double x = Math.random(); double y = Math.random(); return xx + yy < 1; }).count(); System.out.println("Pi is roughly " + 4.0 * count / NUM_SAMPLES); {% endhighlight %}
In Spark, a DataFrame is a distributed collection of data organized into named columns. Users can use DataFrame API to perform various relational operations on both external data sources and Spark’s built-in distributed collections without providing specific procedures for processing data. Also, programs based on DataFrame API will be automatically optimized by Spark’s built-in optimizer, Catalyst.
In this example, we search through the error messages in a log file.
df = textFile.map(lambda r: Row(r)).toDF(["line"]) errors = df.filter(col("line").like("%ERROR%"))
errors.count()
errors.filter(col("line").like("%MySQL%")).count()
errors.filter(col("line").like("%MySQL%")).collect() {% endhighlight %}
// Creates a DataFrame having a single column named "line" val df = textFile.toDF("line") val errors = df.filter(col("line").like("%ERROR%")) // Counts all the errors errors.count() // Counts errors mentioning MySQL errors.filter(col("line").like("%MySQL%")).count() // Fetches the MySQL errors as an array of strings errors.filter(col("line").like("%MySQL%")).collect() {% endhighlight %}
DataFrame errors = df.filter(col("line").like("%ERROR%")); // Counts all the errors errors.count(); // Counts errors mentioning MySQL errors.filter(col("line").like("%MySQL%")).count(); // Fetches the MySQL errors as an array of strings errors.filter(col("line").like("%MySQL%")).collect(); {% endhighlight %}
In this example, we read a table stored in a database and calculate the number of people for every age. Finally, we save the calculated result to S3 in the format of JSON. A simple MySQL table "people" is used in the example and this table has two columns, "name" and "age".
df.printSchema()
countsByAge = df.groupBy("age").count() countsByAge.show()
countsByAge.write.format("json").save("s3a://...") {% endhighlight %}
// Looks the schema of this DataFrame. df.printSchema()
// Counts people by age val countsByAge = df.groupBy("age").count() countsByAge.show()
// Saves countsByAge to S3 in the JSON format. countsByAge.write.format("json").save("s3a://...") {% endhighlight %}
// Looks the schema of this DataFrame. df.printSchema();
// Counts people by age DataFrame countsByAge = df.groupBy("age").count(); countsByAge.show();
// Saves countsByAge to S3 in the JSON format. countsByAge.write().format("json").save("s3a://..."); {% endhighlight %}
MLlib, Spark’s Machine Learning (ML) library, provides many distributed ML algorithms. These algorithms cover tasks such as feature extraction, classification, regression, clustering, recommendation, and more. MLlib also provides tools such as ML Pipelines for building workflows, CrossValidator for tuning parameters, and model persistence for saving and loading models.
In this example, we take a dataset of labels and feature vectors. We learn to predict the labels from feature vectors using the Logistic Regression algorithm.
lr = LogisticRegression(maxIter=10)
model = lr.fit(df)
model.transform(df).show() {% endhighlight %}
// Set parameters for the algorithm. // Here, we limit the number of iterations to 10. val lr = new LogisticRegression().setMaxIter(10)
// Fit the model to the data. val model = lr.fit(df)
// Inspect the model: get the feature weights. val weights = model.weights
// Given a dataset, predict each point's label, and show the results. model.transform(df).show() {% endhighlight %}
// Set parameters for the algorithm. // Here, we limit the number of iterations to 10. LogisticRegression lr = new LogisticRegression().setMaxIter(10);
// Fit the model to the data. LogisticRegressionModel model = lr.fit(df);
// Inspect the model: get the feature weights. Vector weights = model.weights();
// Given a dataset, predict each point's label, and show the results. model.transform(df).show(); {% endhighlight %}
Many additional examples are distributed with Spark:
- Basic Spark: Scala examples, Java examples, Python examples
- Spark Streaming: Scala examples, Java examples