Skip to content

Instantly share code, notes, and snippets.

@robinkraft
Last active August 29, 2015 13:57
Show Gist options
  • Save robinkraft/9625937 to your computer and use it in GitHub Desktop.
Save robinkraft/9625937 to your computer and use it in GitHub Desktop.
n-obs: wrapper for Cascalog's "first-n" operation. Returns N observations of all fields for a given source.
(use 'cascalog.api)
(require '[cascalog.ops :as c])
(defn field-parser
"Convert a string to a fieldname string. If string already
starts with `?` or `!`, return the string directly.
Usage:
(field-parser [\"a\" \"b\"])
;=> [\"?a\" \"?\"]"
[field]
(if (or (= \? (first field))
(= \! (first field)))
field
(str "?" field)))
(defn field-ify
"Convert vector of strings into fieldname strings a la `?a`."
[field-strings]
(vec (map field-parser field-strings)))
(defn n-obs
"Returns a tap that will return N observations for all fields
for a given source."
[n field-names src]
(let [fields (field-ify field-names)
query (<- fields
(src :>> fields))]
(<- fields
((c/first-n query n) :>> fields))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment