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.

Don’t call GetSchema for every RPC method call. The schema typically doesn’t change often, so call GetSchema only one time and store the returned schema for subsequent calls. If the event schema changes, for example, when an administrator adds a field to the event definition, the schema ID changes. Compare the schema ID with the latest schema ID retrieved from PublishResponse or FetchResponse. If the schema ID changes, call GetSchema to retrieve the new schema. Calling GetSchema unnecessarily can slow down your app’s performance.

Important

rpc GetSchema (SchemaRequest) returns (SchemaInfo);

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.

Note

For more information about the fields in SchemaRequest and SchemaInfo, see the Pub/Sub API proto file.