Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training...

47
Reza Zadeh Spark and the Big Data Library Thanks to Matei Zaharia

Transcript of Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training...

Page 1: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Reza Zadeh

Spark and the Big Data Library

Thanks  to  Matei  Zaharia  

Page 2: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Problem Data growing faster than processing speeds Only solution is to parallelize on large clusters » Wide use in both enterprises and web industry

How do we program these things?

Page 3: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Outline Data flow vs. traditional network programming Limitations of MapReduce Spark computing engine

Machine Learning Example Current State of Spark Ecosystem Built-in Libraries

Page 4: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Data flow vs. traditional network programming

Page 5: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Traditional Network Programming

Message-passing between nodes (e.g. MPI) Very difficult to do at scale: » How to split problem across nodes?

•  Must consider network & data locality » How to deal with failures? (inevitable at scale) » Even worse: stragglers (node not failed, but slow) » Ethernet networking not fast » Have to write programs for each machine

Rarely used in commodity datacenters

Page 6: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Data Flow Models Restrict the programming interface so that the system can do more automatically Express jobs as graphs of high-level operators » System picks how to split each operator into tasks

and where to run each task » Run parts twice fault recovery

Biggest example: MapReduce Map

Map

Map

Reduce

Reduce

Page 7: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Example MapReduce Algorithms Matrix-vector multiplication Power iteration (e.g. PageRank) Gradient descent methods

Stochastic SVD Tall skinny QR

Many others!

Page 8: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Why Use a Data Flow Engine? Ease of programming » High-level functions instead of message passing

Wide deployment » More common than MPI, especially “near” data

Scalability to very largest clusters » Even HPC world is now concerned about resilience

Examples: Pig, Hive, Scalding, Storm

Page 9: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Limitations of MapReduce

Page 10: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Limitations of MapReduce MapReduce is great at one-pass computation, but inefficient for multi-pass algorithms No efficient primitives for data sharing » State between steps goes to distributed file system » Slow due to replication & disk storage

Page 11: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

iter. 1 iter. 2 . . .

Input

file system"read

file system"write

file system"read

file system"write

Input

query 1

query 2

query 3

result 1

result 2

result 3

. . .

file system"read

Commonly spend 90% of time doing I/O

Example: Iterative Apps

Page 12: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Example: PageRank Repeatedly multiply sparse matrix and vector Requires repeatedly hashing together page adjacency lists and rank vector

Neighbors (id, edges)

Ranks (id, rank) …

Same file grouped over and over

iteration 1 iteration 2 iteration 3

Page 13: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Result While MapReduce is simple, it can require asymptotically more communication or I/O

Page 14: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Spark computing engine

Page 15: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Spark Computing Engine Extends a programming language with a distributed collection data-structure » “Resilient distributed datasets” (RDD)

Open source at Apache » Most active community in big data, with 50+

companies contributing

Clean APIs in Java, Scala, Python, R

Page 16: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Resilient Distributed Datasets (RDDs)

Main idea: Resilient Distributed Datasets »  Immutable collections of objects, spread across cluster » Statically typed: RDD[T] has objects of type T

val sc = new SparkContext()!val lines = sc.textFile("log.txt") // RDD[String]!!// Transform using standard collection operations !val errors = lines.filter(_.startsWith("ERROR")) !val messages = errors.map(_.split(‘\t’)(2)) !!messages.saveAsTextFile("errors.txt") !

lazily evaluated

kicks off a computation

Page 17: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Key Idea Resilient Distributed Datasets (RDDs) » Collections of objects across a cluster with user

controlled partitioning & storage (memory, disk, ...) » Built via parallel transformations (map, filter, …) » The world only lets you make make RDDs such that

they can be:

Automatically rebuilt on failure

Page 18: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Python, Java, Scala, R // Scala:

val lines = sc.textFile(...) lines.filter(x => x.contains(“ERROR”)).count()

// Java:

JavaRDD<String> lines = sc.textFile(...); lines.filter(new Function<String, Boolean>() { Boolean call(String s) { return s.contains(“error”); } }).count();

Page 19: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Fault Tolerance

file.map(lambda  rec:  (rec.type,  1))          .reduceByKey(lambda  x,  y:  x  +  y)          .filter(lambda  (type,  count):  count  >  10)  

filter reduce map

Inpu

t file

RDDs track lineage info to rebuild lost data

Page 20: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

filter reduce map

Inpu

t file

Fault Tolerance

file.map(lambda  rec:  (rec.type,  1))          .reduceByKey(lambda  x,  y:  x  +  y)          .filter(lambda  (type,  count):  count  >  10)  

RDDs track lineage info to rebuild lost data

Page 21: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Partitioning

file.map(lambda  rec:  (rec.type,  1))          .reduceByKey(lambda  x,  y:  x  +  y)          .filter(lambda  (type,  count):  count  >  10)  

filter reduce map

Inpu

t file

RDDs know their partitioning functions

Known to be"hash-partitioned

Also known

Page 22: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Machine Learning example

Page 23: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Logistic Regression data  =  spark.textFile(...).map(readPoint).cache()    w  =  numpy.random.rand(D)    for  i  in  range(iterations):          gradient  =  data.map(lambda  p:                  (1  /  (1  +  exp(-­‐p.y  *  w.dot(p.x))))  *  p.y  *  p.x          ).reduce(lambda  a,  b:  a  +  b)          w  -­‐=  gradient    print  “Final  w:  %s”  %  w  

Page 24: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Logistic Regression Results

0 500

1000 1500 2000 2500 3000 3500 4000

1 5 10 20 30

Runn

ing T

ime

(s)

Number of Iterations

Hadoop Spark

110 s / iteration

first iteration 80 s further iterations 1 s

100 GB of data on 50 m1.xlarge EC2 machines  

Page 25: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Behavior with Less RAM 68

.8

58.1

40.7

29.7

11.5

0

20

40

60

80

100

0% 25% 50% 75% 100%

Itera

tion

time

(s)

% of working set in memory

Page 26: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Benefit for Users Same engine performs data extraction, model training and interactive queries

… DFS read

DFS write pa

rse DFS read

DFS write tra

in DFS read

DFS write qu

ery

DFS

DFS read pa

rse

train

quer

y

Separate engines

Spark

Page 27: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

State of the Spark ecosystem

Page 28: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Most active open source community in big data

200+ developers, 50+ companies contributing

Spark Community

Giraph Storm

0

50

100

150

Contributors in past year

Page 29: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Project Activity M

apRe

duce

YA

RN HD

FS

Stor

m

Spar

k

0

200

400

600

800

1000

1200

1400

1600

Map

Redu

ce

YARN

HDFS

St

orm

Spar

k

0

50000

100000

150000

200000

250000

300000

350000

Commits Lines of Code Changed

Activity in past 6 months

Page 30: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Continuing Growth

source: ohloh.net

Contributors per month to Spark

Page 31: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Built-in libraries

Page 32: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Standard Library for Big Data Big data apps lack libraries"of common algorithms Spark’s generality + support"for multiple languages make it"suitable to offer this

Core

SQL ML graph …

Python Scala Java R

Much of future activity will be in these libraries

Page 33: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

A General Platform

Spark Core

Spark Streaming"

real-time

Spark SQL structured

GraphX graph

MLlib machine learning

Standard libraries included with Spark

Page 34: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Machine Learning Library (MLlib)

40 contributors in past year

points = context.sql(“select latitude, longitude from tweets”) !

model = KMeans.train(points, 10) !!

Page 35: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

MLlib algorithms classification: logistic regression, linear SVM,"naïve Bayes, classification tree regression: generalized linear models (GLMs), regression tree collaborative filtering: alternating least squares (ALS), non-negative matrix factorization (NMF) clustering: k-means|| decomposition: SVD, PCA optimization: stochastic gradient descent, L-BFGS

Page 36: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

36

GraphX

Page 37: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

37

General graph processing library Build graph using RDDs of nodes and edges

Large library of graph algorithms with composable steps

GraphX

Page 38: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Collaborative Filtering » Alternating Least Squares » Stochastic Gradient Descent » Tensor Factorization

Structured Prediction » Loopy Belief Propagation » Max-Product Linear Programs » Gibbs Sampling

Semi-supervised ML » Graph SSL » CoEM

Community Detection » Triangle-Counting » K-core Decomposition » K-Truss

Graph Analytics » PageRank » Personalized PageRank » Shortest Path » Graph Coloring

Classification » Neural Networks

38

GraphX Algorithms

Page 39: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Spark Streaming Run a streaming computation as a series of very small, deterministic batch jobs

39

Spark  

Spark  Streaming  

batches  of  X  seconds  

live  data  stream  

processed  results  

•  Chop  up  the  live  stream  into  batches  of  X  seconds    

•  Spark  treats  each  batch  of  data  as  RDDs  and  processes  them  using  RDD  opera;ons  

•  Finally,  the  processed  results  of  the  RDD  opera;ons  are  returned  in  batches  

Page 40: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Spark Streaming Run a streaming computation as a series of very small, deterministic batch jobs

40

Spark  

Spark  Streaming  

batches  of  X  seconds  

live  data  stream  

processed  results  

•  Batch  sizes  as  low  as  ½  second,  latency  ~  1  second  

•  Poten;al  for  combining  batch  processing  and  streaming  processing  in  the  same  system  

Page 41: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Spark SQL // Run SQL statements !val teenagers = context.sql( ! "SELECT name FROM people WHERE age >= 13 AND age <= 19") !

!

// The results of SQL queries are RDDs of Row objects !val names = teenagers.map(t => "Name: " + t(0)).collect() !

Page 42: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Enables loading & querying structured data in Spark

c = HiveContext(sc) !rows = c.sql(“select text, year from hivetable”) !rows.filter(lambda r: r.year > 2013).collect() !

From Hive:

{“text”: “hi”, “user”: { “name”: “matei”, “id”: 123 }}

c.jsonFile(“tweets.json”).registerAsTable(“tweets”) !c.sql(“select text, user.name from tweets”) !

From JSON: tweets.json

Spark SQL

Page 43: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Conclusions

Page 44: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Spark and Research Spark has all its roots in research, so we hope to keep incorporating new ideas!

Page 45: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Conclusion Data flow engines are becoming an important platform for numerical algorithms While early models like MapReduce were inefficient, new ones like Spark close this gap More info: spark.apache.org

Page 46: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Class Schedule

Page 47: Spark and the Big Data Library - Stanford Universityrezab/slides/bootcamp_keynote.pdf · training and interactive queries … DFS read DFS parse write DFS read DFS train write DFS

Schedule Today and tomorrow Hands-on exercises, download course materials and slides:http://stanford.edu/~rezab/sparkclass/ "

Friday Advanced talks on Spark libraries and uses