Skip to content

Commit d640ddf

Browse files
committed
merged from master c1a6b8e
2 parents 7c60469 + c1a6b8e commit d640ddf

25 files changed

+2678
-944
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
*.egg-info
12
*.pyc
3+
.tox
24
build
5+
dist
6+
MANIFEST
7+
env

AUTHORS.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Contributors
2+
3+
Ordered by contribution
4+
5+
* David Arthur, [@mumrah](https://github.com/mumrah)
6+
* Mahendra M, [@mahendra](https://github.com/mahendra)
7+
* Ivan Pouzyrevsky, [@sandello](https://github.com/sandello)
8+
* [@anentropic](https://github.com/anentropic)
9+
* Ben Frederickson, [@benfred](https://github.com/benfred)
10+
11+
Thanks, everyone!

CHANGES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Notable Changes
2+
3+
* Changing auto_commit to False in [SimpleConsumer](kafka/consumer.py), until 0.8.1 is release offset commits are unsupported
4+
5+
* Adding fetch_size_bytes to SimpleConsumer constructor to allow for user-configurable fetch sizes
6+
7+
* Allow SimpleConsumer to automatically increase the fetch size if a partial message is read and no other messages were read during that fetch request. The increase factor is 1.5
8+
9+
* Exception classes moved to kafka.common

README.md

Lines changed: 85 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,90 @@ development, APIs are subject to change.
2626
```python
2727
from kafka.client import KafkaClient
2828
from kafka.consumer import SimpleConsumer
29-
from kafka.producer import SimpleProducer
29+
from kafka.producer import SimpleProducer, KeyedProducer
3030

3131
kafka = KafkaClient("localhost", 9092)
3232

33+
# To send messages synchronously
3334
producer = SimpleProducer(kafka, "my-topic")
3435
producer.send_messages("some message")
3536
producer.send_messages("this method", "is variadic")
3637

38+
# To send messages asynchronously
39+
producer = SimpleProducer(kafka, "my-topic", async=True)
40+
producer.send_messages("async message")
41+
42+
# To wait for acknowledgements
43+
# ACK_AFTER_LOCAL_WRITE : server will wait till the data is written to
44+
# a local log before sending response
45+
# ACK_AFTER_CLUSTER_COMMIT : server will block until the message is committed
46+
# by all in sync replicas before sending a response
47+
producer = SimpleProducer(kafka, "my-topic", async=False,
48+
req_acks=SimpleProducer.ACK_AFTER_LOCAL_WRITE,
49+
acks_timeout=2000)
50+
51+
response = producer.send_messages("async message")
52+
53+
if response:
54+
print(response[0].error)
55+
print(response[0].offset)
56+
57+
# To send messages in batch. You can use any of the available
58+
# producers for doing this. The following producer will collect
59+
# messages in batch and send them to Kafka after 20 messages are
60+
# collected or every 60 seconds
61+
# Notes:
62+
# * If the producer dies before the messages are sent, there will be losses
63+
# * Call producer.stop() to send the messages and cleanup
64+
producer = SimpleProducer(kafka, "my-topic", batch_send=True,
65+
batch_send_every_n=20,
66+
batch_send_every_t=60)
67+
68+
# To consume messages
3769
consumer = SimpleConsumer(kafka, "my-group", "my-topic")
3870
for message in consumer:
3971
print(message)
4072

4173
kafka.close()
4274
```
4375

76+
## Keyed messages
77+
```python
78+
from kafka.client import KafkaClient
79+
from kafka.producer import KeyedProducer
80+
from kafka.partitioner import HashedPartitioner, RoundRobinPartitioner
81+
82+
kafka = KafkaClient("localhost", 9092)
83+
84+
# HashedPartitioner is default
85+
producer = KeyedProducer(kafka, "my-topic")
86+
producer.send("key1", "some message")
87+
producer.send("key2", "this methode")
88+
89+
producer = KeyedProducer(kafka, "my-topic", partitioner=RoundRobinPartitioner)
90+
```
91+
92+
## Multiprocess consumer
93+
```python
94+
from kafka.client import KafkaClient
95+
from kafka.consumer import MultiProcessConsumer
96+
97+
kafka = KafkaClient("localhost", 9092)
98+
99+
# This will split the number of partitions among two processes
100+
consumer = MultiProcessConsumer(kafka, "my-group", "my-topic", num_procs=2)
101+
102+
# This will spawn processes such that each handles 2 partitions max
103+
consumer = MultiProcessConsumer(kafka, "my-group", "my-topic",
104+
partitions_per_proc=2)
105+
106+
for message in consumer:
107+
print(message)
108+
109+
for message in consumer.get_messages(count=5, block=True, timeout=4):
110+
print(message)
111+
```
112+
44113
## Low level
45114

46115
```python
@@ -101,16 +170,18 @@ pip install python-snappy
101170

102171
# Tests
103172

104-
Some of the tests will fail if Snappy is not installed. These tests will throw
105-
NotImplementedError. If you see other failures, they might be bugs - so please
106-
report them!
107-
108173
## Run the unit tests
109174

110175
_These are broken at the moment_
111176

112177
```shell
113-
python -m test.unit
178+
tox ./test/test_unit.py
179+
```
180+
181+
or
182+
183+
```shell
184+
python -m test.test_unit
114185
```
115186

116187
## Run the integration tests
@@ -125,11 +196,15 @@ cd kafka-src
125196
./sbt package
126197
```
127198

128-
Next start up a ZooKeeper server on localhost:2181
199+
And then run the tests. This will actually start up real local Zookeeper
200+
instance and Kafka brokers, and send messages in using the client.
129201

130202
```shell
131-
/opt/zookeeper/bin/zkServer.sh start
203+
tox ./test/test_integration.py
132204
```
133205

134-
This will actually start up real Kafka brokers and send messages in using the
135-
client.
206+
or
207+
208+
```shell
209+
python -m test.test_integration
210+
```

kafka/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
__license__ = 'Apache License 2.0'
55
__copyright__ = 'Copyright 2012, David Arthur under Apache License, v2.0'
66

7-
from kafka.client import KafkaClient
7+
from kafka.client import KafkaClient
88
from kafka.conn import KafkaConnection
99
from kafka.protocol import (
1010
create_message, create_gzip_message, create_snappy_message
1111
)
12-
from kafka.producer import SimpleProducer
13-
from kafka.consumer import SimpleConsumer
12+
from kafka.producer import SimpleProducer, KeyedProducer
13+
from kafka.partitioner import RoundRobinPartitioner, HashedPartitioner
14+
from kafka.consumer import SimpleConsumer, MultiProcessConsumer
1415

1516
__all__ = [
16-
'KafkaClient', 'KafkaConnection', 'SimpleProducer', 'SimpleConsumer',
17-
'create_message', 'create_gzip_message', 'create_snappy_message'
17+
'KafkaClient', 'KafkaConnection', 'SimpleProducer', 'KeyedProducer',
18+
'RoundRobinPartitioner', 'HashedPartitioner', 'SimpleConsumer',
19+
'MultiProcessConsumer', 'create_message', 'create_gzip_message',
20+
'create_snappy_message'
1821
]

0 commit comments

Comments
 (0)