Kafka Schema Registry - block messages that aren't accepted in the schema registry in kafka

981 Views Asked by At

I have a schema in kafka and I need that every time I post a post in this topic, the schema that I registered checks if it is in the same pattern that is being sent.

My schema is: enter image description here

Curl post:

curl -X POST -H "Content-Type: application/vnd.schemaregistry.v1+json" --data '{"schema": "{\"type\":\"record\",\"name\":\"Operacao\",\"namespace\":\"data.brado.operacao\",\"fields\":[{\"name\":\"id_operacao\",\"type\":\"string\"},{\"name\":\"tipo_container\",\"type\":\"string\"}, {\"name\":\"descricao_operacao\",\"type\":\"string\"},{\"name\":\"entrega\",\"type\":\"string\"},{\"name\":\"coleta\",\"type\":\"string\"},{\"name\":\"descricao_checklist\",\"type\":\"string\"},{\"name\":\"cheio\",\"type\":\"string\"},{\"name\":\"ativo\",\"type\":\"string\"},{\"name\":\"tipo_operacao\",\"type\":\"string\"} ]}"}' http://localhost:38081/subjects/teste/versions

What I need is that when I make a post in the topic it doesn't allow me to send it if it doesn't have this pattern

I was supposed to accuse an error here, because I'm not sending the right schema enter image description here

And it would work in that caseenter image description here

Can anyone help me how to do this check in schema? I've looked everywhere I've found and I haven't found any answers to this.

1

There are 1 best solutions below

8
OneCricketeer On

The schema registry will only block producers that are configured to use it. By default, the broker will not enforce a schema. For that, you need to pay for Confluent Server.

Hard to tell what image you are showing (what REST endpoint you are using), but if it is the Confluent Kafka REST Proxy, then refer to quick-start section on producing and consuming Avro.

# Produce a message using Avro embedded data, including the schema which will
# be registered with schema registry and used to validate and serialize
# before storing the data in Kafka
curl -X POST -H "Content-Type: application/vnd.kafka.avro.v2+json" \
      -H "Accept: application/vnd.kafka.v2+json" \
      --data '{"value_schema": "{\"type\": \"record\", \"name\": \"User\", \"fields\": [{\"name\": \"name\", \"type\": \"string\"}]}", "records": [{"value": {"name": "testUser"}}]}' \
      "http://localhost:8082/topics/avrotest"

Without these specifics, you're just sending plain-text JSON, which will do no schema checks.

If you have direct access to the Kafka cluster, then writing an Avro producer client would be easier since you don't need to embed the key and/or value schemas every time you want to send an event.