confluent-schema-registry icon indicating copy to clipboard operation
confluent-schema-registry copied to clipboard

How to get the schema subject from a message?

Open Matan-Yadaev-Forter opened this issue 7 months ago • 4 comments

I have a service that publishes messages to a queue (not Kafka), and these messages are encoded using a schema from the Schema Registry.

On the consumer side, I want to validate that the message was encoded using a schema subject that the consumer knows and supports.

Extracting the schema ID is straightforward (bytes 1-4 of the message), but I couldn't find an API that allows retrieving the subject associated with a given schema ID.

Is there a recommended way to achieve this? Any ideas or best practices?

Thanks!

Matan-Yadaev-Forter avatar Apr 08 '25 17:04 Matan-Yadaev-Forter

I have the same need.

In our case, we're processing events with multiple event types sent on a single kafka topic. In other words, we're receiving events on a given topic that were encoded with varying schemas.

When processing these events, we'd like to determine the schema subject of the schema used to encode the received event so we can handle it appropriately.

As @Matan-Yadaev-Forter indicated, it's possible to get the id of the schema used to decode the incoming event message.

It'd be nice if the library exposed a convenience method for retrieving the schema subject:

const schemaSubject = getSchemaSubjectFromMessage(buffer);

joyrexus avatar Apr 09 '25 16:04 joyrexus

Looking at the decode method, looks like it'd be possible to fetch a message's schema by using the wireDecoder (to get the registryId) and the existing getSchema method:

Image

joyrexus avatar Apr 09 '25 16:04 joyrexus

@joyrexus You can get the schema this way, but I didn't find how I can extract the subject out of it.

Matan-Yadaev-Forter avatar Apr 10 '25 09:04 Matan-Yadaev-Forter

@Matan-Yadaev-Forter You should be able to use the registry API directly to get the schema subject for each version: GET /schemas/ids/{int: id}/versions

Image

joyrexus avatar Apr 10 '25 20:04 joyrexus