Slide 1

Slide 1 text

CASCALOG 2.0 Sam Ritchie :: @sritchie :: Clojure/Conj 2013 Datalog in Realtime

Slide 2

Slide 2 text

CASCALOG 2.0 Sam Ritchie :: @sritchie :: Clojure/Conj 2013 Datalog in Realtime

Slide 3

Slide 3 text

AGENDA

Slide 4

Slide 4 text

• What is Cascalog? + Examples AGENDA

Slide 5

Slide 5 text

• What is Cascalog? + Examples • Why Logic Programming? AGENDA

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

• What is Cascalog? + Examples • Why Logic Programming? • How Cascalog Compiles Datalog to MapReduce • Different Compilation Targets • What’s Next? AGENDA

Slide 9

Slide 9 text

:)

Slide 10

Slide 10 text

WHAT IS CASCALOG?

Slide 11

Slide 11 text

WHAT IS CASCALOG? • Datalog DSL in that helps you write

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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!)

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

(defn word-count [gen] (let [split (mapcatfn [^String sentence] (.split sentence "\\s+"))] (<- [?word ?count] (gen ?text) (split ?text :> ?word) (c/count ?count))))

Slide 17

Slide 17 text

;; 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

Slide 18

Slide 18 text

(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

Slide 19

Slide 19 text

(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))))

Slide 20

Slide 20 text

;; Subquery structure (<- /* output-variables */ /* 1-or-more predicates */)

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

;; Cascalog Predicate (split ?text :> ?word) “Operation” Inputs Outputs

Slide 26

Slide 26 text

;; Cascalog Predicate [split “?text” :> “?word”] “Operation” Inputs Outputs

Slide 27

Slide 27 text

;; 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

Slide 28

Slide 28 text

;; [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.

Slide 29

Slide 29 text

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))

Slide 30

Slide 30 text

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:

Slide 31

Slide 31 text

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 ;; -----------------------

Slide 32

Slide 32 text

ABSTRACTION LAYERS :’(

Slide 33

Slide 33 text

ABSTRACTION LAYERS :’( :-\

Slide 34

Slide 34 text

ABSTRACTION LAYERS :’( :-\ └ʢ˒̾˒ʣ┐

Slide 35

Slide 35 text

ABSTRACTION LAYERS └ʢ˒̾˒ʣ┐ ?

Slide 36

Slide 36 text

ABSTRACTION LAYERS └ʢ˒̾˒ʣ┐ ?

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Why Datalog?

Slide 39

Slide 39 text

└ʢ˒̾˒ʣ┐

Slide 40

Slide 40 text

“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

Slide 41

Slide 41 text

Compiling Cascalog

Slide 42

Slide 42 text

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 ;; -----------------------

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

(<- [?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 _________________ ____________________

Slide 46

Slide 46 text

(<- [?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 _________________ ____________________

Slide 47

Slide 47 text

(<- [?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 _________________ ____________________

Slide 48

Slide 48 text

(<- [?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

Slide 49

Slide 49 text

(<- [?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

Slide 50

Slide 50 text

(<- [?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

Slide 51

Slide 51 text

Does this really look like ?

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

Datalog Logical Plan Optimizers CUSTOM PLATFORMS IN 2.0

Slide 54

Slide 54 text

;; 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."))

Slide 55

Slide 55 text

;; 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))

Slide 56

Slide 56 text

;; 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))

Slide 57

Slide 57 text

POSSIBLE PLATFORMS

Slide 58

Slide 58 text

POSSIBLE PLATFORMS • “Cascalog in the Small”: Native Clojure

Slide 59

Slide 59 text

POSSIBLE PLATFORMS • “Cascalog in the Small”: Native Clojure • ClojureScript?

Slide 60

Slide 60 text

POSSIBLE PLATFORMS • “Cascalog in the Small”: Native Clojure • ClojureScript? • core.async?

Slide 61

Slide 61 text

POSSIBLE PLATFORMS • “Cascalog in the Small”: Native Clojure • ClojureScript? • core.async? • Storm

Slide 62

Slide 62 text

TAKEAWAYS

Slide 63

Slide 63 text

TAKEAWAYS • Let the system reduce complexity for you.

Slide 64

Slide 64 text

TAKEAWAYS • Let the system reduce complexity for you. • Use the properties of your data

Slide 65

Slide 65 text

TAKEAWAYS • Let the system reduce complexity for you. • Use the properties of your data • Share data by sharing code!

Slide 66

Slide 66 text

Sam Ritchie :: @sritchie :: Clojure/Conj 2013 Questions?