Skip to content

KafkaProducer #515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,34 @@ for examples.
KafkaProducer
*************

<`in progress - see SimpleProducer for legacy producer implementation`>
KafkaProducer is a high-level, asynchronous message producer. The class is
intended to operate as similarly as possible to the official java client.
See `ReadTheDocs <http://kafka-python.readthedocs.org/en/master/apidoc/KafkaProducer.html>`_
for more details.

>>> from kafka import KafkaProducer
>>> producer = KafkaProducer(bootstrap_servers='localhost:1234')
>>> producer.send('foobar', b'some_message_bytes')

>>> # Blocking send
>>> producer.send('foobar', b'another_message').get(timeout=60)

>>> # Use a key for hashed-partitioning
>>> producer.send('foobar', key=b'foo', value=b'bar')

>>> # Serialize json messages
>>> import json
>>> producer = KafkaProducer(value_serializer=json.loads)
>>> producer.send('fizzbuzz', {'foo': 'bar'})

>>> # Serialize string keys
>>> producer = KafkaProducer(key_serializer=str.encode)
>>> producer.send('flipflap', key='ping', value=b'1234')

>>> # Compress messages
>>> producer = KafkaProducer(compression_type='gzip')
>>> for i in range(1000):
... producer.send('foobar', b'msg %d' % i)


Protocol
Expand Down
3 changes: 2 additions & 1 deletion docs/apidoc/KafkaProducer.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
KafkaProducer
=============

<unreleased> See :class:`kafka.producer.SimpleProducer`
.. autoclass:: kafka.KafkaProducer
:members:
3 changes: 2 additions & 1 deletion kafka/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
__copyright__ = 'Copyright 2016 Dana Powers, David Arthur, and Contributors'

from kafka.consumer import KafkaConsumer
from kafka.producer import KafkaProducer
from kafka.conn import BrokerConnection
from kafka.protocol import (
create_message, create_gzip_message, create_snappy_message)
Expand All @@ -28,7 +29,7 @@ def __init__(self, *args, **kwargs):


__all__ = [
'KafkaConsumer', 'KafkaClient', 'BrokerConnection',
'KafkaConsumer', 'KafkaProducer', 'KafkaClient', 'BrokerConnection',
'SimpleClient', 'SimpleProducer', 'KeyedProducer',
'RoundRobinPartitioner', 'HashedPartitioner',
'create_message', 'create_gzip_message', 'create_snappy_message',
Expand Down
23 changes: 23 additions & 0 deletions kafka/partitioner/default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import random

from .hashed import murmur2


class DefaultPartitioner(object):
"""Default partitioner.

Hashes key to partition using murmur2 hashing (from java client)
If key is None, selects partition randomly from available,
or from all partitions if none are currently available
"""
@classmethod
def __call__(cls, key, all_partitions, available):
if key is None:
if available:
return random.choice(available)
return random.choice(all_partitions)

idx = murmur2(key)
idx &= 0x7fffffff
idx %= len(all_partitions)
return all_partitions[idx]
4 changes: 3 additions & 1 deletion kafka/producer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .kafka import KafkaProducer
from .simple import SimpleProducer
from .keyed import KeyedProducer

__all__ = [
'SimpleProducer', 'KeyedProducer'
'KafkaProducer',
'SimpleProducer', 'KeyedProducer' # deprecated
]
Loading