使用GCP Cloud Function读取Kafka数据时遇依赖报错求助
解决GCP Cloud Function中confluent_avro的Retry参数错误问题
问题场景
我希望通过GCP Cloud Function读取Kafka Topic的数据,main.py中导入模块如下:
from kafka import KafkaConsumer from confluent_avro import AvroKeyValueSerde, SchemaRegistry from confluent_avro.schema_registry import HTTPBasicAuth
requirements.txt内容:
kafka-python pandas confluent_avro requests
测试云函数时触发如下错误:
from confluent_avro import AvroKeyValueSerde, SchemaRegistry File "/layers/google.python.pip/pip/lib/python3.10/site-packages/confluent_avro/__init__.py", line 8, in <module> from confluent_avro.schema_registry import SchemaRegistry File "/layers/google.python.pip/pip/lib/python3.10/site-packages/confluent_avro/schema_registry/__init__.py", line 1, in <module> from confluent_avro.schema_registry import auth, client_http File "/layers/google.python.pip/pip/lib/python3.10/site-packages/confluent_avro/schema_registry/client_http.py", line 29, in <module> RETRY_POLICY = SchemaRegistryRetry( TypeError: Retry.__init__() got an unexpected keyword argument "method_whitelist"
已尝试confluent_avro、confluent_avro~=1.8.0、confluent-avero等依赖配置,均出现相同错误。
问题原因与解决方法
错误根源是confluent_avro依赖的urllib3版本过高:新版urllib3已移除method_whitelist参数,改用allowed_methods替代,但当前使用的confluent_avro版本仍在调用废弃参数,导致兼容性报错。
解决步骤
- 修改
requirements.txt,锁定兼容的urllib3版本(1.26.x系列是最后支持method_whitelist的版本范围):
kafka-python pandas confluent_avro requests urllib3~=1.26.18
- 重新部署GCP Cloud Function,让pip安装指定版本的依赖包。
另外也可尝试升级confluent_avro至最新版本,确认是否已修复参数兼容问题;若最新版仍未修复,锁定urllib3版本是最直接的解决方案。
内容的提问来源于stack exchange,提问作者ishan chaturvedi
相关产品推荐
相关产品推荐

