Schema overview

A Pub/Sub schema is an optional feature that you can use to enforce the format of the data field in a Pub/Sub message.

A schema creates a contract between the publisher and subscriber about the format of the messages. Pub/Sub enforces this format. Schemas facilitate inter-team consumption of data streams in your organization by creating a central authority for message types and permissions. A Pub/Sub message schema defines the names and data types for the fields in a message.

You can create a schema and associate it with a topic to enforce the schema for published messages. If a specific message does not conform to the schema, the message is not published. You can also create additional revisions for a schema.

Types of schemas

You can create a schema in Pub/Sub by using one of the following frameworks:

For example, the following schema defines the inventory of a warehouse, first in the Avro format and then in the Protocol Buffer format.

Apache Avro format

{
 "type" : "record",
 "name" : "Avro",
 "fields" : [
   {
     "name" : "ProductName",
     "type" : "string",
     "default": ""
   },
   {
     "name" : "SKU",
     "type" : "int",
     "default": 0
   },
   {
     "name" : "InStock",
     "type" : "boolean",
     "default": false
   }
 ]
}

Protocol Buffer format

syntax = "proto3";
message ProtocolBuffer {
  string product_name = 1;
  int32