diff --git a/README.md b/README.md index a660bdb..48e0568 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,21 @@ Issue druid queries supplying (query client random (:queryType q) q) (close client)) ``` - +To run a query _without_ runtime validation of the query against a schema (using +the `q` from above: +```clj + (let [client (connect {:zk {:host "127.0.0.1:2181" + :discovery-path "/druid/discovery" + :node-type "druid:broker"}})] + (execute client random q) + (close client)) +;; if you want to validate + (let [client (connect {:zk {:host "127.0.0.1:2181" + :discovery-path "/druid/discovery" + :node-type "druid:broker"}})] + (s/with-fn-validation (execute client random q)) ;; s is plumatic/schema + (close client)) +``` ### Example An example using compojure-api to spawn a full druid API is located in the examples folder diff --git a/src/clj_druid/client.clj b/src/clj_druid/client.clj index 023120a..1c36348 100644 --- a/src/clj_druid/client.clj +++ b/src/clj_druid/client.clj @@ -4,8 +4,8 @@ [clojure.data.json :as json] [clj-druid.schemas.query :as sch] [clj-druid.validations :as v] - [swiss.arrows :refer :all] - [clj-http.client :as http])) + [clj-http.client :as http] + [schema.core :as s])) (defprotocol ^:private Client (close [this] @@ -76,17 +76,23 @@ (curator-client (:zk params)) (static-client (:hosts params)))) -(defn query +(s/defn execute "Issue a druid query" + ([client balance-strategy druid-query :- sch/query] + (execute client balance-strategy druid-query {})) + ([client balance-strategy druid-query http-params] + (let [nodes (nodes client)] + (when (empty? nodes) + (throw (Exception. + "No druid node available for query"))) + (http/post (balance-strategy nodes) + (merge {:body (json/write-str druid-query) + :as :text + :content-type :json} + http-params))))) +(defn query + "Issue a druid query with validation" [client balance-strategy query-type druid-query & params] - (let [params (apply hash-map params) - options (-<> (into druid-query {:queryType query-type}) - (v/validate query-type) - (json/write-str <>) - {:body <> :as :text} - (merge params)) - nodes (nodes client)] - (when (empty? nodes) - (throw (Exception. - "No druid node available for query"))) - (http/post (balance-strategy nodes) options))) + (let [http-params (apply hash-map params) + query (assoc druid-query :queryType query-type)] + (s/with-fn-validation (execute client balance-strategy query http-params)))) diff --git a/src/clj_druid/schemas/query.clj b/src/clj_druid/schemas/query.clj index 65473e2..5bb2775 100644 --- a/src/clj_druid/schemas/query.clj +++ b/src/clj_druid/schemas/query.clj @@ -112,7 +112,7 @@ TopNs are much faster and resource efficient than GroupBys for this use case." #(= :search (:queryType %)) search #(= :segmentMetadata (:queryType %)) segmentMetadata #(= :timeBoundary (:queryType %)) timeBoundary - #(= :timeSeries (:queryType %)) timeseries + #(= :timeseries (:queryType %)) timeseries #(= :topN (:queryType %)) topN #(= :select (:queryType %)) select))