-
Notifications
You must be signed in to change notification settings - Fork 47
/
kafka.clj
154 lines (110 loc) · 4.2 KB
/
kafka.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
(ns com.brunobonacci.mulog.publishers.kafka
(:require [com.brunobonacci.mulog.publisher :as p]
[com.brunobonacci.mulog.agents :as ag]
[com.brunobonacci.mulog.buffer :as rb]
[cheshire.core :as json]
[clojure.string :as str])
(:import [java.util Map]
[org.apache.kafka.clients.producer KafkaProducer ProducerRecord RecordMetadata]
[org.apache.kafka.common.serialization StringSerializer Serializer]))
(defn- normalize-config
[config]
(->> config
(merge {:key.serializer StringSerializer
:value.serializer StringSerializer})
(map (fn [[k v]]
[(name k)
(cond
(string? v) v
(keyword? v) (name v)
(number? v) (str v)
(class? v) (.getName ^Class v)
(nil? v) v
:else (str v))]))
(into {})))
(defn producer
([config]
(KafkaProducer. ^Map (normalize-config config)))
([config key-serde value-serde]
(KafkaProducer. ^Map (normalize-config config) ^Serializer key-serde ^Serializer value-serde)))
(defn- RecordMetadata->data
[^RecordMetadata rm]
{:topic-name (.topic rm)
:partition (.partition rm)
:timestamp (.timestamp rm)
:offset (.offset rm)
:serialized-key-size (.serializedKeySize rm)
:serialized-value-size (.serializedValueSize rm)})
(defn send!
[^KafkaProducer producer ^String topic key value]
(.send producer (ProducerRecord. topic key value)))
(comment
(def kcfg {:bootstrap.servers "localhost:9092"
:key.serializer StringSerializer
:value.serializer StringSerializer})
(def kp (producer kcfg))
(RecordMetadata->data @(send! kp "mulog" "key1" "value1"))
)
(defn- publish-records!
[{:keys [key-field format topic producer*] :as config} records]
(let [fmt* (if (= :json format) json/generate-string pr-str)]
(->> records
(map (juxt #(get % key-field) fmt*))
(map (fn [[k v]] (send! producer* topic k v)))
(doall))))
(comment
(publish-records! {:key-field :puid :format :json :topic "mulog" :producer* kp}
[{:timestamp (System/currentTimeMillis) :event-name :hello :k 1}
{:timestamp (System/currentTimeMillis) :event-name :hello :k 2}])
)
(defn deep-merge
"Like merge, but merges maps recursively. It merges the maps from left
to right and the right-most value wins. It is useful to merge the
user defined configuration on top of the default configuration."
[& maps]
(let [maps (filter (comp not nil?) maps)]
(if (every? map? maps)
(apply merge-with deep-merge maps)
(last maps))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; ----==| K A F K A |==---- ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(deftype KafkaPublisher
[config buffer]
com.brunobonacci.mulog.publisher.PPublisher
(agent-buffer [_]
buffer)
(publish-delay [_]
(:publish-delay config))
(publish [_ buffer]
;; items are pairs [offset <item>]
(let [items (take (:max-items config) (rb/items buffer))
last-offset (-> items last first)]
(if-not (seq items)
buffer
;; else send to kafka
(do
(publish-records! config (map second items))
(rb/dequeue buffer last-offset))))))
(def ^:const DEFAULT-CONFIG
{:max-items 1000
:publish-delay 1000
:kafka {;; the comma-separated list of brokers to connect
;; :bootstrap.servers "localhost:9092"
;; you can add more kafka connection properties here
}
:topic "mulog"
;; one of: :json, :edn
:format :json
:key-field :puid
})
(defn kafka-publisher
[config]
{:pre [(get-in config [:kafka :bootstrap.servers])]}
(KafkaPublisher.
(as-> config $
(deep-merge DEFAULT-CONFIG $)
(assoc $ :producer* (producer (:kafka $))))
(ag/buffer-agent 10000)))