|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2018 Confluent Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | + |
| 18 | +# This is a simple example demonstrating how to produce a message to |
| 19 | +# Confluent Cloud then read it back again. |
| 20 | +# |
| 21 | +# https://www.confluent.io/confluent-cloud/ |
| 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, |
| 27 | +# refer to the quick-start: |
| 28 | +# |
| 29 | +# https://docs.confluent.io/current/cloud-quickstart.html |
| 30 | +# |
| 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 |
| 37 | +# |
| 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 |
| 44 | + |
| 45 | +import uuid |
| 46 | +from confluent_kafka import Producer, Consumer, KafkaError |
| 47 | + |
| 48 | +p = Producer({ |
| 49 | + 'bootstrap.servers': '<bootstrap servers>', |
| 50 | + 'api.version.request': True, |
| 51 | + 'broker.version.fallback': '0.10.0.0', |
| 52 | + 'api.version.fallback.ms': 0, |
| 53 | + 'sasl.mechanisms': 'PLAIN', |
| 54 | + 'security.protocol': 'SASL_SSL', |
| 55 | + 'ssl.ca.location': '/usr/local/etc/openssl/cert.pem', |
| 56 | + 'sasl.username': '<key>', |
| 57 | + 'sasl.password': '<secret>' |
| 58 | +}) |
| 59 | + |
| 60 | +def acked(err, msg): |
| 61 | + if err is not None: |
| 62 | + print("failed to deliver message: {0}".format(err.str())) |
| 63 | + else: |
| 64 | + print("produced to: {0}/{1}/{2}".format(msg.topic(), msg.partition(), msg.offset())) |
| 65 | + |
| 66 | +p.produce('python-test-topic', value='python test value', callback=acked) |
| 67 | +p.flush(10) |
| 68 | + |
| 69 | +c = Consumer({ |
| 70 | + 'bootstrap.servers': '<bootstrap servers>', |
| 71 | + 'api.version.request': True, |
| 72 | + 'broker.version.fallback': '0.10.0.0', |
| 73 | + 'api.version.fallback.ms': 0, |
| 74 | + 'sasl.mechanisms': 'PLAIN', |
| 75 | + '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()), |
| 80 | + 'default.topic.config': {'auto.offset.reset': 'smallest'} |
| 81 | +}) |
| 82 | + |
| 83 | +c.subscribe(['python-test-topic']) |
| 84 | + |
| 85 | +try: |
| 86 | + while True: |
| 87 | + msg = c.poll(0.1) |
| 88 | + if msg is None: |
| 89 | + continue |
| 90 | + elif not msg.error(): |
| 91 | + print('consumed: {0}'.format(msg.value())) |
| 92 | + elif msg.error().code() == KafkaError._PARTITION_EOF: |
| 93 | + print('end of partition: {0}/{1}'.format(msg.topic(), msg.partition())) |
| 94 | + else: |
| 95 | + print('error: {0}'.format(msg.error().str())) |
| 96 | + |
| 97 | +except KeyboardInterrupt: |
| 98 | + pass |
| 99 | + |
| 100 | +finally: |
| 101 | + c.close() |
0 commit comments