GetSchema RPC Method
The GetSchema
method returns the schema of an event topic using the schema ID. Use the schema to encode the payload in the Avro format of the event to publish, or to decode the payload of a received event.
Because the schema typically doesn’t change often, we recommend that you call GetSchema
once and use the returned schema for all operations. If the event schema changes, for example, when an administrator adds a field to the event definition, the schema ID changes. We recommend that you store the schema ID and compare it with the latest schema ID retrieved from PublishResponse or FetchResponse. If the schema ID changes, call GetSchema
to retrieve the new schema.
To get the schema ID for the SchemaRequest
parameter, do one of the following.
- Call GetTopic. The return value of this method is TopicInfo. TopicInfo contains
schema_id
, which represents the latest schema. We recommend that you publish events with the latest schema. - For events received from the event bus, get the schema ID from the event message in the FetchResponse,
ProducerEvent.schema_id
. Use this schema for deserialization. For events published to the event bus, get the schema ID from the PublishResponse.
You can still publish events with an old schema saved from an earlier GetTopic
call. You use an Avro code generator to generate classes based on Avro types and deploy your app. If the event schema changes later, you can still publish and subscribe to the events as long as the schema differences are resolvable by the Avro schema resolution rules.
For more information about the fields in SchemaRequest and SchemaInfo, see the Pub/Sub API proto file.