Coder Social home page Coder Social logo

Libraries/Functions in closures about pigpen HOT 7 CLOSED

netflix avatar netflix commented on July 26, 2024
Libraries/Functions in closures

from pigpen.

Comments (7)

mbossenbroek avatar mbossenbroek commented on July 26, 2024

This just means that you can't close over a compiled function. For example:

(require '[simple-time.core :as st]))

;; works
(defn format-ts [data]
  (pig/map (fn [x] (st/format x :date)) data))

(format-ts my-data)

;; works
(defn format-ts [data format]
  (pig/map (fn [x] (st/format x format)) data))

(format-ts my-data :date)

;; won't work because f is compiled
(defn format-ts [data f]
  (pig/map f data))

(format-ts my-data (fn [x] (st/format x :date)))

There is a way around this, but it's not officially supported yet:

(defn format-ts [data f]
  (pigpen.map/map* f data))

(format-ts my-data (pigpen.code/trap (fn [x] (st/format x :date))))

Let me know if that's not clear or if you have a specific example of what you're trying to do.

Also, check out [email protected] or https://groups.google.com/forum/#!forum/pigpen-support for future questions.

from pigpen.

ljank avatar ljank commented on July 26, 2024

I still get CompilerException java.lang.RuntimeException: No such namespace: st in cases that meant to be working :\

from pigpen.

mbossenbroek avatar mbossenbroek commented on July 26, 2024

Could you send a code sample and stack trace that you get?

from pigpen.

mbossenbroek avatar mbossenbroek commented on July 26, 2024

Might be worth mentioning - any code that you close over needs to be in a file that will end up in the uberjar that goes to hadoop. If you're just in a user ns in a repl, the code I listed won't work.

If that's the case, let me know if that's not clear from the docs & I can update them.

from pigpen.

ljank avatar ljank commented on July 26, 2024

I've spotted that it behaves differently when using pig/return and loading data from files (same for JSON and Avro). This works just fine:

(require '[simple-time.core :as st])

(defn time->ymd
  [data]
  (pig/map (fn [entry]
             (assoc entry
               :ymd (st/format (st/datetime (:time entry)) :date)))
           data))

(->> (pig/return [{:time 1425254400010} {:time 1425254400019} {:time 1425254400090}])
     (time->ymd)
     (pig/dump))
; [{:ymd "2015-03-02", :time 1425254400010} 
;  {:ymd "2015-03-02", :time 1425254400019} 
;  {:ymd "2015-03-02", :time 1425254400090}]

For JSON:

(spit "/tmp/events.json" "{\"time\": 1425254400010}\n{\"time\": 1425254400019}\n{\"time\": 1425254400090}")
(->> (pig/load-json "/tmp/events.json")
     (time->ymd)
     (pig/dump))

CompilerException java.lang.RuntimeException: No such namespace: st

Same error while using Avro.

from pigpen.

mbossenbroek avatar mbossenbroek commented on July 26, 2024

Yeah, it sounds like you're in a user ns. This complete example works for me:

(ns pigpen-demo.core
  (:require [pigpen.core :as pig]
            [simple-time.core :as st]))

(defn time->ymd
  [data]
  (pig/map (fn [entry]
             (assoc entry
               :ymd (st/format (st/datetime (:time entry)) :date)))
           data))

(clojure.pprint/pprint
  (->> (pig/return [{:time 1425254400010} {:time 1425254400019} {:time 1425254400090}])
       (time->ymd)
       (pig/dump)))

(spit "/tmp/events.json" "{\"time\": 1425254400010}\n{\"time\": 1425254400019}\n{\"time\": 1425254400090}")

(clojure.pprint/pprint
  (->> (pig/load-json "/tmp/events.json")
       (time->ymd)
       (pig/dump)))

and produces this output:

[{:ymd "2015-03-01", :time 1425254400010}
 {:ymd "2015-03-01", :time 1425254400019}
 {:ymd "2015-03-01", :time 1425254400090}]
Start reading from  /tmp/events.json
Stop reading from  /tmp/events.json
[{:ymd "2015-03-01", :time 1425254400010}
 {:ymd "2015-03-01", :time 1425254400019}
 {:ymd "2015-03-01", :time 1425254400090}]
nil

If you're in a file & still getting that exception, could you run these commands in the REPL and let me know what you get?

(pigpen.code/trap identity)
(ns-name *ns*)
(pigpen.code/ns-exists *1)

from pigpen.

ljank avatar ljank commented on July 26, 2024

You're right — everything works fine when running from file and being not in a user namespace. Thank you for lightning fast and correct diagnosis!

Next time I'll use mailgroup. Sorry!

from pigpen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.