Skip to content

Commit c6b9f84

Browse files
committed
Update SimpleClient docs (Issue 543)
1 parent 6c213d6 commit c6b9f84

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

docs/simple.rst

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ Simple APIs (DEPRECATED)
22
************************
33

44

5-
SimpleConsumer
6-
==============
5+
SimpleConsumer (DEPRECATED)
6+
===========================
77

88
.. code:: python
99
@@ -37,8 +37,8 @@ SimpleConsumer
3737
client.close()
3838
3939
40-
SimpleProducer
41-
==============
40+
SimpleProducer (DEPRECATED)
41+
===========================
4242

4343
Asynchronous Mode
4444
-----------------
@@ -98,8 +98,8 @@ Synchronous Mode
9898
logging.info(r.offset)
9999
100100
101-
KeyedProducer
102-
=============
101+
KeyedProducer (DEPRECATED)
102+
==========================
103103

104104
.. code:: python
105105
@@ -121,24 +121,43 @@ KeyedProducer
121121
producer = KeyedProducer(kafka, partitioner=RoundRobinPartitioner)
122122
123123
124-
SimpleClient
125-
============
124+
SimpleClient (DEPRECATED)
125+
=========================
126126

127127

128128
.. code:: python
129129
130-
from kafka import SimpleClient, create_message
131-
from kafka.protocol import KafkaProtocol
132-
from kafka.common import ProduceRequest
130+
import time
131+
from kafka import SimpleClient
132+
from kafka.common import (
133+
LeaderNotAvailableError, NotLeaderForPartitionError,
134+
ProduceRequestPayload)
135+
from kafka.protocol import create_message
133136
134-
kafka = SimpleClient("localhost:9092")
137+
kafka = SimpleClient('localhost:9092')
138+
payload = ProduceRequestPayload(topic='my-topic', partition=0,
139+
messages=[create_message("some message")])
140+
141+
retries = 5
142+
resps = []
143+
while retries and not resps:
144+
retries -= 1
145+
try:
146+
resps = kafka.send_produce_request(
147+
payloads=[payload], fail_on_error=True)
148+
except LeaderNotAvailableError, NotLeaderForPartitionError:
149+
kafka.load_metadata_for_topics()
150+
time.sleep(1)
151+
152+
# Other exceptions you might consider handling:
153+
# UnknownTopicOrPartitionError, TopicAuthorizationFailedError,
154+
# RequestTimedOutError, MessageSizeTooLargeError, InvalidTopicError,
155+
# RecordListTooLargeError, InvalidRequiredAcksError,
156+
# NotEnoughReplicasError, NotEnoughReplicasAfterAppendError
135157
136-
req = ProduceRequest(topic="my-topic", partition=1,
137-
messages=[create_message("some message")])
138-
resps = kafka.send_produce_request(payloads=[req], fail_on_error=True)
139158
kafka.close()
140159
141-
resps[0].topic # "my-topic"
142-
resps[0].partition # 1
143-
resps[0].error # 0 (hopefully)
160+
resps[0].topic # 'my-topic'
161+
resps[0].partition # 0
162+
resps[0].error # 0
144163
resps[0].offset # offset of the first message sent in this request

0 commit comments

Comments
 (0)