Commonly used confluent schema-registry API methods
Tags: Confluent, Schemaregistry, Kafka

List all subjects

This will list all the subjects registered in the schema registry. The above command assumes schema-registry is accessible at address schema-registry-1 with port 8081 and no auth i.e. SSL enabled. You could also get the output in multi-line for more readability, but assuming you have python and JSON module available.

curl -s -X GET http://schema-registry-1:8081/subjects/ | python -m jsob.tool

List all versions registered for a subject

This should list all the versions registered under a subject name, there could be 1 or 100 or n..

Please note the following method will return an error when executed, so make sure to specify “versions” after the subject name



{“error_code”:405,”message”:”HTTP 405 Method Not Allowed”} curl -s -X GET http://schema-registry-1:8081/subjects/test-value/ {“error_code”:405,”message”:”HTTP 405 Method Not Allowed”}

Get the schema from a specific version under subject

This will give you the schema with metadata i.e. under which subject this schema is been referred, what is the version and the globally unique id of the schema itself (which is 1 in this case)

List the schema by schema id

curl -s -X GET http://schema-registry-1:8081/schemas/ids/< schema id> e.g:

{

“schema”: “{\”type\”:\”record\”,\”name\”:\”myrecord\”,\”fields\”:[{\”name\”:\”name\”,\”type\”:\”string\”},{\”name\”:\”age\”,\”type\”:\”int\”}]}”

} curl -s -X GET http://schema-registry-1:8081/schemas/ids/1 | python -m json.tool“schema”: “{\”type\”:\”record\”,\”name\”:\”myrecord\”,\”fields\”:[{\”name\”:\”name\”,\”type\”:\”string\”},{\”name\”:\”age\”,\”type\”:\”int\”}]}”

If you happen to already know the schema id number that you want to lookup, the above command should be straightforward.

However, there are many other ways to get the schema id of the schema. The easy one would be to just run the previous GET command for a given subject and version combination, another one, rather not so intuitive, is running the kafka-avro-console-consumer with print.schema.ids=true been passes as a property. For example,

This will tell you what schema id was embedded with each of these messages in the test topic, then you can take these ids (1 in this example) and run curl on /schemas/ids api endpoint. Note that, we could also have some messages in the same topic referring to a different schema, this is totally acceptable as long as these are schemas are compatible based on compatibility settings. For example, in “BACKWARD” compatibility settings where we are allowed to send a message by dropping a field, that message will refer to a different schema. Let take a further look.

Send a message with just one field i.e. name



--broker-list broker-1:9092 — topic test \

--property schema.registry.url=

--property value.schema=’{“type”:”record”,”name”:”myrecord”,”fields”:[{“name”:”name”,”type”:”string”}]}' /usr/bin/kafka-avro-console-producer \--broker-list broker-1:9092 — topic test \--property schema.registry.url= http://schema-registry-1:8081 --property value.schema=’{“type”:”record”,”name”:”myrecord”,”fields”:[{“name”:”name”,”type”:”string”}]}' {"name":"test123"}

Run the consumer to check whether this message is referring to a different schema?

And there you can see the last message sent to this topic is referring to a different schema

Get top-level of compatibility config

Whatever you have set the top-level compatibility setting, it will show that up. Please note that “BACKWARD” is the default setting.