Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Why Big Data Needs To Be Functional

marakana
March 22, 2012
3.1k

Why Big Data Needs To Be Functional

Dean Wampler, principal consultant at Think Big Analytics, shares his thoughts on why "Big Data" needs to be Functional at the 2012 Northeast Scala Symposium. Dean examines how OOP and Java thinking is impacting the effectiveness of Hadoop (the current darling of the Big Data world).

Using Scala examples from tools like Scrunch, Spark, and others, Dean demonstrates why functional programming is the way to improve internal efficiency and developer productivity. Finally, Dean looks at the potential future role for Scala in Big Data.

marakana

March 22, 2012
Tweet

Transcript

  1. Why Big Data Needs to Be Functional 1 NE Scala

    Symposium @deanwampler March 9, 2012 Friday, March 16, 12 All pictures © Dean Wampler, 2011-2012.
  2. What is Big Data? Data so big that traditional solutions

    are too slow, too small, or too expensive to use. 2 Hat tip: Bob Korbus Friday, March 16, 12 It’s a buzz word, but generally associated with the problem of data sets too big to manage with traditional SQL databases. A parallel development has been the NoSQL movement that is good at handling semistructured data, scaling, etc.
  3. 4 Data Size ‐ Friday, March 16, 12 Data volumes

    are obviously growing… rapidly.
  4. 5 Formal Schemas ‑ Friday, March 16, 12 There is

    less emphasis on “formal” schemas and domain models, because data changes rapidly, there are disparate data sources being joined, using relatively-agnostic software (e.g., collections of things where the software is agnostic about the contents) tends to be faster to develop and run.
  5. 6 Data-Driven Programs ‐ Friday, March 16, 12 Machine learning

    is growing in importance. Here, generic algorithms and data structures are trained to represent the “world” using data, rather than encoding a model of the world in the software itself.
  6. 7 Object Model toJSON ParentB1 toJSON ChildB1 toJSON ChildB2 Object-

    Relational Mapping Other, Object- Oriented Domain Logic Database Query SQL Result Set Objects 1 2 3 4 Relational/ Functional Domain Logic Database Query SQL Result Set 1 2 Functional Wrapper for Relational Data 3 Functional Abstractions vs. Friday, March 16, 12 Traditionally, (on the left) we’ve kept a rich, in-memory domain model requiring an ORM to convert persistent data into the model. This is resource overhead and complexity we can’t afford in big data systems. Rather, (on the right) we should treat the result set as it is, a particular kind of collection, do the minimal transformation required to exploit our collections libraries and classes representing some domain concepts (e.g., Address, StockOption, etc.), then write functional code to implement business logic (or drive emergent behavior with machine learning algorithms…)
  7. 8 toJSON ParentB1 toJSON ChildB1 toJSON ChildB2 Web Client 1

    Web Client 2 Web Client 3 Service 1 Service 2 Database Web Client 1 Web Client 2 Web Client 3 Service 1 Service 2 Database Process 1 Process 2 Process 3 vs. Friday, March 16, 12 In a broader view, object models (on the left) tend to push us towards centralized, complex systems that don’t decompose well and stifle reuse and optimal deployment scenarios. FP code makes it easier to write smaller, focused services (on the right) that we compose and deploy as appropriate.
  8. 9 Web Client 1 Web Client 2 Web Client 3

    Service 1 Service 2 Database Process 1 Process 2 Process 3 • Data Size ‐ • Formal Schema ‑ • Data-Driven Programs ‐ Friday, March 16, 12 Smaller, focused services scale better, especially horitzontally. They also don’t encapsulate more business logic than is required, and this (informal) architecture is also suitable for scaling ML and related algorithms.
  9. 10 Using Scala for MapReduce Friday, March 16, 12 Back

    to Scala, let’s look at the mess that Java has introduced into Hadoop MapReduce, and get a glimpse of a better way. As an example, I’ll walk you through the “Hello World” of MapReduce; the Word Count algorithm...
  10. There is a Map phase Hadoop uses MapReduce Input (doc1,

    "…") (doc2, "…") (doc3, "") Mappers There is a Reduce phase (doc4, "…") Consider Word Count Friday, March 16, 12 Each document gets a mapper. I’m showing the document contents in the boxes for this example. Actually, large documents might get split to several mappers (as we’ll see). It is also possible to concatenate many small documents into a single, larger document for input to a mapper. Each mapper will receive a key-value pair, where the key is the document path and the value is the contents of the document. It will ignore the key, tokenize the content, convert all words to lower case and count them...
  11. There is a Map phase Hadoop uses MapReduce Input (doc1,

    "…") (doc2, "…") (doc3, "") Mappers Sort, Shuffle Reducers There is a Reduce phase (doc4, "…") (hadoop, 1) (uses, 1) (mapreduce, 1) (is, 1), (a, 1) (there, 1) (there, 1), (reduce 1) (phase,1) (map, 1),(phase,1) (is, 1), (a, 1) 0-9, a-l m-q r-z Friday, March 16, 12 The mappers emit key-value pairs, where each key is one of the words, and the value is the count. In the most naive (but also most memory efficient) implementation, each mapper simply emits (word, 1) each time “word” is seen. The mappers themselves don’t decide to which reducer each pair should be sent. Rather, the job setup configures what to do and the Hadoop runtime enforces it during the Sort/Shuffle phase, where the key-value pairs in each mapper are sorted by key (that is locally, not globally or “totally”) and then the pairs are routed to the correct reducer, on the current machine or other machines. Note how we partitioned the reducers (by first letter of the keys). Also, note that the mapper for the empty doc. emits no pairs, as you would expect.
  12. There is a Map phase Hadoop uses MapReduce Input (doc1,

    "…") (doc2, "…") (doc3, "") Mappers Sort, Shuffle (a, [1,1]), (hadoop, [1]), (is, [1,1]) (map, [1]), (mapreduce, [1]), (phase, [1,1]) Reducers There is a Reduce phase (doc4, "…") (reduce, [1]), (there, [1,1]), (uses, 1) (hadoop, 1) (uses, 1) (mapreduce, 1) (is, 1), (a, 1) (there, 1) (there, 1), (reduce 1) (phase,1) (map, 1),(phase,1) (is, 1), (a, 1) 0-9, a-l m-q r-z Friday, March 16, 12 The mappers emit key-value pairs, where each key is one of the words, and the value is the count. In the most naive (but also most memory efficient) implementation, each mapper simply emits (word, 1) each time “word” is seen. The mappers themselves don’t decide to which reducer each pair should be sent. Rather, the job setup configures what to do and the Hadoop runtime enforces it during the Sort/Shuffle phase, where the key-value pairs in each mapper are sorted by key (that is locally, not globally or “totally”) and then the pairs are routed to the correct reducer, on the current machine or other machines. Note how we partitioned the reducers (by first letter of the keys). Also, note that the mapper for the empty doc. emits no pairs, as you would expect.
  13. There is a Map phase Hadoop uses MapReduce Input (doc1,

    "…") (doc2, "…") (doc3, "") Mappers Sort, Shuffle (a, [1,1]), (hadoop, [1]), (is, [1,1]) (map, [1]), (mapreduce, [1]), (phase, [1,1]) Reducers map 1 mapreduce 1 phase 2 a 2 hadoop 1 is 2 Output There is a Reduce phase (doc4, "…") (reduce, [1]), (there, [1,1]), (uses, 1) reduce 1 there 2 uses 1 (hadoop, 1) (uses, 1) (mapreduce, 1) (is, 1), (a, 1) (there, 1) (there, 1), (reduce 1) (phase,1) (map, 1),(phase,1) (is, 1), (a, 1) 0-9, a-l m-q r-z Friday, March 16, 12 The final view of the WordCount process flow for our example. We’ll see in more detail shortly how the key-value pairs are passed to the reducers, which add up the counts for each word (key) and then writes the results to the output files. The the output files contain one line for each key (the word) and value (the count), assuming we’re using text output. The choice of delimiter between key and value is up to you. (We’ll discuss options as we go.)
  14. 15 The MapReduce Java API Friday, March 16, 12 A

    Java API, but I’ll show you Scala code; see my GitHub scala-hadoop project.
  15. 16 import org.apache.hadoop.io._ import org.apache.hadoop.mapred._ import java.util.StringTokenizer object SimpleWordCount {

    val one = new IntWritable(1) val word = new Text // Value will be set in a non-thread-safe way! class WCMapper extends MapReduceBase with Mapper[LongWritable, Text, Text, IntWritable] { def map(key: LongWritable, valueDocContents: Text, output: OutputCollector[Text, IntWritable], reporter: Reporter): Unit = { val tokens = valueDocContents.toString.split("\\s+") for (wordString <- tokens) { if (wordString.length > 0) { word.set(wordString.toLowerCase) output.collect(word, one) } } } class Reduce extends MapReduceBase with Reducer[Text, IntWritable, Text, IntWritable] { def reduce(keyWord: Text, valuesCounts: java.util.Iterator[IntWritable], output: OutputCollector[Text, IntWritable], reporter: Reporter): Unit = { var totalCount = 0 while (valuesCounts.hasNext) { totalCount += valuesCounts.next.get } output.collect(keyWord, new IntWritable(totalCount)) } } Friday, March 16, 12 This is intentionally too small to read. The algorithm is simple, but the framework is in your face. Note all the green types floating around and relatively few yellow methods implementing actual operations. Still, I’ve omitted many boilerplate details for configuring and running the job. This is just the “core” MapReduce code. In fact, Word Count is not too bad, but when you get to more complex algorithms, even conceptually simple things like relational-style joins, code in this API gets complex and tedious very fast.
  16. 17 Using Crunch (Java) Friday, March 16, 12 Crunch is

    a Java library that provides a higher-level abstraction for data computations and flows on top of MapReduce, inspired by a paper from Google researchers on an internal project called FlumeJava. See https://github.com/cloudera/crunch.
  17. 18 • Pipeline<T>: Abstraction for data processing flow. • PCollection<T>:

    Distributed, unordered collection of elements T. • parallelDo(): Apply operation across collection. Crunch Concepts Friday, March 16, 12 A quick overview of Crunch concepts. Note that it’s necessary for Crunch to provide features missing in Java: parallel data structures and operations over them. (Even non-parallel data structures are poor, missing the “power tools” of functional programming, operations like map, flatMap, fold, etc.)
  18. 19 • PTable<K, V>: Distributed multimap. • groupByKey(): group together

    all values with the same key. • parallelDo(): apply operation across collection. Crunch Concepts Friday, March 16, 12
  19. 20 • PGroupedTable<K, V>: Output of groupByKey(). • combineValues(): associative

    and commutative . • groupByKey(): group together all values with the same key. • parallelDo(): apply operation across collection. Crunch Concepts Friday, March 16, 12
  20. 21 import com.cloudera.crunch.*; import org.apache.hadoop.*; ... public class WordCount extends

    Configured implements Tool, Serializable { public int run(String[] args) throws Exception { Pipeline pipeline = new MRPipeline(WordCount.class, getConf()); PCollection<String> lines = pipeline.readTextFile(args[0]); PCollection<String> words = lines.parallelDo(new DoFn<String, String>() { public void process(String line, Emitter<String> emitter) { for (String word : line.split("\\s+")) { emitter.emit(word); } } }, Writables.strings()); PTable<String, Long> counts = Aggregate.count(words); pipeline.writeTextFile(counts, args[1]); pipeline.done(); return 0; } } Friday, March 16, 12 This is back to Java (rather than Scala calling a Java API). I omitted the setup and “main” again, but that code is a lot smaller than for generic MapReduce. It’s a definite improvement, especially for more sophisticated algorithms. Crunch (and FlumeJava), as well as similar toolkits like Cascading, are steps in the right direction, but there is still 1) lots of boilerplate, 2) functional ideas crying to escape the Java world view, and poor support in the MapReduce API (like no Tuple type) that harms Crunch, etc. Still, note that relatively heavy amount of type information compared to methods (operations). It would improve some if I wrote this code in Scala, thereby exploiting type inference, but not by a lot.
  21. 22 Using Scrunch (Scala) Friday, March 16, 12 Scrunch is

    a scala DSL around Crunch writing by the same developers at Cloudera and also included in the Crunch distro. It uses type classes and similar constructs to provide wrap the Crunch classes and MR classes with REAL map, flatMap, etc. functionality.
  22. 23 import com.cloudera.crunch._ import com.cloudera.scrunch._ ... class ScrunchWordCount { def

    wordCount(inputFile: String, outputFile: String) = { val pipeline = new Pipeline[ScrunchWordCount] pipeline.read(from.textFile(inputFile)) .flatMap(_.toLowerCase.split("\\W+")) .filter(!_.isEmpty()) .count .write(to.textFile(outputFile)) // Word counts .map((w, c) => (w.slice(0, 1), c)) .groupByKey.combine(v => v.sum).materialize pipeline.done } } object ScrunchWordCount { def main(args: Array[String]) = { new ScrunchWordCount.wordCount(args(0), args(1)) } } Friday, March 16, 12 (Back to Scala) I cheated; I’m showing you the WHOLE program, “main” and all. Not only is the size significantly smaller and more concise still, but note the “builder” style notation, which intuitive lays out the data flow required. There is must less green - fewer types are explicitly tossed about, and not just because Scala does implicit typing. In contrast, there is more yellow - function calls showing the sequence of operations that more naturally represent the real “business logic”, i.e., the data flow that is Word Count! Also, fewer comments are required to help you sort out what’s going on. Once you understand the meaning of the individual, high-reusable operations like flatMap and filter, you can construct arbitrarily-complex transformations and calculations with relative ease.
  23. 24 • Cascading (Java) vs. • Cascalog (Clojure) • Scalding

    (Scala) You Also See this Functional Improvement with... Friday, March 16, 12 Similarly, for the better-known Cascading Java API on top of Hadoop, similar “improvements” occur when you use Cascalog or Scalding.
  24. 25 Scala (and FP) give us natural tools for big

    data! Friday, March 16, 12 This is obvious for this crowd, but it’s under-appreciated by most people in the big data world. Functional programming is ideal for data transformations, filtering, etc. Ad-hoc object models are not “the simplest thing that could possible work” (an Agile catch phrase), at least for data-oriented problems. There’s a reason that SQL has been successful all these years; the relational model is very functional and fits data very well.
  25. Hadoop is the Enterprise Java Beans of our time. Friday,

    March 16, 12 I worked with EJBs a decade ago. The framework was completely invasive into your business logic. There were too many configuration options in XML files. The framework “paradigm” was a poor fit for most problems (like soft real time systems and most algorithms beyond Word Count). Internally, EJB implementations were inefficient and hard to optimize, because they relied on poorly considered object boundaries that muddled more natural boundaries. (I’ve argued in other presentations and my “FP for Java Devs” book that OOP is a poor modularity tool…) The fact is, Hadoop reminds me of EJBs in almost every way. It works okay and people do get stuff done, but just as the Spring Framework brought an essential rethinking to Enterprise Java, I think there is an essential rethink that needs to happen in Big Data. The Scala community is well positioned to create it.
  26. Scala Collections. Friday, March 16, 12 We already have the

    write model in Scala’s collections and the parallel versions already support multi-core horizontal scaling. With an extension to distributed horizontal scaling, they will be the ideal platform for diverse services, including those poorly served by Hadoop...
  27. Akka for distributed computation. Friday, March 16, 12 Akka is

    the right platform for distributed services. It exposes clean, low-level primitives for robust, distributed services (e.g., Actors), upon which we can build flexible big data systems that can handle soft real time and batch processing efficiently and scalably. (No, this isn’t Akka Mountain in Sweden. So sue me… ;)
  28. FP for Java Devs 1/2 off today!! 30 Dean Wampler

    Functional Programming for Java Developers Friday, March 16, 12 That’s it. Today only (3/9), you can get my ebook 1/2 off!