[CS61C FA20] Lecture 36.4 - MapReduce, Spark: Spark — Transcript
Full transcript
- 0:00welcome back now we're going to teach
- 0:02you spark
- 0:03which is another way to think of the
- 0:04mapreduce paradigm with a little bit
- 0:07lighter weight touch in terms of how
- 0:08much programming you have to do for it
- 0:10so it's a fast and general engine for
- 0:12large-scale data processing it came out
- 0:14of uc berkeley
- 0:15very excited about that and this is a
- 0:17performance bar
- 0:18that says running time on a particular
- 0:20file
- 0:21and spark is blowing out of water at a
- 0:23factor of more than 100 times faster
- 0:25and part of the reason for that is that
- 0:27spark
- 0:29does the work in memory map produce is
- 0:31always about disk
- 0:32disk in disk out so there's they say but
- 0:35basically mapreduce says
- 0:36we're dealing with such big data files
- 0:38data sets that i can't do it in memory
- 0:40i have to i mean i'm going to load it in
- 0:41memory and do it but put it down
- 0:43basically the kind of input output
- 0:44characteristic is always based on disk
- 0:46file in file out that's the idea you
- 0:48write a file then you're done if it
- 0:49crashes halfway through it leaves your
- 0:50files written and
- 0:51you can rest spark says well that's nice
- 0:54but wouldn't it be nice if i could work
- 0:56just in memory and just live in memory
- 0:58not have to pay the 1000 time or more
- 1:00performance penalty to go to disk
- 1:03that's painful so spark has an advanced
- 1:07execution engine that figures out what's
- 1:09needed when
- 1:10and it gives you just in time
- 1:12computation it's quite quite clever it's
- 1:14very it's lazy it's a lazy evaluation
- 1:15model which is really nice
- 1:16and as much as you can in memory
- 1:18computing easy right applications in
- 1:20java in scala or python we're going to
- 1:22see the python example
- 1:24and it offers very quite a few
- 1:25high-level operations to make it very
- 1:27easy and you can even interact with it
- 1:28interactively
- 1:29you can interface with interactively as
- 1:31well as a batch processing way which is
- 1:32how mapreduce is done
- 1:34but the fact that you can play this
- 1:35interactively is really very fun and
- 1:36very exciting
- 1:38so ladies and gentlemen let me show you
- 1:41this is the java code i showed you in
- 1:42the last lecture
- 1:44this is the same word count
- 1:48in spark in python file
- 1:52i've loaded file there's a there's
- 1:54another line above it which it says you
- 1:55know what file is i've loaded file in
- 1:56but the actual processing is
- 1:58first i flat map by splitting my
- 2:01document into words now i have as some
- 2:04sense a list of words
- 2:07i then map across that word
- 2:10and one and i return now a list of word
- 2:12and one
- 2:14and i reduce by key remember the
- 2:15reduction by key so this is rather than
- 2:17having a shuffling phase
- 2:19this is reduced by key and what that
- 2:21says is
- 2:22each of these worker elements is going
- 2:26to get
- 2:26again just the values of the same key
- 2:29reduced by key means
- 2:30each of those mach worker b's is only
- 2:33going to get
- 2:34all only get the values for a particular
- 2:36key and what does it do with those two
- 2:37values
- 2:38it adds them up ladies and gentlemen
- 2:42mic drop so not only are you 100 times
- 2:44faster
- 2:45but you can do in three lines what java
- 2:47had to do in
- 2:4945 lines pretty impressive so let's
- 2:51actually play with it a little bit so
- 2:53you can load this up i encourage you to
- 2:54do this as well
- 2:56this is i'm going to show you about what
- 2:58um what flat map does i want
- 3:00if you didn't know what flatmap does i
- 3:01want to show explain them what it is and
- 3:02why we knew we need that
- 3:04so here's an example called neighbor
- 3:05neighbor takes a number returns a list
- 3:06of the number to the left
- 3:08my number number to my right pretty easy
- 3:10we start this by saying
- 3:12sc spark context dot
- 3:15paralyze range of five that says i'm
- 3:18going to take
- 3:19um the number zero through four
- 3:21inclusive
- 3:22and in some sense parallelize it load it
- 3:24into its
- 3:25uh rdd set into it into its the way it
- 3:28stores data
- 3:29into a parallel way it stores data okay
- 3:31and call it r
- 3:33now it's nothing's happened yet by the
- 3:35way that didn't actually you know that
- 3:37range i hope you know that the
- 3:39difference between range
- 3:41in python 2 versus python 3 is python 2
- 3:45if you say range of a million it will
- 3:47actually make a million list
- 3:49and return it python 3 range says
- 3:52yeah but what if you never use it that's
- 3:54silly to make that million list range
- 3:55but never use it so python 3
- 3:57takes the lazy approach and returns a
- 3:59promise here's range everything's a new
- 4:01type called range of
- 4:02i think it's called range of five range
- 4:04zero five one maybe
- 4:05and it says well that didn't do any work
- 4:07it returns instantly
- 4:09python two call range of a million and
- 4:10see how long it takes call range of a
- 4:12million on python three instantaneously
- 4:14here's a promise for a range of a
- 4:15million if you ever wanted them
- 4:18and then if you say well let me iterate
- 4:19well it's gonna one by one grab a value
- 4:21from the range
- 4:22hey range i need your next value range
- 4:23okay here it is three
- 4:25range i need a nice value here's four
- 4:26that's how that works that's how lazy
- 4:28evaluation works
- 4:29same idea here in spark that range
- 4:32didn't do any work so range returns a
- 4:34promise that paralyzed into anywhere
- 4:35because you never asked for it
- 4:37only when you say collect are you saying
- 4:39okay now you gotta
- 4:40pay the piper where's your where's your
- 4:42data
- 4:43parallel says well where's the data who
- 4:45recall it on oh range of five quick
- 4:46range of five give some value range says
- 4:48okay how much you need all of them great
- 4:50and range will give you those values
- 4:51only at that moment does range do any
- 4:53work
- 4:53and only that moment does the parallel
- 4:55parallels do any work that's the idea
- 4:56it's a very lazy system very clever that
- 4:58way
- 4:59it also means you might have delays like
- 5:01there's like 20 commands like
- 5:03why are they so fast yeah because
- 5:04nothing's happened yet only when you say
- 5:05collect
- 5:06to say okay now i can do the work so
- 5:07only on a collect we'll actually gather
- 5:09this up and try to spit it back at you
- 5:10it's really important idea that you
- 5:11understand that
- 5:12so now if i were to say r dot map of
- 5:14neighbor it'd return
- 5:16a list of five of these triplets right
- 5:18the n minus one
- 5:19n and n plus one but it wouldn't do it
- 5:22until i say
- 5:22collect if i just says r.map on neighbor
- 5:25that's fine range could have been over
- 5:2710 billion 100 billion
- 5:29a trillion that r dot map of neighbor
- 5:32would have instantly returned doing no
- 5:33work
- 5:33because it's not asked to produce
- 5:34anything yet if you say collect it's
- 5:36going to try to give it all and actually
- 5:37try to
- 5:38do its work so if i say art map neighbor
- 5:40dot collect
- 5:41it will give you that it will give you a
- 5:42list of five sub lists
- 5:45but i'm saying to myself i don't want
- 5:46five sub i want one flat map i want one
- 5:49flat list well that's what flatmap does
- 5:53it's exactly the same idea as map except
- 5:54it flattens whatever
- 5:56sub-list came out of what map would have
- 5:58given you it flattens those it's like it
- 5:59appends them all together
- 6:01so flat map returns the same thing as
- 6:03map does except they're all flattened
- 6:05does that make sense i teach you that
- 6:07because we're going to see this as we
- 6:08look at what word count does
- 6:10when we play with word count we're going
- 6:11to actually run let's do a slow motion
- 6:13version of word count
- 6:15in in in in spark so let's do this now
- 6:18so here is word count in smart we saw
- 6:20this already in one line where the
- 6:21stephen colbert dropped his mic
- 6:23but let's do a little bit slower this is
- 6:25exactly the same data i showed you
- 6:26before
- 6:27literally to the to the to the letter
- 6:29the same data these are the files
- 6:33uh in the first one and then there's an
- 6:36empty file on the second one that's just
- 6:38all these are the same data before that
- 6:39picture you saw earlier so the first
- 6:41thing i say is
- 6:42w for word count equals sc.text file i
- 6:46give a text file
- 6:47it then load that's like it's
- 6:48parallelized but it's reading text files
- 6:50rather than paralyzing a python
- 6:52object first thing i say is
- 6:56flat map again lambda line line dot
- 6:58split and only if i say dot collectible
- 7:01to print something out if i just did
- 7:02that it wouldn't do normally give you
- 7:03any output
- 7:04but i say this and it gives me
- 7:06essentially if it says i don't
- 7:07what this what this flat map does again
- 7:09it says i don't care what file this what
- 7:11you came from
- 7:11i want to just flatten you into one big
- 7:13pool we call it a pool of words
- 7:16okay one big pool of words so here we
- 7:19are there's all the words i have
- 7:21then i say flat map and by the way the
- 7:22orange is the only thing that's new okay
- 7:24so this this
- 7:25all the white is the same i'm adding dot
- 7:27map lambda word word of one
- 7:29okay and what that's going to do is for
- 7:31each of these words
- 7:32i add i tag it one now that's new and
- 7:34you've seen that before now three times
- 7:36here
- 7:36and now here's the key i add reduce by
- 7:39key
- 7:40lambda a b a plus b and this is
- 7:42literally
- 7:43i just copy and paste this yesterday
- 7:44what you get from from
- 7:46uh from spark and that's what you get er
- 7:49and by the way these are in no no
- 7:50particular order obviously they're not
- 7:51alphabetized or anything but err is one
- 7:53four oz two ifs uh three ors
- 7:56and an uh that's my output
- 8:00word count now i see i broke this into
- 8:02pieces i probably could have done this
- 8:03in one line i mean
- 8:05you know this this sc.text file
- 8:08is w i could have said that dot flat map
- 8:11and then that dot
- 8:13map and then that dot reduced by key the
- 8:15same again one line like i showed you
- 8:17before
- 8:18pretty neat isn't that beautiful so
- 8:20that's an example now you say well dan
- 8:22is it faster
- 8:23you sure did it in parallel while i did
- 8:24this little thing that little baby thing
- 8:26that's insanity check it
- 8:27let's run this thing let's say i'm going
- 8:28to crunch some numbers simulated
- 8:29crunching a big number
- 8:31i take a number n i'm going to be
- 8:33chugging chugging chugging doing lots of
- 8:34testing in here and bluebird
- 8:36something it's going to take a long an
- 8:37hour a long i mean five seconds i'm
- 8:39saying
- 8:40so i'm gonna actually say i'm gonna
- 8:42sleep for five seconds and then return
- 8:43the square of that number
- 8:45so if i say crunch of ten it returns a
- 8:48hundred
- 8:49five seconds later by the way try this
- 8:50yourself this trust me
- 8:53if i then say map of crunch and range of
- 8:56four so i'm going to square all the
- 8:58elements from zero
- 8:59zero through three um if i just say that
- 9:01remember if i just say that what'll
- 9:03happen is it'll say oh yeah i've
- 9:04returned a map type
- 9:05now i don't i want you to actually give
- 9:06me the values so i have to say list that
- 9:08to get the actual values
- 9:09but it takes 20 seconds later there's
- 9:11four elements there
- 9:12four times five is 20 that's 20 seconds
- 9:14later and that does take 20 seconds if
- 9:15you time it
- 9:17then i just for fun i say well let's try
- 9:18it in sparks world
- 9:20sc paralyze range of four fine
- 9:25r dot map crunch collect
- 9:28five seconds later five seconds later we
- 9:31came up with that however so this is
- 9:32like a little silly test
- 9:34but now what's interesting is i have i
- 9:36run this on an acorn machine
- 9:38i would love to retest this and i want
- 9:40you to play with this on your own
- 9:41machines
- 9:42please play with this to see well okay
- 9:45how big can this be before
- 9:48i only if i only have you know we know
- 9:50we have hyper threading so maybe i have
- 9:51eight cores that's 16 threads i can work
- 9:53with
- 9:54how how big is it before this stops
- 9:57taking five seconds
- 9:58so continue to change this the size of
- 10:00this to see what happens and that's a
- 10:02kind of a way to poke into your machine
- 10:04and by the way when you instantiate
- 10:05spark you can tell it how many
- 10:07uh how many uh how many cores you want
- 10:09to have how many parallel threads you
- 10:10want to allow
- 10:11so play with that play with that number
- 10:13and see what happens as you change this
- 10:14number to see if it's always five
- 10:16seconds or when it when it gets
- 10:17above five seconds but it's kind of neat
- 10:18that this whole thing happened in five
- 10:19seconds just as a standard check
- 10:20to play with that so that's in this
- 10:23lecture
- 10:24fourth big idea that you've seen in this
- 10:26course is parallelism we are hitting it
- 10:28hard for many different ways in fact we
- 10:29saw two of those ways
- 10:31today data level parallelism and request
- 10:33level parallelism
- 10:34amdah's law is heartbreaking and
- 10:36unfortunate but it's part of our reality
- 10:38part as part of the by the way part of
- 10:39the reality of not computer science this
- 10:40is anything anything working in parallel
- 10:42amdahl's law will affect that
- 10:44and with again with infinite parallelism
- 10:46speed up is one over s that's the
- 10:47maximum
- 10:48mapreduce is a wonderful abstraction
- 10:51google uses it many many people yahoo
- 10:53uses it uh
- 10:54for for their big data processing uh it
- 10:56is file based
- 10:57spark does it even better and it's a
- 11:00very fast growing
- 11:01uh that the the number of developers
- 11:04involved in spark is growing and by the
- 11:06way
- 11:07this makes you very marketable nudge
- 11:09nudge wink wink say no more say no more
- 11:11i encourage all of you to learn about
- 11:12spark and be fluent in spark
- 11:14play with play with it over the break
- 11:16you add spark to this you say i can work
- 11:18with big data
- 11:19onto your resume you're you're looking
- 11:21like a pretty good candidate
- 11:23that's it we'll see at the next level
- 11:24when we talk about cloud computing
- 11:26pretty exciting
- 11:27see you there
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 36.4 - MapReduce, Spark: Spark by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 2,473 words across 389 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.