初始化Kafka Producer报错不存在schema.compatibility.level配置如何解决
你传入的schema.compatibility.level、avro.compatibility.level均不属于confluent-kafka生产者的客户端配置项,该类参数是Schema Registry服务端的配置属性,你将其放到了生产者的broker客户端配置configurations中,底层librdkafka无法识别该参数,因此抛出无效参数错误。你提到的acks、debug等参数属于librdkafka原生支持的客户端配置,因此可以正常生效。
根据你的需求可以选择以下任意一种方案:
方案1:修改Schema Registry对应主题的兼容性级别
直接调用Schema Registry的接口修改目标主题的Schema兼容性级别为NONE,示例命令如下:# 修改events_topic的value schema兼容性级别为NONE curl -X PUT -H "Content-Type: application/vnd.schemaregistry.v1+json" \ --data '{"compatibility": "NONE"}' \ http://{schema_registry_host}:{schema_registry_port}/config/events_topic-value # 如果需要修改全局所有主题的兼容性级别,执行以下命令 curl -X PUT -H "Content-Type: application/vnd.schemaregistry.v1+json" \ --data '{"compatibility": "NONE"}' \ http://{schema_registry_host}:{schema_registry_port}/config修改完成后无需再在生产者配置中添加兼容性相关参数,直接初始化生产者即可正常注册不兼容的schema。
方案2:通过生产者的Schema Registry专用配置传递参数
你使用的SimpleAvroProducer属于上层封装类,所有传入configurations参数的配置都会直接传递给底层Kafka broker客户端,Schema Registry相关的独立配置需要传入到对应的专用参数中,通常该参数命名为schema_registry_conf,修改后的初始化代码示例如下:producer = SimpleAvroProducer(producer_id="producer_1", topic_name="events_topic", broker_host= brkr_host, broker_port= brkr_port, schema_registry_host= schema_registry_host, schema_registry_port= schema_registry_port, key_schema_path=None, value_schema_path=DIR_PATH + "/event_schema.avsc", # 注意:兼容性配置放到schema registry专用配置参数中,而非configurations schema_registry_conf={"schema.compatibility.level": "none"}, configurations={"acks": "all", "debug": "msg" })不同封装版本的
SimpleAvroProducer的Schema Registry配置参数名可能略有差异,如果上述参数不生效可以查看对应封装类的初始化参数说明,找到Schema Registry相关的配置项传入即可。
内容的提问来源于stack exchange,提问作者darthsithius

