Browse Source

Add function to allow for query execution without validation, fixes #25 (#26)

* Add function to allow for query execution without validation, fixes #25

- Using s/defn to allow for client to decide when to use validation
- Add new function (`execute`) to keep backward compatability
- Fixed error in schema for `timeseries`
- Updated README.md
- Remove use of swiss-arrow

* as pr review
master
Erik Assum 5 years ago
committed by Guillaume Buisson
parent
commit
37fc967f00
  1. 16
      README.md
  2. 34
      src/clj_druid/client.clj
  3. 2
      src/clj_druid/schemas/query.clj

16
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

34
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))))

2
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))

Loading…
Cancel
Save