Search code examples
javascriptapache-kafkajsonschemaconfluent-schema-registry

How to JSON serialize message and post it to Kafka Topic with JSON Schema


confluent-schema-registry javascript package can be used to serialize and de-serialize messages posted to Kafka Topic. Unfortunately, it only supports AVRO format. Is there a similar package that supports JSON serialization?


Solution

  • If you go beyond the first page of the documentation, it shows more than just Avro.

    const { SchemaType } = require('@kafkajs/confluent-schema-registry')
    
    const schema = `
      {
        "definitions" : {
          "record:examples.Person" : {
            "type" : "object",
            "required" : [ "fullName" ],
            "additionalProperties" : false,
            "properties" : {
              "fullName" : {
                "type" : "string"
              }
            }
          }
        },
        "$ref" : "#/definitions/record:examples.Person"
      }
    `
    const { id } = await registry.register({ type: SchemaType.JSON, schema })
    

    https://kafkajs.github.io/confluent-schema-registry/docs/usage#json-schema

    Later in the page, it shows how to encode a payload with the schema ID, which can then be passed into your producer as a Buffer type