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

Cascalog 2.0: Datalog in Realtime

Sam Ritchie
November 15, 2013

Cascalog 2.0: Datalog in Realtime

Cascalog is a logic programming library for Clojure that allows users to run queries on enormous datasets (using Apache Hadoop). Cascalog's 2.0 release introduces two new query compilation modes - a local mode for in-memory analysis and a Trident mode for running Cascalog queries on Storm. These new features make Cascalog extremely powerful at every corner of the big data trinity; testing, batch-processing and realtime streaming.

This is a talk about the design of Cascalog's flexible, functional logic DSL. I'll discuss the protocol-based design of Cascalog's DSL and show how Clojure's dynamic dispatching made it easy to add these new compilation modes. After you learn Cascalog, you wonder how you ever did data processing any other way.

Sam Ritchie

November 15, 2013
Tweet

More Decks by Sam Ritchie

Other Decks in Programming

Transcript

  1. • What is Cascalog? + Examples • Why Logic Programming?

    • How Cascalog Compiles Datalog to MapReduce AGENDA
  2. • What is Cascalog? + Examples • Why Logic Programming?

    • How Cascalog Compiles Datalog to MapReduce • Different Compilation Targets AGENDA
  3. • What is Cascalog? + Examples • Why Logic Programming?

    • How Cascalog Compiles Datalog to MapReduce • Different Compilation Targets • What’s Next? AGENDA
  4. :)

  5. WHAT IS CASCALOG? • Datalog DSL in that helps you

    write • Tries to write analytics for you, using facts about your data
  6. WHAT IS CASCALOG? • Datalog DSL in that helps you

    write • Tries to write analytics for you, using facts about your data • Hadoop support enables petabyte scale ETL and analysis
  7. WHAT IS CASCALOG? • Datalog DSL in that helps you

    write • Tries to write analytics for you, using facts about your data • Hadoop support enables petabyte scale ETL and analysis • Batch and Hadoop only (until recently!)
  8. (defn word-count [gen] (let [split (mapcatfn [^String sentence] (.split sentence

    "\\s+"))] (<- [?word ?count] (gen ?text) (split ?text :> ?word) (c/count ?count))))
  9. ;; The above code produces: (word-count ["what does the fox

    say" "what does that mean??"]) ;;=> [["what" 2] ["does" 2] ["the" 1] ["fox" 1] ["say" 1] ["that" 1] ["mean??" 1]] (defn word-count [gen] (let [get-words (fn [^String sentence] (.split sentence "\\s+"))] (->> gen (mapcat get-words) ;; (“what” “does” “the” “fox” ....) (map (fn [word] [word 1])) ;; ([“what” 1] [“does” 1] ....) (group-by (fn [[word _]] word)) ;; {“what” [[“what” 1] [“what” 1]] ....} (map (fn [[k items]] [k (reduce (fn [acc [_ count]] (+ acc count)) 0 items)]))))) MapReduce in Clojure
  10. (defn word-count [gen] (let [split (mapcatfn [^String sentence] (.split sentence

    "\\s+"))] (<- [?word ?count] (split ?text :> ?word) (gen ?text) (c/count ?count)))) ;; The above code produces: (word-count ["what does the fox say" "what does that mean??"]) ;;=> [["what" 2] ["does" 2] ["the" 1] ["fox" 1] ["say" 1] ["that" 1] ["mean??" 1]] MapReduce in Cascalog
  11. (defn modis-chunks "Takes a cascading source, and returns a number

    of tuples that fully describe chunks of MODIS data for the supplied datasets. Chunks are represented as seqs of floats. Be sure to convert chunks to vector before running any sort of data analysis, as seqs require linear time for lookups." [datasets chunk-size source] (let [ks ["SHORTNAME" "TileID" "RANGEBEGINNINGDATE"] chunkifier (p/chunkify chunk-size)] (<- [?datachunk] (source _ ?hdf) (unpack-modis [datasets] ?hdf :> ?dataset ?freetile) (raster-chunks [chunk-size] ?freetile :> ?chunkid ?chunk) (meta-values [ks] ?freetile :> ?productname ?tileid ?date) (split-id ?tileid :> ?mod-h ?mod-v) ((c/juxt #'spatial-res #'temporal-res) ?productname :> ?s-res ?t-res) (chunkifier ?dataset ?date ?s-res ?t-res ?mod-h ?mod-v ?chunkid ?chunk :> ?datachunk))))
  12. ;; Subquery structure (<- /* output-variables */ /* 1-or-more predicates

    */) ;; outputs (<- [?word ?count] ;; generator (gen ?text)
  13. ;; Subquery structure (<- /* output-variables */ /* 1-or-more predicates

    */) ;; outputs (<- [?word ?count] ;; generator (gen ?text) ;; operation (split ?text :> ?word)
  14. ;; Subquery structure (<- /* output-variables */ /* 1-or-more predicates

    */) ;; outputs (<- [?word ?count] ;; generator (gen ?text) ;; operation (split ?text :> ?word) ;; aggregation (c/count ?count))
  15. ;; Function from generator => subquery (defn word-count [gen] (let

    [split (mapcatfn [^String sentence] (.split sentence "\\s+"))] (<- [?word ?count] ;; <- outputs (gen ?text) ;; <- generator (split ?text :> ?word) ;; <- operation (c/count ?count)))) ;; <- aggregation
  16. ;; [follower person] (def follows [["alice" "david"] ["alice" "bob"] ["alice"

    "emily"] ["bob" "david"] ["bob" "george"] ["bob" "luanne"] ["david" "alice"] ["david" "luanne"] ["emily" "alice"] ["emily" "bob"] ["emily" "george"] ["emily" "gary"] ["george" "gary"] ["harold" "bob"] ["luanne" "harold"] ["luanne" "gary"]]) ;; [person age] (def age [["alice" 28] ["bob" 33] ["chris" 40] ["david" 25] ["emily" 25] ["george" 31] ["gary" 28] ["kumar" 27] ["luanne" 36]]) ;; [follower full-name] (def full-names [["alice" "Alice Smith"] ["bob" "Bobby John Johnson"] ["chris" "CHRIS"] ["david" "A B C D E"] ["emily" "Emily Buchanan"] ["george" "George Jett"]]) Find the full name of every person following someone under 30.
  17. Find the full name of every person following someone under

    30. (<- [?full-name] (follows ?follower ?person) (full-names ?follower ?full-name) (age ?person ?age) (:distinct true) (< ?age 30))
  18. Find the full name of every person following someone under

    30. (<- [?full-name] (follows ?follower ?person) (full-names ?follower ?full-name) (age ?person ?age) (:distinct true) (< ?age 30)) Gen: Gen: Gen: Agg: Filter:
  19. Find the full name of every person following someone under

    30. (<- [?full-name] (follows ?follower ?person) (full-names ?follower ?full-name) (age ?person ?age) (:distinct true) (< ?age 30)) Gen: Gen: Gen: Agg: Filter: ;; RESULTS ;; ----------------------- ;; A B C D E ;; Alice Smith ;; Bobby John Johnson ;; Emily Buchanan ;; George Jett ;; -----------------------
  20. “When you specify something to be designed, tell what properties

    you need, not how they are to be achieved.” -Fred Brooks, The Design of Design
  21. Find the full name of every person following someone under

    30. (<- [?full-name] (follows ?follower ?person) (full-names ?follower ?full-name) (age ?person ?age) (:distinct true) (< ?age 30)) Gen: Gen: Gen: Agg: Filter: ;; RESULTS ;; ----------------------- ;; A B C D E ;; Alice Smith ;; Bobby John Johnson ;; Emily Buchanan ;; George Jett ;; -----------------------
  22. (<- [?full-name] (follows ?follower ?person) (full-names ?follower ?full-name) (age ?person

    ?age) (:distinct true) (< ?age 30)) follows ?follower ?person _________________
  23. (<- [?full-name] (follows ?follower ?person) (full-names ?follower ?full-name) (age ?person

    ?age) (:distinct true) (< ?age 30)) follows full-names ?follower ?person ?full-name ?person _________________ ____________________
  24. (<- [?full-name] (follows ?follower ?person) (full-names ?follower ?full-name) (age ?person

    ?age) (:distinct true) (< ?age 30)) ___________ follows full-names age ?follower ?person ?full-name ?person ?person ?age _________________ ____________________
  25. (<- [?full-name] (follows ?follower ?person) (full-names ?follower ?full-name) (age ?person

    ?age) (:distinct true) (< ?age 30)) ___________ follows full-names age ?follower ?person ?full-name ?person ?person ?age insert 30 ?person ?age ?temp1 _________________ ____________________
  26. (<- [?full-name] (follows ?follower ?person) (full-names ?follower ?full-name) (age ?person

    ?age) (:distinct true) (< ?age 30)) ___________ follows full-names age ?follower ?person ?full-name ?person ?person ?age (< ?age ?temp1) insert 30 ?person ?age ?temp1 _________________ ____________________ ________ ?person ?age ?temp1
  27. (<- [?full-name] (follows ?follower ?person) (full-names ?follower ?full-name) (age ?person

    ?age) (:distinct true) (< ?age 30)) ___________ follows full-names age ?follower ?person ?full-name ?person ?person ?age (< ?age ?temp1) insert 30 ?person ?age ?temp1 _________________ ____________________ ________ ?person ?age ?temp1 join on ?person ?person ?follower ?full-name ?age ?temp1
  28. (<- [?full-name] (follows ?follower ?person) (full-names ?follower ?full-name) (age ?person

    ?age) (:distinct true) (< ?age 30)) __________ ___________ follows full-names age ?follower ?person ?full-name ?person ?person ?age (< ?age ?temp1) insert 30 ?person ?age ?temp1 _________________ ____________________ ________ ?person ?age ?temp1 join on ?person ?person ?follower ?full-name ?age ?temp1 distinct on ?full-name ?full-name
  29. ;; we dispatch on type here so we can use

    function metadata. (defmulti to-predicate (fn [op input output] (type op))) (defprotocol ICouldFilter (filter? [this] "filter? returns true if, given no input or output signifier, the operation takes inputs by default, false if outputs by default.")) (defprotocol IPlatform (generator? [this candidate] "Returns true if the supplied candidate can become a generator, false otherwise.") (generator [this candidate output-fields options] "Returns a tuple producer, in the world of the implementing Platform.") (plan [this query] "Accepts a Cascalog subquery and compiles it down into some notion of a plan in the target platform's world."))
  30. ;; default filters: (extend-protocol ICouldFilter Object (filter? [_] false) clojure.lang.Fn

    (filter? [_] true) clojure.lang.Var (filter? [v] (fn? @v)) clojure.lang.MultiFn (filter? [_] true))
  31. ;; default filters: (extend-protocol ICouldFilter Object (filter? [_] false) clojure.lang.Fn

    (filter? [_] true) clojure.lang.Var (filter? [v] (fn? @v)) clojure.lang.MultiFn (filter? [_] true)) ;; Cascading extension: (extend-protocol p/ICouldFilter cascading.operation.Filter (filter? [_] true))
  32. TAKEAWAYS • Let the system reduce complexity for you. •

    Use the properties of your data • Share data by sharing code!