Using Schema Registry

Starting Schema Registry

Start Schema Registry and its dependent services ZooKeeper and Kafka. Each service reads its configuration from its property files under etc.

Development or Test Environment

You can use the Confluent CLI to start Schema Registry and its dependent services with this command:

confluent start schema-registry

Important

The Confluent CLI is meant for development purposes only and is not suitable for a production environment. The data that are produced are transient and are intended to be temporary.

Production Environment

Start each Confluent Platform service in its own terminal using this order of operations:

  1. Start ZooKeeper. Run this command in its own terminal.

    bin/zookeeper-server-start ./etc/kafka/zookeeper.properties
    
  2. Start Kafka. Run this command in its own terminal.

    bin/kafka-server-start ./etc/kafka/server.properties
    
  3. Start Schema Registry. Run this command in its own terminal.

    bin/schema-registry-start ./etc/schema-registry/schema-registry.properties
    

See the On-Premises Deployments for a more detailed explanation of how to get these services up and running.

Common Schema Registry Usage Examples

Tip

For a detailed example that uses Schema Registry configured with security, see the Confluent Platform demo.

These examples use curl commands to interact with the Schema Registry API.

Registering a New Version of a Schema Under the Subject “Kafka-key”

curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" \
  --data '{"schema": "{\"type\": \"string\"}"}' \
  http://localhost:8081/subjects/Kafka-key/versions
{"id":1}

Registering a New Version of a Schema Under the Subject “Kafka-value”

curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" \
  --data '{"schema": "{\"type\": \"string\"}"}' \
   http://localhost:8081/subjects/Kafka-value/versions
{"id":1}

Registering an Existing Schema to a New Subject Name

Use case: there is an existing schema registered to a subject called Kafka1, and this same schema needs to be available to another subject called Kafka2. The following one-line command reads the existing schema from Kafka1-value and registers it to Kafka2-value. It assumes the tool jq is installed on your machine.

curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" \
  --data "{\"schema\": $(curl -s http://localhost:8081/subjects/Kafka1-value/versions/latest | jq '.schema')}" \
   http://localhost:8081/subjects/Kafka2-value/versions
{"id":1}

Listing All Subjects

curl -X GET http://localhost:8081/subjects
["Kafka-value","Kafka-key"]

Fetching a Schema by Globally Unique ID 1

curl -X GET http://localhost:8081/schemas/ids/1
{"schema":"\"string\""}

Listing All Schema Versions Registered Under the Subject “Kafka-value”

curl -X GET http://localhost:8081/subjects/Kafka-value/versions
[1]

Fetch Version 1 of the Schema Registered Under Subject “Kafka-value”

curl -X GET http://localhost:8081/subjects/Kafka-value/versions/1
{"subject":"Kafka-value","version":1,"id":1,"schema":"\"string\""}

Deleting Version 1 of the Schema Registered Under Subject “Kafka-value”

curl -X DELETE http://localhost:8081/subjects/Kafka-value/versions/1
1

Registering the Same Schema Under the Subject “Kafka-value”

curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" \
  --data '{"schema": "{\"type\": \"string\"}"}' \
   http://localhost:8081/subjects/Kafka-value/versions
{"id":1}

Deleting the Most Recently Registered Schema Under Subject “Kafka-value”

curl -X DELETE http://localhost:8081/subjects/Kafka-value/versions/latest
2

Registering the Same Schema Under the Subject “Kafka-value”

curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" \
  --data '{"schema": "{\"type\": \"string\"}"}' \
   http://localhost:8081/subjects/Kafka-value/versions
{"id":1}

Fetching the Schema Again by Globally Unique ID 1

curl -X GET http://localhost:8081/schemas/ids/1
{"schema":"\"string\""}

Checking if a Schema Is Registered Under Subject “Kafka-key”

curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" \
  --data '{"schema": "{\"type\": \"string\"}"}' \
  http://localhost:8081/subjects/Kafka-key
{"subject":"Kafka-key","version":3,"id":1,"schema":"\"string\""}

Testing Compatibility of a Schema with the Latest Schema Under Subject “Kafka-value”

curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" \
  --data '{"schema": "{\"type\": \"string\"}"}' \
  http://localhost:8081/compatibility/subjects/Kafka-value/versions/latest
{"is_compatible":true}

Getting the Top Level Config

curl -X GET http://localhost:8081/config
{"compatibilityLevel":"BACKWARD"}

Updating Compatibility Requirements Globally

curl -X PUT -H "Content-Type: application/vnd.schemaregistry.v1+json" \
  --data '{"compatibility": "NONE"}' \
  http://localhost:8081/config
{"compatibility":"NONE"}

Updating Compatibility Requirements Under the Subject “Kafka-value”

curl -X PUT -H "Content-Type: application/vnd.schemaregistry.v1+json" \
  --data '{"compatibility": "BACKWARD"}' \
  http://localhost:8081/config/Kafka-value
{"compatibility":"BACKWARD"}

Deleting All Schema Versions Registered Under the Subject “Kafka-value”

curl -X DELETE http://localhost:8081/subjects/Kafka-value
[3]

Listing All Subjects

curl -X GET http://localhost:8081/subjects
["Kafka-key"]