CS-A1120 Programming 2
Department of Computer Science
Aalto University
(Based on material by Petteri Kaski and Tommi Junttila)
Apache SPARK is picky, and will throw exceptions.
How can data like this be processed?
Scalable distributed computing infrastructure
Warehouse-scale computers
Compute clusters
Computing grids
How can warehouses full of servers be programmed?
Apache Spark RDD Programming Guide
map
reduce
Examples:
map(func) |
Return a new distributed dataset formed by passing each element of the source through a function func . |
filter(func) |
Return a new dataset formed by selecting those elements of the source on which func returns true. |
union(otherDataset) |
Return a new dataset that contains the union of the elements in the source dataset and the argument. |
intersection(otherDataset) |
Return a new RDD that contains the intersection of elements in the source dataset and the argument. |
distinct([numPartitions])) |
Return a new dataset that contains the distinct elements of the source dataset. |
groupByKey([numPartitions]) |
When called on a dataset of (K, V) pairs, returns a dataset of (K, Iterable<V>) pairs. |
sortByKey([ascending], [numPartitions]) |
When called on a dataset of (K, V) pairs where K implements Ordered, returns a dataset of (K, V) pairs sorted by keys in ascending or descending order, as specified in the boolean ascending argument. |
Full list available here
Examples:
reduce(func) |
Aggregate the elements of the dataset using a function func (which takes two arguments and returns one). The function should be commutative and associative so that it can be computed correctly in parallel. |
collect() |
Return all the elements of the dataset as an array at the driver program. This is usually useful after a filter or other operation that returns a sufficiently small subset of the data. |
count() |
Return the number of elements in the dataset. |
take(n) |
Return an array with the first n elements of the dataset. |
Full list available here
Create a SparkContext
, e.g
// Connect to `cluster` and call app `name`
val sc = new SparkContext(cluster, name)
Set up RDD, e.g
// Create RDD from local collection `inputs`
val parInputs: RDD[Int] = sc
.parallelize(inputs)
Apply transformations, e.g
// Map function f over RDD
val parTransf: RDD[Int] = parInputs
.map(input => f(input))
Apply actions, e.g
// Reduce by multiplication of elements
// result to local variable `total`
val total = parTransf.reduce(_*_)
.
.
.
7ANX6 : 26
7ANX6 : 55
KY76X : 23
DZJQR : 31
SSRU2 : 90
KY76X : 12
SSRU2 : 87
.
.
.
SparkContext
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
@main def main() =
// Set up context
val sc = new SparkContext("local[4]", "example")
// File of data pairs, e.g. id and points
val rawData : RDD[String] = sc.textFile("data.txt")
// Split at ',' and convert second column to double
// Note ASSUMES well formatted file
val data : RDD[(String, Int)] = rawData.map(x =>{
val cols = x.split(":")
(cols(0).trim(), cols(1).trim.toInt)
})
// Filter on values in a certain range,
// e.g. to get rid of outliers
val filtedData : RDD[(String,Int)] =
data.filter((_,v) => (v > 5 && v < 95))
// Calculate sum of values for each key value,
// then get results
val keySums : Array[(String, Int)] =
filtedData
.reduceByKey(_+_)
.collect()
// keySums contains the data collected,
*Run.scala
fileOUTPUT
to *Solutions.scala
file