简体   繁体   中英

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

Previous note: I am fairly new to 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.

Can't understand why. Here's the code (with the consumer in place).

ConsumerConfig is just a class with all configurations needed. Including the Schema Registry 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

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.

You don't have to create KafkaConsumer instance at all. 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.

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);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM