|
20 | 20 | #
|
21 | 21 | # https://www.confluent.io/confluent-cloud/
|
22 | 22 | #
|
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, |
27 | 31 | # refer to the quick-start:
|
28 | 32 | #
|
29 | 33 | # https://docs.confluent.io/current/cloud-quickstart.html
|
30 | 34 | #
|
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 |
37 | 41 | #
|
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 |
44 | 48 |
|
45 | 49 | import uuid
|
46 | 50 | from confluent_kafka import Producer, Consumer, KafkaError
|
47 | 51 |
|
48 | 52 | p = Producer({
|
49 |
| - 'bootstrap.servers': '<bootstrap servers>', |
50 |
| - 'api.version.request': True, |
| 53 | + 'bootstrap.servers': '<ccloud bootstrap servers>', |
51 | 54 | 'broker.version.fallback': '0.10.0.0',
|
52 | 55 | 'api.version.fallback.ms': 0,
|
53 | 56 | 'sasl.mechanisms': 'PLAIN',
|
54 | 57 | '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>' |
58 | 60 | })
|
59 | 61 |
|
| 62 | + |
60 | 63 | def acked(err, msg):
|
| 64 | + """Delivery report callback called (from flush()) on successful or failed delivery of the message.""" |
61 | 65 | if err is not None:
|
62 | 66 | print("failed to deliver message: {0}".format(err.str()))
|
63 | 67 | 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 | + |
65 | 70 |
|
66 | 71 | 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. |
67 | 77 | p.flush(10)
|
68 | 78 |
|
69 | 79 | c = Consumer({
|
70 |
| - 'bootstrap.servers': '<bootstrap servers>', |
71 |
| - 'api.version.request': True, |
| 80 | + 'bootstrap.servers': '<ccloud bootstrap servers>', |
72 | 81 | 'broker.version.fallback': '0.10.0.0',
|
73 | 82 | 'api.version.fallback.ms': 0,
|
74 | 83 | 'sasl.mechanisms': 'PLAIN',
|
75 | 84 | '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. |
80 | 88 | 'default.topic.config': {'auto.offset.reset': 'smallest'}
|
81 | 89 | })
|
82 | 90 |
|
83 | 91 | c.subscribe(['python-test-topic'])
|
84 | 92 |
|
85 | 93 | try:
|
86 | 94 | while True:
|
87 |
| - msg = c.poll(0.1) |
| 95 | + msg = c.poll(0.1) # Wait for message or event/error |
88 | 96 | 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 |
89 | 100 | continue
|
90 | 101 | elif not msg.error():
|
91 | 102 | print('consumed: {0}'.format(msg.value()))
|
92 | 103 | 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())) |
94 | 105 | else:
|
95 | 106 | print('error: {0}'.format(msg.error().str()))
|
96 | 107 |
|
97 | 108 | except KeyboardInterrupt:
|
98 | 109 | pass
|
99 | 110 |
|
100 | 111 | finally:
|
| 112 | + # Leave group and commit final offsets |
101 | 113 | c.close()
|
0 commit comments