How to run aggregations on data?

9 views
Skip to first unread message

Punit Naik

unread,
Feb 25, 2016, 8:19:36 AM2/25/16
to PigPen Support
I have a json linke this:

{:pagePath "/" :timeSpent 23}
{:pagePath "/w" :timeSpent 3}
{:pagePath "/" :timeSpent 211}
{:pagePath "/" :timeSpent 86}
{:pagePath "/w" :timeSpent 2}

And I want total time spent on a page. So I want this output:

{:pagePath "/" :totalTimeSpent 320}
{:pagePath "/" :totalTimeSpent 5}


I know I have to do a "group-by" on "pagePath" but I am a bit confused. Can anyone show me a PigPen solution for this?

Matt Bossenbroek

unread,
Feb 25, 2016, 11:49:03 AM2/25/16
to Punit Naik, PigPen Support
There are two options:

=> (->>

     (pig/return [{:pagePath "/" :timeSpent 23}

                  {:pagePath "/w" :timeSpent 3}

                  {:pagePath "/" :timeSpent 211}

                  {:pagePath "/" :timeSpent 86}

                  {:pagePath "/w" :timeSpent 2}])

     (pig/group-by :pagePath)

     (pig/map (fn [[pagePath vs]]

                {:pagePath pagePath

                 :timeSpent (->> vs

                              (map :timeSpent)

                              (reduce +))}))

     (pig/dump))

({:pagePath "/", :timeSpent 320} {:pagePath "/w", :timeSpent 5})



Or with a fold:

=> (->>

     (pig/return [{:pagePath "/" :timeSpent 23}

                  {:pagePath "/w" :timeSpent 3}

                  {:pagePath "/" :timeSpent 211}

                  {:pagePath "/" :timeSpent 86}

                  {:pagePath "/w" :timeSpent 2}])

     (pig/group-by :pagePath {:fold (->>

                                      (fold/map :timeSpent)

                                      (fold/sum))})

     (pig/map (fn [[pagePath timeSpent]]

                {:pagePath pagePath

                 :timeSpent timeSpent}))

     (pig/dump))

({:pagePath "/", :timeSpent 320} {:pagePath "/w", :timeSpent 5})




-Matt

--
You received this message because you are subscribed to the Google Groups "PigPen Support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pigpen-suppor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages