【Azure事件中心】使用Python SDK(Confluent)相关方法获取offset或lag时提示SSL相关错误
问题描述
使用Python SDK(Confluent)相关方法获取offset或lag时, 提示SSL相关错误, 是否有更清晰的实例以便参考呢?
问题解决
执行代码,因为一直连接不成功,所以检查 confluent_kafka 的连接配置,最后定位是 sasl.password 值设置有误。此处,需要使用Event Hub Namespace级别的连接字符串(Connection String).
在Event Hub中,获取方式为: (1: Shared access policies ---> 2: RootManageSharedAccessKey or ..----> 3: Connection String )
完整的示例代码:
import confluent_kafka topics = ["<Your_topic_name>"] broker = "<Eventhub-namespace-name>.servicebus.chinacloudapi.cn:9093" group_name = "<Consumer-group-name>" sasl_password = "<Connection-string>" # Create consumer. # This consumer will not join the group, but the group.id is required by # committed() to know which group to get offsets for. consumer = confluent_kafka.Consumer({'bootstrap.servers': broker, 'security.protocol': 'SASL_SSL', 'sasl.mechanism': 'PLAIN', 'sasl.username': '$ConnectionString', 'sasl.password': sasl_password, 'group.id': group_name}) print("%-50s %9s %9s" % ("Topic [Partition]", "Committed", "Lag")) print("=" * 72) for topic in topics: # Get the topic's partitions metadata = consumer.list_topics(topic, timeout=10) if metadata.topics[topic].error is not None: raise confluent_kafka.KafkaException(metadata.topics[topic].error) # Construct TopicPartition list of partitions to query partitions = [confluent_kafka.TopicPartition(topic, p) for p in metadata.topics[topic].partitions] # Query committed offsets for this group and the given partitions committed = consumer.committed(partitions, timeout=10) for partition in committed: # Get the partitions low and high watermark offsets. (lo, hi) = consumer.get_watermark_offsets(partition, timeout=10, cached=False) if partition.offset == confluent_kafka.OFFSET_INVALID: offset = "-" else: offset = "%d" % (partition.offset) if hi < 0: lag = "no hwmark" # Unlikely elif partition.offset < 0: # No committed offset, show total message count as lag. # The actual message count may be lower due to compaction # and record deletions. lag = "%d" % (hi - lo) else: lag = "%d" % (hi - partition.offset) print("%-50s %9s %9s" % ( "{} [{}]".format(partition.topic, partition.partition), offset, lag)) consumer.close()
参考文档
confluent-kafka-python : https://github.com/confluentinc/confluent-kafka-python/blob/master/examples/list_offsets.py
当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2022-02-10 【Azure 应用服务】使用命令行创建 webapp 应用出现命令语法不正确错误
2021-02-10 【Azure 微服务】基于已经存在的虚拟网络(VNET)及子网创建新的Service Fabric并且为所有节点配置自定义DNS服务