|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# |
| 4 | +# |
| 5 | +# Copyright 2018 Confluent Inc. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | +# |
| 19 | + |
| 20 | +from .cimpl import Consumer as _impl |
| 21 | +from warnings import warn |
| 22 | + |
| 23 | + |
| 24 | +class Consumer(_impl): |
| 25 | + """ |
| 26 | + Create a new Kafka Consumer instance. |
| 27 | +
|
| 28 | + To avoid spontaneous calls from non-Python threads all callbacks will only be served upon |
| 29 | + calling ```client.poll()``` or ```client.flush()```. |
| 30 | +
|
| 31 | + :param dict conf: Configuration properties. |
| 32 | + See https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md for more information. |
| 33 | + :param func key_deserializer(topic, key): Converts message key bytes to object. |
| 34 | + **note** deserializers are responsible for handling NULL keys |
| 35 | + :param func value_deserializer(topic, value): Converts message value bytes to object. |
| 36 | + **note** deserializers are responsible for handling NULL values |
| 37 | + :param func on_commit(err, [partitions]): Callback used to indicate success or failure |
| 38 | + of an offset commit. |
| 39 | + :param func stats_cb(json_str): Callback for statistics emitted every ``statistics.interval.ms``. |
| 40 | + See https://github.com/edenhill/librdkafka/wiki/Statistics” for more information. |
| 41 | + :param func throttle_cb(confluent_kafka.ThrottleEvent): Callback for throttled request reporting. |
| 42 | + :param logging.handlers logger: Forwards logs from the Kafka client to the provided handler instance. |
| 43 | + Log messages will only be forwarded when ``client.poll()`` or ``producer.flush()`` are called. |
| 44 | + :raises TypeError: If conf is not a dict. |
| 45 | + """ |
| 46 | + |
| 47 | + __slots__ = ["_key_deserializer", "_value_deserializer"] |
| 48 | + |
| 49 | + # conf must remain optional as long as kwargs are supported |
| 50 | + def __init__(self, conf={}, key_deserializer=None, value_deserializer=None, |
| 51 | + on_commit=None, stats_cb=None, throttle_cb=None, logger=None, **kwargs): |
| 52 | + |
| 53 | + if not isinstance(conf, dict): |
| 54 | + raise TypeError("expected configuration dict") |
| 55 | + |
| 56 | + if kwargs: |
| 57 | + # Handle kwargs for backwards compatibility |
| 58 | + conf.update(kwargs) |
| 59 | + warn("The use of kwargs is being deprecated. " |
| 60 | + "In future releases `conf` will be mandatory and " |
| 61 | + "all keyword arguments must match the constructor signature explicitly.", |
| 62 | + category=DeprecationWarning, stacklevel=2) |
| 63 | + |
| 64 | + self._key_deserializer = key_deserializer |
| 65 | + self._value_deserializer = value_deserializer |
| 66 | + |
| 67 | + # Callbacks can be set in the conf dict or *ideally* as parameters. |
| 68 | + # Handle both cases prior to passing along to _impl |
| 69 | + # If callbacks are configured in both places parameter values take precedence. |
| 70 | + if not on_commit: |
| 71 | + on_commit = conf.get('on_commit', None) |
| 72 | + |
| 73 | + if not stats_cb: |
| 74 | + stats_cb = conf.get('stats_cb', None) |
| 75 | + |
| 76 | + if not throttle_cb: |
| 77 | + throttle_cb = conf.get('throttle_cb', None) |
| 78 | + |
| 79 | + if not logger: |
| 80 | + logger = conf.get('logger', None) |
| 81 | + super(Consumer, self).__init__(conf, on_commit=on_commit, stats_cb=stats_cb, |
| 82 | + throttle_cb=throttle_cb, logger=logger) |
| 83 | + |
| 84 | + def poll(self, timeout=-1.0, key_deserializer=None, value_deserializer=None): |
| 85 | + """ |
| 86 | + Consumes a message, triggers callbacks, returns an event. |
| 87 | +
|
| 88 | + The application must check the returned Message object’s Message.error() method to distinguish |
| 89 | + between proper messages, an error(see error().code() for specifics), or an event. |
| 90 | +
|
| 91 | + :param float timeout: Maximum time in seconds to block waiting for message, event or callback. |
| 92 | + :param func key_deserializer(topic, key): Converts message key bytes to object. |
| 93 | + **note** deserializers are responsible for handling NULL keys |
| 94 | + :param func value_deserializer(topic, value): Converts message value bytes to object. |
| 95 | + **note** deserializers are responsible for handling NULL values |
| 96 | + :returns: A confluent_kafka.Message or None on timeout. |
| 97 | + :raises RuntimeError: If called on a closed consumer. |
| 98 | + """ |
| 99 | + |
| 100 | + msg = super(Consumer, self).poll(timeout) |
| 101 | + |
| 102 | + if not msg or msg.error(): |
| 103 | + return msg |
| 104 | + |
| 105 | + topic = msg.topic() |
| 106 | + |
| 107 | + # parameter overrides take precedence over instance functions |
| 108 | + if not key_deserializer: |
| 109 | + key_deserializer = self._key_deserializer |
| 110 | + |
| 111 | + if key_deserializer: |
| 112 | + msg.set_key(key_deserializer(topic, msg.key())) |
| 113 | + |
| 114 | + if not value_deserializer: |
| 115 | + value_deserializer = self._value_deserializer |
| 116 | + |
| 117 | + if value_deserializer: |
| 118 | + msg.set_value(value_deserializer(topic, msg.value())) |
| 119 | + |
| 120 | + return msg |
| 121 | + |
| 122 | + def consume(self, num_messages=1, timeout=-1): |
| 123 | + """ |
| 124 | + Consume messages, calls callbacks and returns a list of messages. (possibly empty on timeout) |
| 125 | +
|
| 126 | + The application must check Message.error() to distinguish between |
| 127 | + proper messages, an error(see error().code() for specifics), or an event. for each |
| 128 | + Message in the list. |
| 129 | +
|
| 130 | + :param int num_messages: Maximum number of messages to return (default: 1) |
| 131 | + :param float timeout: Maximum time in seconds to block waiting for message, event or callback. |
| 132 | + (default: infinite (-1)) |
| 133 | + :returns: A list of Message objects (possibly empty on timeout) |
| 134 | + :rtype: list(Message) |
| 135 | + :raises NotImplementedError: If used with key/value serializers. |
| 136 | + :raises RuntimeError: If called on a closed consumer. |
| 137 | + :raises KafkaError: In case of internal error. |
| 138 | + :raises ValueError: If num_messages > 1M. |
| 139 | + """ |
| 140 | + |
| 141 | + # Disable consume() method when serializers are in use. |
| 142 | + if self._key_deserializer or self._value_deserializer: |
| 143 | + raise(NotImplementedError, "Batch consumption does not support the use of deserializers") |
| 144 | + |
| 145 | + return super(Consumer, self).consume(num_messages, timeout) |
0 commit comments