[英]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.