                      :host "localhost"
                      :port 8123
                      :database "default"
-                     :table "riemann"}
-                    opts)]
+                     :table "riemann"
+                     :username "default"
+                     :password ""}
+                    opts)
+        _ (post-datapoint (generate-create-url opts) (generate-create-query opts))]
     (fn [events]
-      (let [url (generate-url opts)
+      (let [insert-url (generate-insert-url opts)
             datapoint (generate-datapoint-batch events)]
         (if-not (nil? datapoint)
-          (post-datapoint url datapoint))))))+          (post-datapoint insert-url datapoint))))))
   - `:port`           ClickHouse Server Port (default: 8123)
   - `:database`       ClickHouse Database Name (default: \"default\")
   - `:table`          ClickHouse Table Name (default: \"riemann\")

   You need to first create a clickhouse table:

   CREATE TABLE default.riemann
   (
      `timestamp` DateTime,
      `host` String,

  (let [processed-events (map generate-datapoint events)]
    (str/join "" processed-events)))

(defn generate-create-query
  "ClickHouse query for creating table"
  [opts]
  (let [database (:database opts)
        table    (:table opts)]
    (str "CREATE TABLE IF NOT EXISTS " database "." table "(
            `timestamp` DateTime,
            `host` String,
            `service` String,
            `metric` Float32,
            `tags` Array(String)
         )
         ENGINE = MergeTree
         PARTITION BY toYYYYMM(timestamp)
         ORDER BY (timestamp, host, service)
         SETTINGS index_granularity = 8192;")))

(defn generate-create-url
  "Generates the URL to which create table query should be posted."
  [opts]
  (let [scheme   (:scheme opts)
        host     (:host opts)
        port     (:port opts)
        username (:username opts)
        password (:password opts)]
    (str scheme username ":" password "@" host ":" port)))

(defn generate-insert-url
  "Generates the URL to which datapoint should be posted."
  [opts]
  (let [scheme   (:scheme opts)
        host     (:host opts)
        port     (:port opts)
        database (:database opts)
        table    (:table opts)
        username (:username opts)
        password (:password opts)
        param    (str "INSERT INTO " database "." table " FORMAT CSV")]
    (str scheme username ":" password "@" host ":" port "/?query=" param)))

(defn post-datapoint
  "Post the riemann event as clickhouse datapoint."
