您如何为 Confluent Schema Registry 配置主题命名策略?

How do you configure the subject naming strategy for Confluent Schema Registry?

我正在将 Confluent Schema Registry 与 Avro 结合使用,我想在一个 Kafka 主题中使用多个模式。

默认主题命名策略 TopicNameStrategy 不允许这样做,因为它将架构主题名称与主题名称耦合。显然可以覆盖它并将主题命名策略设置为 RecordNameStrategyTopicRecordNameStrategy.

遗憾的是,文档对于如何或在何处可以覆盖主题命名策略不是很清楚。

document建议您可以在创建或修改主题时提供配置:

Starting with Confluent Platform 5.5.0, the naming strategy is associated with the topics. Therefore, you now have the option to configure a naming strategy to something other than the default on a per-topic basis for both the schema subject key and value with confluent.key.subject.name.strategy and confluent.value.subject.name.strategy.

From the Confluent CLI, use the --config option to create or modify a topic with the specified naming strategy. For example:

To create a topic that uses RecordNameStrategy for the value:

./bin/kafka-topics --create --bootstrap-server localhost:9092 \
--replication-factor 1 --partitions 1 --topic my-other-cool-topic \
--config confluent.value.schema.validation=true --config confluent.value.subject.name.strategy=io.confluent.kafka.serializers.subject.RecordNameStrategy

当我尝试此操作时,kafka-topics 应用程序 returns 出现以下错误:

Error while executing topic command : Unknown topic config name: confluent.value.subject.name.strategy
[2020-10-31 10:17:00,947] ERROR org.apache.kafka.common.errors.InvalidConfigurationException: Unknown topic config name: confluent.value.subject.name.strategy
 (kafka.admin.TopicCommand$)

我也试过在使用 kafka-avro-console-producer 通过 属性 value.subject.name.strategy 将记录写入 Kafka 时配置主题名称策略,正如 document 所建议的那样。然而,控制台生产者忽略了这个 属性 并使用默认策略为主题创建了一个新主题。

kafka-avro-console-producer \
    --broker-list kafka-broker-0.kafka-broker:9092 \
    --property schema.registry.url='http://kafka-schema-registry:8081' \
    --property value.schema='<MYSCHEMA>' \
    --property value.subject.name.strategy='io.confluent.kafka.serializers.subject.TopicRecordNameStrategy' \
    --topic mytopic

显然曾经有一种方法可以在代理本身上配置策略,但我也找不到任何说明如何执行该操作的文档。

配置命名策略的正确方法是什么?应该在哪里配置——在主题上,在主题制作者上(kafka-avro-console-producer 在这种情况下),还是在其他地方?

其他上下文:

kafka-avro-console-producer暂不支持指定命名策略。您可以使用其他工具,例如 zoe:https://adevinta.github.io/zoe/advanced/avro/

您可以在生产者中将其定义为 属性(key.subject.name.strategy 和 value.subject.name.strategy)。您可以选择现有策略之一,也可以开发自己的策略。