21
21
from warnings import warn
22
22
23
23
24
+ def byteDeserializer (topic , payload ):
25
+ """
26
+ byteDeserializer returns an unaltered payload to the caller
27
+ """
28
+
29
+ return payload
30
+
31
+
24
32
class Consumer (_impl ):
25
33
"""
26
- Create a new Kafka Consumer instance with or without serializer support .
34
+ Create a new Kafka Consumer instance.
27
35
28
36
To avoid spontaneous calls from non-Python threads all callbacks will only be served upon
29
37
calling ```client.poll()``` or ```client.flush()```.
@@ -51,31 +59,24 @@ def __new__(cls, *args, **kwargs):
51
59
52
60
class DeserializingConsumer (Consumer ):
53
61
"""
54
- Create a new Kafka Consumer instance .
62
+ DeserializingConsumer extends Consumer with configurable key and value deserializer .
55
63
56
- To avoid spontaneous calls from non-Python threads all callbacks will only be served upon
57
- calling ```client.poll()``` or ```client.flush()```.
64
+ Instances of DeserializingConsumer cannot be created directly.
65
+ To obtain an instance of this class instantiate a Consumer with a key and/or value deserializer.
66
+
67
+ Duplicate params have been omitted for brevity. See Consumer for class documentation.
58
68
59
- :param dict conf: Configuration properties.
60
- See https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md for more information.
61
- :param func key_deserializer(topic, key): Converts message key bytes to object.
62
- **note** deserializers are responsible for handling NULL keys
63
- :param func value_deserializer(topic, value): Converts message value bytes to object.
64
- **note** deserializers are responsible for handling NULL values
65
- :param func on_commit(err, [partitions]): Callback used to indicate success or failure
66
- of an offset commit.
67
- :param func stats_cb(json_str): Callback for statistics emitted every ``statistics.interval.ms``.
68
- See https://github.com/edenhill/librdkafka/wiki/Statistics” for more information.
69
- :param func throttle_cb(confluent_kafka.ThrottleEvent): Callback for throttled request reporting.
70
- :param logging.handlers logger: Forwards logs from the Kafka client to the provided handler instance.
71
- Log messages will only be forwarded when ``client.poll()`` or ``producer.flush()`` are called.
72
69
:raises TypeError: If conf is not a dict.
70
+ :raises TypeError: If instantiated directly.
73
71
"""
74
72
75
73
__slots__ = ["_key_deserializer" , "_value_deserializer" ]
76
74
75
+ def __new__ (cls , * args , ** kwargs ):
76
+ raise TypeError ("DeserializingConsumer is a non user-instantiable class" )
77
+
77
78
# conf must remain optional as long as kwargs are supported
78
- def __init__ (self , conf = {}, key_deserializer = None , value_deserializer = None ,
79
+ def __init__ (self , conf = {}, key_deserializer = byteDeserializer , value_deserializer = byteDeserializer ,
79
80
on_commit = None , stats_cb = None , throttle_cb = None , logger = None , ** kwargs ):
80
81
81
82
if not isinstance (conf , dict ):
@@ -143,15 +144,14 @@ def consume(self, num_messages=1, timeout=-1):
143
144
Consume messages, calls callbacks and returns a list of messages. (possibly empty on timeout)
144
145
145
146
The application must check Message.error() to distinguish between
146
- proper messages, an error(see error().code() for specifics), or an event. for each
147
+ proper messages, an error(see error().code() for specifics), or an event for each
147
148
Message in the list.
148
149
149
150
:param int num_messages: Maximum number of messages to return (default: 1)
150
151
:param float timeout: Maximum time in seconds to block waiting for message, event or callback.
151
152
(default: infinite (-1))
152
153
:returns: A list of Message objects (possibly empty on timeout)
153
154
:rtype: list(Message)
154
- :raises NotImplementedError: If used with key/value serializers.
155
155
:raises RuntimeError: If called on a closed consumer.
156
156
:raises KafkaError: In case of internal error.
157
157
:raises ValueError: If num_messages > 1M.
@@ -162,7 +162,7 @@ def consume(self, num_messages=1, timeout=-1):
162
162
if msg .error ():
163
163
continue
164
164
165
- msg .set_key (self ._key_deserializer (topic , msg .key ()))
166
- msg .set_value (self ._value_deserializer (topic , msg .value ()))
165
+ msg .set_key (self ._key_deserializer (msg . topic () , msg .key ()))
166
+ msg .set_value (self ._value_deserializer (msg . topic () , msg .value ()))
167
167
168
168
return msgset
0 commit comments