This document shows you how to create schemas for Pub/Sub topics.
Before you begin
Before you create a schema, complete the following:
- Learn about topics and the publishing workflow.
- Understand how Pub/Sub schemas work.
- Create a topic.
Required roles and permissions
To get the permissions that you need to create and manage schemas,
ask your administrator to grant you the
Pub/Sub Editor (roles/pubsub.editor
) IAM role on your project.
For more information about granting roles, see Manage access to projects, folders, and organizations.
This predefined role contains the permissions required to create and manage schemas. To see the exact permissions that are required, expand the Required permissions section:
Required permissions
The following permissions are required to create and manage schemas:
-
Create schema:
pubsub.schemas.create
-
Attach schema to topic:
pubsub.schemas.attach
-
Commit a schema revision:
pubsub.schemas.commit
-
Delete a schema or a schema revision:
pubsub.schemas.delete
-
Get a schema or schema revisions:
pubsub.schemas.get
-
List schemas:
pubsub.schemas.list
-
List schema revisions:
pubsub.schemas.listRevisions
-
Rollback a schema:
pubsub.schemas.rollback
-
Validate a message:
pubsub.schemas.validate
-
Get the IAM policy for a schema:
pubsub.schemas.getIamPolicy
-
Configure the IAM policy for a schema:
pubsub.schemas.setIamPolicy
You might also be able to get these permissions with custom roles or other predefined roles.
You can grant roles and permissions to principals such as users, groups, domains, or service accounts. You can create a schema in one project and attach it to a topic located in a different project. Ensure that you have the required permissions for each project.
Create a schema
You can create a schema using the Google Cloud console, the gcloud CLI, the Pub/Sub API, or the Cloud Client Libraries.
Before creating a schema, review this important information about schemas.
Console
To create a schema, follow these steps:
In the Google Cloud console, go to the Pub/Sub schemas page.
Click Create schema.
In the Schema ID field, enter an ID for your schema.
For guidelines to name a schema, see Guidelines to name a topic, subscription, or snapshot.
For Schema type, select either Avro or Protocol Buffer.
Learn more about schema types.
In the Schema definition field, enter the Avro or Protocol Buffer definition for your schema.
For example, here's a sample schema in Avro.
{ "type": "record", "name": "Avro", "fields": [ { "name": "ProductName", "type": "string", "default": "" }, { "name": "SKU", "type": "int", "default": 0 }, { "name": "InStock", "type": "boolean", "default": false } ] }
Optional: Click Validate definition to check if the schema definition is correct.
The validation check does not check the compatibility of the schema with the messages to be published. Test messages in the next step.
Optional: You can test if messages with the correct schema get published.
Click Test message.
In the Test message window, select a type of Message encoding.
In the Message body, enter a test message.
Click Test.
For example, here's a sample message for the test schema. In this example, select the Message encoding as
JSON
.{"ProductName":"GreenOnions", "SKU":34543, "InStock":true}
Exit the test message page.
Click Create to save the schema.
gcloud
gcloud pubsub schemas create SCHEMA_ID \ --type=SCHEMA_TYPE \ --definition=SCHEMA_DEFINITION
Where:
- SCHEMA_TYPE is either
avro
orprotocol-buffer
. - SCHEMA_DEFINITION is a
string
containing the definition of the schema, formatted according to the chosen schema type.
You can also specify the schema definition in a file:
gcloud pubsub schemas create SCHEMA_ID \ --type=SCHEMA_TYPE \ --definition-file=SCHEMA_DEFINITION_FILE
Where:
- SCHEMA_TYPE is either
avro
orprotocol-buffer
. - SCHEMA_DEFINITION_FILE is a
string
containing the path to the file with the definition of the schema, formatted according to the chosen schema type.
REST
To create a schema, send a POST request like the following:
POST https://pubsub.googleapis.com/v1/projects/PROJECT_ID/schemas/SCHEMA_ID Authorization: Bearer $(gcloud auth application-default print-access-token) Content-Type: application/json --data @response-body.json
Specify the following fields in the request body:
{ "definition": SCHEMA_DEFINITION "type": SCHEMA_TYPE }
Where:
- SCHEMA_TYPE is either
avro
orprotocol-buffer
. - SCHEMA_DEFINITION is a string containing the definition of the schema, formatted according to the chosen schema type.
The response body should contain a JSON representation of a schema resource. For example:
{ "name": SCHEMA_NAME, "type": SCHEMA_TYPE, "definition": SCHEMA_DEFINITION "revisionId": REVISION_ID "revisionCreateTime": REVISION_CREATE_TIME }
Where:
- REVISION_ID is the server-generated ID for the revision.
- REVISION_CREATE_TIME is the ISO 8601 timestamp at which the revision was created.
C++
Before trying this sample, follow the C++ setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub C++ API reference documentation.
Avro
Proto
C#
Before trying this sample, follow the C# setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub C# API reference documentation.
Avro
Proto
Go
Before trying this sample, follow the Go setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Go API reference documentation.
Avro
Proto
Java
Before trying this sample, follow the Java setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Java API reference documentation.
Avro
Proto
Node.js
Before trying this sample, follow the Node.js setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Node.js API reference documentation.
Avro
Proto
Node.js
Before trying this sample, follow the Node.js setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Node.js API reference documentation.
Avro
Proto
PHP
Before trying this sample, follow the PHP setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub PHP API reference documentation.
Avro
Proto
Python
Before trying this sample, follow the Python setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Python API reference documentation.
Avro
Proto
Ruby
Before trying this sample, follow the Ruby setup instructions in Quickstart: Using Client Libraries. For more information, see the Pub/Sub Ruby API reference documentation.
Avro
Proto
After a schema is created, you can see the details of the schema in the Schemas page.
You can associate the schema with a topic.