-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: stdout sink * feat: op kafka-to-std-out * feat: es-to-stdout operation * feat: source stdin; ops: stdin-to-es, stdin-to-kafka
- Loading branch information
1 parent
9b67fbe
commit d7edb0a
Showing
18 changed files
with
326 additions
and
9 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"max_docs": 10000, | ||
"source": { | ||
"remote": { | ||
"host": "http://localhost:9200" | ||
}, | ||
"index": ".kibana", | ||
"query": { | ||
"size": 2000 | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"max_docs": 1, | ||
"source": { | ||
"topic": "source-topic", | ||
"bootstrap.servers": "127.0.0.1:9092" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"max_docs": 1, | ||
"sink": { | ||
"connection.url": "http://localhost:9200", | ||
"dest.index": "dest-index-name2" | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
(ns ops.es-to-stdout | ||
(:require [sink :as sink] | ||
[source :as source])) | ||
|
||
(def default-opts | ||
{:max_docs nil | ||
:source {:implementation :elasticsearch | ||
:remote {:host "http://localhost:9200"} | ||
:index "*" | ||
:query {:sort ["_doc"] | ||
:size 2000}} | ||
:sink {:implementation :stdout}}) | ||
|
||
(defn execute | ||
"Reads documents from Elasticsearch and writes them to stdout" | ||
[opts] | ||
(sink/store! | ||
(source/fetch! (assoc opts :source (merge (:source default-opts) | ||
(:source opts)))) | ||
(assoc opts :sink | ||
(merge (:sink default-opts) | ||
(:sink opts))))) | ||
|
||
(comment | ||
(execute | ||
{:max_docs 1 | ||
:source {:implementation :elasticsearch | ||
:remote {:host "http://localhost:9200"} | ||
:index ".kibana" | ||
:query {:sort ["_doc"] | ||
:size 2000}}})) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
(ns ops.kafka-to-stdout) | ||
|
||
(def default-opts | ||
{:max_docs 5 | ||
:source {:implementation :kafka | ||
:bootstrap.servers "127.0.0.1:9092" | ||
:topic nil | ||
:decode-value? false | ||
:impatient? true} | ||
:sink {:implementation :stdout}}) | ||
|
||
(defn execute | ||
"Reads records from Kafka and writes them to STDOUT." | ||
[opts] | ||
(let [source-opts (merge (:source default-opts) (:source opts)) | ||
sink-opts (merge (:sink default-opts) (:sink opts)) | ||
records (source/fetch! (assoc opts :source source-opts))] | ||
(sink/store! records (assoc opts :sink sink-opts)))) | ||
|
||
(comment | ||
(execute | ||
{:max_docs 1 | ||
:source {:topic "source-topic" | ||
:bootstrap.servers "127.0.0.1:9092"}})) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
(ns ops.stdin-to-es | ||
(:require [sink.elasticsearch.index :as indexer] | ||
[source :as source] | ||
[jsonista.core :as json])) | ||
|
||
(def default-opts | ||
{:max_docs 5 | ||
:source {:implementation :stdin | ||
:decode-value? true} | ||
:sink (merge {:implementation :elasticsearch} | ||
indexer/default-opts)}) | ||
|
||
(defn execute [opts] | ||
(let [stdin-records (source/fetch! (assoc opts :source (merge (:source default-opts) | ||
(:source opts))))] | ||
(indexer/store! stdin-records | ||
(merge | ||
(:sink default-opts) | ||
(:sink opts))))) | ||
|
||
(comment | ||
(with-in-str | ||
(json/write-value-as-string | ||
{:_id "123" :_source {:foo "bar"}}) | ||
(ops.stdin-to-es/execute | ||
{:max_docs 1 | ||
:source {:implementation :stdin | ||
:decode-value? true} | ||
:sink {:connection.url "http://localhost:9200" | ||
:dest.index "dest-index-name"}}))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
(ns ops.stdin-to-kafka | ||
(:require [sink :as sink] | ||
[source :as source] | ||
[jsonista.core :as json]) | ||
(:import (sink KafkaRecord))) | ||
|
||
(def default-es-to-kafka-config | ||
{:max_docs nil | ||
:source {:implementation :stdin | ||
:decode-value? true} | ||
:sink {:implementation :kafka | ||
:topic "sink-topic" | ||
:bootstrap.servers "127.0.0.1:9092"}}) | ||
|
||
(defn es-record? [record] | ||
(and (contains? record :_id) | ||
(contains? record :_source))) | ||
|
||
(defn execute [opts] | ||
(sink/store! | ||
(map (fn [record] | ||
(if (string? record) | ||
(KafkaRecord. nil record nil) | ||
(if (es-record? record) | ||
(KafkaRecord. | ||
(get record :_id (get record :_id)) | ||
(get record :_source (get record :_source)) | ||
(dissoc record :_id :_source)) | ||
record))) | ||
(source/fetch! (assoc opts :source (merge (:source default-es-to-kafka-config) | ||
(:source opts))))) | ||
(assoc opts :sink | ||
(merge (:sink default-es-to-kafka-config) | ||
(:sink opts))))) | ||
|
||
(comment | ||
(with-in-str | ||
(json/write-value-as-string | ||
{:_id "123" :_source {:foo "bar"}}) | ||
(ops.stdin-to-kafka/execute | ||
{:max_docs 1 | ||
:source {:implementation :stdin | ||
:decode-value? true} | ||
:sink {:topic "stdin-to-kafka-test" | ||
:bootstrap.servers "127.0.0.1:9092"}})) | ||
|
||
(with-in-str | ||
(json/write-value-as-string | ||
{:_id "123" :_source {:foo "bar"}}) | ||
(ops.stdin-to-kafka/execute | ||
{:max_docs 1 | ||
:source {:implementation :stdin | ||
:decode-value? false} | ||
:sink {:topic "stdin-to-kafka-test" | ||
:bootstrap.servers "127.0.0.1:9092"}})) | ||
|
||
(with-in-str | ||
(slurp "kafka-file.json") | ||
(ops.stdin-to-kafka/execute | ||
{:max_docs 1 | ||
:source {:implementation :stdin | ||
:decode-value? true} | ||
:sink {:topic "stdin-to-kafka-test" | ||
:bootstrap.servers "127.0.0.1:9092"}})) | ||
|
||
(with-in-str | ||
(slurp "kafka-file.json") | ||
(ops.stdin-to-kafka/execute | ||
{:max_docs 1 | ||
:source {:implementation :stdin | ||
:decode-value? false} | ||
:sink {:topic "stdin-to-kafka-test" | ||
:bootstrap.servers "127.0.0.1:9092"}}))) |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
(ns sink.stdout | ||
(:require [jsonista.core :as json]) | ||
(:import (java.io BufferedWriter PrintWriter))) | ||
|
||
(defn store! | ||
"JSON encodes each record and writes it to stdout." | ||
[records opts] | ||
(let [^PrintWriter writer (PrintWriter. (BufferedWriter. *out* (* 1024 8192)))] | ||
(doseq [record records] | ||
(.println writer (json/write-value-as-string record))) | ||
(.flush writer))) | ||
|
||
(comment | ||
(sink.stdout/store! | ||
[{:value "line1"} | ||
{:value "line2"}] | ||
{:sink {}})) |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
(ns source.stdin | ||
(:require [clojure.core.async :as a] | ||
[core.async :as async] | ||
[core.json :as json]) | ||
(:import (java.io BufferedReader))) | ||
|
||
(def defaults | ||
{:max_docs 1 | ||
:source {:implementation :stdin | ||
:decode-value? true}}) | ||
|
||
(defn fetch | ||
"Reads lines from STDIN and returns a lazy sequence of lines." | ||
[opts] | ||
(let [decode-value? (get-in opts [:source :decode-value?] true) | ||
line-in-chan (a/chan 128)] | ||
(a/go | ||
(with-open [^BufferedReader rdr (BufferedReader. *in*)] | ||
(loop [^String line (.readLine rdr)] | ||
(if (= nil line) | ||
(a/close! line-in-chan) | ||
(do | ||
(a/>!! line-in-chan | ||
(if decode-value? (json/decode line) line)) | ||
(recur (.readLine rdr))))))) | ||
(if-let [max-docs (get opts :max_docs)] | ||
(take max-docs (async/seq-of-chan line-in-chan)) | ||
(async/seq-of-chan line-in-chan)))) | ||
|
||
(comment | ||
(with-in-str | ||
"{\"foo\":\"bar\"}" | ||
(println | ||
(source.stdin/fetch | ||
{:max_docs 1 | ||
:source {:decode-value? true}})))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
(ns sink.stdout-test | ||
(:require [clojure.test :refer :all] | ||
[clojure.string :as str] | ||
[sink.stdout :as stdout])) | ||
|
||
(deftest sinking-to-stdout | ||
(let [records [{:value "line1"} | ||
{:value "line2"}] | ||
opts {:sink {}}] | ||
(is (= "{\"value\":\"line1\"}\n{\"value\":\"line2\"}" | ||
(str/trim | ||
(with-out-str | ||
(stdout/store! records opts))))))) |
Oops, something went wrong.