Registering schema(REST API) with Confluent Schema Registry.

In this post, I want to share how you can register (through REST API) your schema with Confluent Schema Registry.
Sometimes it can be tricky because you have to know how to escape characters and use proper schema format.

AVRO schema for REST call, user.avsc file in resources folder:

{ "schema":"{ \"type\": \"record\",
     \"namespace\": \"com.example\",
     \"name\": \"User\",
     \"doc\" : \"Represents a user schema\",
     \"version\": \"1\",
            { \"name\": \"user_Id\", \"type\": \"string\", \"doc\": \"never should be null, this is unique ID\"},
            { \"name\": \"user_name\", \"type\": \"string\", \"doc\": \"first and last name of user\"}

Note schema word in the beginnig of schema declaration and backslashes before each double quote.
This kind of schema formatting is used only for registering schema via REST.

Normally, your usual schema declaration will look like this:

    "type": "record",
     "namespace": "com.example",
     "name": "User",
     "doc" : "Represents a user schema",
     "version": "1",
            { "name": "user_Id", "type": "string", "doc": "never should be null, this is unique ID"},
            { "name": "user_name", "type": "string", "doc": "first and last name of user"}

Below a piece of code where I use Spring Framework RestTemplate class to send HTTP POST request to Schema Registry server:

public void registerAvroSchema() throws IOException {
        // Prepare URL for request, you need to provide URL of Schema Registry and name of kafka topic
        String url = String.format("%s/subjects/%s-value/versions", "", "kafka-topic");

        HttpHeaders headers = new HttpHeaders();

        HttpEntity<String> request =
                new HttpEntity<>(
                        // Specify a path to avsc file, convert it to String and then remove all LF (Line Feed)
                        IOUtils.toString(new ClassPathResource("avro-rest/user.avsc").getInputStream()).replace("\n", ""),

        restTemplate.postForEntity(url, request, String.class);

Leave a Reply

Your email address will not be published. Required fields are marked *