简体   繁体   English

为什么需要创建一个Kafka使用者才能连接到Schema Registry?

[英]Why do I need to create a Kafka Consumer to connect to Schema Registry?

Previous note: I am fairly new to Kafka. 前记:我对Kafka还是陌生的。

I am trying to get all schemas from the Schema Registry, but I am not being able to do so only with a schema registry client. 我试图从架构注册表中获取所有架构,但是我无法仅通过架构注册表客户端来实现。 It only works if, prior to that, I instantiate a KafkaConsumer. 只有在此之前我实例化KafkaConsumer,它才起作用。

Can't understand why. 不明白为什么。 Here's the code (with the consumer in place). 这是代码(使用了使用者)。

ConsumerConfig is just a class with all configurations needed. ConsumerConfig只是具有所有必需配置的一类。 Including the Schema Registry URL. 包括架构注册表URL。

Consumer<String, String>  consumer = new KafkaConsumer<String, String>(ConsumerConfig.get());
CachedSchemaRegistryClient client = new CachedSchemaRegistryClient(ConsumerConfig.getSchemaRegistryURL(), 30);
Collection<String> listOfSubjects = client.getAllSubjects();
consumer.close();

Without the consumer, i get: 没有消费者,我得到:

io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException: No content to map due to end-of-input io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException:由于输入结束,没有内容要映射

With the consumer, everything works fine. 对于消费者,一切正常。 I would like if someone could shine some light on why this happens, has I see no reason for me to need to connect to the actual Kafka Cluster via a consumer in order to access the Schema Registry that is on another endpoint. 我想知道是否有人可以解释为什么会发生这种情况,我是否没有理由让我需要通过使用者连接到实际的Kafka集群才能访问另一个端点上的Schema Registry。

You don't have to create KafkaConsumer instance at all. 您根本不必创建KafkaConsumer实例。 Both are totally independent. 两者都是完全独立的。

If you just want to get all the subjects and schema from SchemaRegistry, just create an instance of CachedSchemaRegistryClient and call the related operation. 如果只想从SchemaRegistry获取所有主题和架构,则只需创建CachedSchemaRegistryClient实例并调用相关操作即可。

Here is a working example : 这是一个工作示例:

 private final static Map<String, Schema> schemas = new ConcurrentHashMap<>();
 protected static SchemaRegistryClient schemaRegistryClient;

 public static void main(String[] args) {
       String registryUrl = "http://localhost:8081";
        try {
            schemaRegistryClient = new CachedSchemaRegistryClient(registryUrl, 30);
            System.out.println(schemaRegistryClient);
            Collection<String> subjects = schemaRegistryClient.getAllSubjects();
            System.out.println(subjects);
        } catch (Exception e){
            throw new RuntimeException(e);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我是否需要在模式注册表下为 kafka stream 更改日志主题注册模式? - Do i need to register schema for kafka stream changelog topic under schema registry? 用于模式注册表和代理的 Kafka 客户端消费者配置 - Kafka Client Consumer Config for Schema Registry and Broker Spring 使用模式注册表启动 kafka - 负载在消费者端不匹配 - Spring boot kafka with schema registry - payload is not matching at consumer end 使用 Avro Schema 注册表的 Kafka 消费者单元测试失败 - Kafka consumer unit test with Avro Schema registry failing 如何创建 AvroDeserialzationSchema 并在 Flink Kafka Consumer 中使用? - How do I create the AvroDeserialzationSchema and use in a Flink Kafka Consumer? Spring 使用 Glue 模式注册表反序列化 AVRO GENERIC_RECORD 启动 kafka 消费者问题 - Spring boot kafka consumer issue deserializing AVRO GENERIC_RECORD using Glue schema registry 消费者如何在从 Kafka 代理获取数据之前检查模式注册表是否可访问? - How can consumer check if the schema registry is reachable before getting data from Kafka broker? 如果我的团队将提供 jar 库和 model,我是否需要模式注册表? - Do I need schema registry if my team will provide jar library with model? 使用Kafka实现架构注册表时出错 - Error implementing schema registry with kafka 使用 protobuf 创建 kafka 消费者 - create kafka consumer with protobuf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM