Last active
August 29, 2015 13:57
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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