geoffwilliams@home:~$

How to register JSON schemas in Confluent Schema Registry using the REST API

Create the schema

  • By hand
  • By generation (online, from POJO etc)

Save the schema somewhere

Example:

schemas/value.json

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "additionalProperties": true,
  "properties": {
    "messageCreateTime": {
      "type": "string"
    },
    "messageType": {
      "type": "string"
    },
    "sourceApplication": {
      "type": "string"
    },
    "targetApplication": {
      "type": "string"
    },
    "tranID": {
      "type": "string"
    }
  },
  "title": "Message",
  "type": "object"
}

Note: due to "additionalProperties": true the above schema will create an open content model. If you want a closed content model instead, set "additionalProperties": false.

Upload the schema

  • Use Unix (\n) line endings or strip any \r characters to prevent Schema Registry error: Illegal unquoted character ((CTRL-CHAR, code 13))
  • Easiest way to do this is with a script, although you could also use postman
  • For JSON schema must set schemaType: "JSON"

Example:

#!/bin/bash

schema=$(cat schemas/value.json | sed 's/\"/\\\"/g' | tr -d "\n\r")
SCHEMA="{\"schema\": \"$schema\", \"schemaType\": \"JSON\"}"
curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" \
  --data "$SCHEMA" \
  http://localhost:8081/subjects/thenameofthetopic-value/versions

Done!

Bonus points

Generate a Java POJO from the JSON schema. For you to do!

Post comment