Skip to content

Commit e7da3ca

Browse files
author
Matt Howlett
committed
addressing review comments
1 parent dd26988 commit e7da3ca

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

examples/confluent_cloud.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,82 +20,94 @@
2020
#
2121
# https://www.confluent.io/confluent-cloud/
2222
#
23-
# Confluent Cloud does not auto-create topics. You will need to use the ccloud
24-
# cli to create the python-test-topic topic before running this example. The
25-
# <ccloud bootstrap servers>, <ccloud key> and <ccloud secret> parameters are
26-
# available via the confluent cloud web interface. For more information,
23+
# Auto-creation of topics is disabled in Confluent Cloud. You will need to
24+
# use the ccloud cli to create the python-test-topic topic before running this
25+
# example.
26+
#
27+
# $ ccloud topic create python-test-topic
28+
#
29+
# The <ccloud bootstrap servers>, <ccloud key> and <ccloud secret> parameters
30+
# are available via the Confluent Cloud web interface. For more information,
2731
# refer to the quick-start:
2832
#
2933
# https://docs.confluent.io/current/cloud-quickstart.html
3034
#
31-
# to execute using Python 2.x:
32-
# virtualenv ccloud_example
33-
# source ccloud_example/bin/activate
34-
# pip install confluent_kafka
35-
# python example.py
36-
# deactivate
35+
# to execute using Python 2.7:
36+
# $ virtualenv ccloud_example
37+
# $ source ccloud_example/bin/activate
38+
# $ pip install confluent_kafka
39+
# $ python confluent_cloud.py
40+
# $ deactivate
3741
#
38-
# to execute using Python 3.x:
39-
# python -m venv ccloud_example
40-
# source ccloud_example/bin/activate
41-
# pip install confluent_kafka
42-
# python example.py
43-
# deactivate
42+
# to execute using Python 3.x:
43+
# $ python -m venv ccloud_example
44+
# $ source ccloud_example/bin/activate
45+
# $ pip install confluent_kafka
46+
# $ python confluent_cloud.py
47+
# $ deactivate
4448

4549
import uuid
4650
from confluent_kafka import Producer, Consumer, KafkaError
4751

4852
p = Producer({
49-
'bootstrap.servers': '<bootstrap servers>',
50-
'api.version.request': True,
53+
'bootstrap.servers': '<ccloud bootstrap servers>',
5154
'broker.version.fallback': '0.10.0.0',
5255
'api.version.fallback.ms': 0,
5356
'sasl.mechanisms': 'PLAIN',
5457
'security.protocol': 'SASL_SSL',
55-
'ssl.ca.location': '/usr/local/etc/openssl/cert.pem',
56-
'sasl.username': '<key>',
57-
'sasl.password': '<secret>'
58+
'sasl.username': '<ccloud key>',
59+
'sasl.password': '<ccloud secret>'
5860
})
5961

62+
6063
def acked(err, msg):
64+
"""Delivery report callback called (from flush()) on successful or failed delivery of the message."""
6165
if err is not None:
6266
print("failed to deliver message: {0}".format(err.str()))
6367
else:
64-
print("produced to: {0}/{1}/{2}".format(msg.topic(), msg.partition(), msg.offset()))
68+
print("produced to: {0} [{1}] @ {2}".format(msg.topic(), msg.partition(), msg.offset()))
69+
6570

6671
p.produce('python-test-topic', value='python test value', callback=acked)
72+
73+
# flush() is typically called when the producer is done sending messages to wait
74+
# for outstanding messages to be transmitted to the broker and delivery report
75+
# callbacks to get called. For continous producing you should call p.poll(0)
76+
# after each produce() call to trigger delivery report callbacks.
6777
p.flush(10)
6878

6979
c = Consumer({
70-
'bootstrap.servers': '<bootstrap servers>',
71-
'api.version.request': True,
80+
'bootstrap.servers': '<ccloud bootstrap servers>',
7281
'broker.version.fallback': '0.10.0.0',
7382
'api.version.fallback.ms': 0,
7483
'sasl.mechanisms': 'PLAIN',
7584
'security.protocol': 'SASL_SSL',
76-
'ssl.ca.location': '/usr/local/etc/openssl/cert.pem',
77-
'sasl.username': '<key>',
78-
'sasl.password': '<secret>',
79-
'group.id': str(uuid.uuid1()),
85+
'sasl.username': '<ccloud key>',
86+
'sasl.password': '<ccloud secret>',
87+
'group.id': str(uuid.uuid1()), # this will create a new consumer group on each invocation.
8088
'default.topic.config': {'auto.offset.reset': 'smallest'}
8189
})
8290

8391
c.subscribe(['python-test-topic'])
8492

8593
try:
8694
while True:
87-
msg = c.poll(0.1)
95+
msg = c.poll(0.1) # Wait for message or event/error
8896
if msg is None:
97+
# No message available within timeout.
98+
# Initial message consumption may take up to `session.timeout.ms` for
99+
# the group to rebalance and start consuming
89100
continue
90101
elif not msg.error():
91102
print('consumed: {0}'.format(msg.value()))
92103
elif msg.error().code() == KafkaError._PARTITION_EOF:
93-
print('end of partition: {0}/{1}'.format(msg.topic(), msg.partition()))
104+
print('end of partition: {0} [{1}] @ {2}'.format(msg.topic(), msg.partition(), msg.offset()))
94105
else:
95106
print('error: {0}'.format(msg.error().str()))
96107

97108
except KeyboardInterrupt:
98109
pass
99110

100111
finally:
112+
# Leave group and commit final offsets
101113
c.close()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy