-
Notifications
You must be signed in to change notification settings - Fork 11
/
kafka.clj
223 lines (165 loc) · 8.05 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
(ns dvlopt.kafka
"Start here.
This library aims to be clojure idiomatic while not being too smart about wrapping the java libraries, so that upgrading
will not be laborious in the future.
It is organized by specific namespaces. Producers (dvlopt.kafka.out) produce records, consumers (dvlopt.kafka.in)
consume records, and administrators can alter Kafka (dvlopt.kafka.admin). For anything related to Kafka Streams, see the
`dvlopt.kstreams` namespace.
Records
=======
A record is a map containing at least a ::topic. It might also hold :
::headers
::offset
::key
::timestamp
::value
Obviously, the offset cannot be decided by the user when sending the record. Headers allow records to have metadata
clearly distinct from the value. They exerce the same role as in other protocols such as HTTP. They consist of a list
of key-values where the order might be important. Keys are arbitrary strings and can appear more than once. Values are
arbitrary byte arrays or may be missing.
Connecting to nodes
===================
Opening a resource such as a producer requires a list of nodes (ie. list of [host port]). If not provided, the library
tries to reach [[\"localhost\" 9092]]. When described, a node is a map containing ::host, ::port, ::id (numerical
identification), and ::rack when one is assigned. A replica node also contains the attribute ::synced?.
Ser/de
======
Within Kafka, records are kept as transparent byte arrays. When sending records, the key and the value need to be
serialized and when consuming some, deserialized.
A serializer is a function mapping some data to a byte array or nil. It is needed for producing records.
Ex. (fn serializer [data metadata]
(some-> v
nippy/freeze))
A deserializer does the opposite, it maps a byte array (or nil) to a value. It is needed for consuming records.
Ex. (fn deserializer [ba metadata]
(some-> ba
nippy/thaw))
Both type of functions are often used throughout the library. The provided metadata might help the user to decide on
how to ser/de the data. It is a map containing the ::topic involved as well as ::headers when there are some.
Built-in deserializers and serializers are available at `deserializers` and `serializers` respectively. Because ser/de
is so common, a key word specifying one of the built-in functions can be provided :
:boolean
:byte-array
:byte-buffer
:double
:integer
:long
:string
If a ser/de is not provided by the user when needed, the default is :byte-array.
Time
====
All time intervals such as timeouts, throughout all this library, are expressed as [Duration Unit] where
Duration is coerced to an integer and Unit is one of :
:nanoseconds
:microseconds
:milliseconds
:seconds
:minutes
:hours
:days
Ex. [5 :seconds]"
{:author "Adam Helinski"}
(:import (org.apache.kafka.common.serialization ByteArrayDeserializer
ByteArraySerializer
ByteBufferDeserializer
ByteBufferSerializer
Deserializer
DoubleDeserializer
DoubleSerializer
IntegerDeserializer
IntegerSerializer
LongDeserializer
LongSerializer
Serializer
StringDeserializer
StringSerializer)))
;;;;;;;;;; Serde
(def deserializers
"Built-in deserializers."
(reduce-kv (fn prepare-deserializer [deserializers' kw-type ^Deserializer original-deserializer]
(assoc deserializers'
kw-type
(with-meta (fn deserialize
([x]
(deserialize x
nil))
([x metadata]
(.deserialize original-deserializer
nil
x)))
{::original-deserializer original-deserializer})))
{}
{:boolean (reify Deserializer
(close [_]
nil)
(configure [_ _ _]
nil)
(deserialize [_ _topic ba]
(if (nil? ba)
nil
(not (zero? (aget ba
0))))))
:byte-array (ByteArrayDeserializer.)
:byte-buffer (ByteBufferDeserializer.)
:double (DoubleDeserializer.)
:integer (IntegerDeserializer.)
:long (LongDeserializer.)
:string (StringDeserializer.)}))
(def serializers
"Built-in serializers."
(reduce-kv (fn prepare-serializer [serializers' kw-type ^Serializer original-serializer]
(assoc serializers'
kw-type
(with-meta (fn serialize
([x]
(serialize x
nil))
([x metadata]
(.serialize original-serializer
nil
x)))
{::original-serializer original-serializer})))
{}
{:boolean (reify Serializer
(close [_]
nil)
(configure [_ _ _]
nil)
(serialize [_ _topic bool]
(if (nil? bool)
nil
(let [ba (byte-array 1)]
(when bool
(aset-byte ba
0
1))
ba))))
:byte-array (ByteArraySerializer.)
:byte-buffer (ByteBufferSerializer.)
:double (DoubleSerializer.)
:integer (IntegerSerializer.)
:long (LongSerializer.)
:string (StringSerializer.)}))
;;;;;;;;;; Misc
(def defaults
"Default values and options used throughout this library."
{::deserializer.key :byte-array
::deserializer.value :byte-array
::internal? false
::nodes [["localhost" 9092]]
::serializer.key :byte-array
::serializer.value :byte-array
:dvlopt.kafka.admin/name-pattern [:any]
:dvlopt.kafka.admin/number-of-partitions 1
:dvlopt.kafka.admin/operation :all
:dvlopt.kafka.admin/permission :any
:dvlopt.kafka.admin/replication-factor 1
:dvlopt.kafka.admin/resource-type :any
:dvlopt.kstreams/offset-reset :earliest
:dvlopt.kstreams.store/cache? true
:dvlopt.kstreams.store/changelog? true
:dvlopt.kstreams.store/duplicate-keys? false
:dvlopt.kstreams.store/lru-size 0
:dvlopt.kstreams.store/retention [1 :days]
:dvlopt.kstreams.store/segments 2
:dvlopt.kstreams.store/type :kv.regular})