List schemas for a topic

This document shows you how to list schemas for Pub/Sub topics.

Before you begin

Required roles and permissions

To get the permissions that you need to list schemas and manage them, 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 list schemas and manage them. To see the exact permissions that are required, expand the Required permissionssection:

Required permissions

The following permissions are required to list schemas and manage them:

  • 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.

List schemas

You can list the schemas in a Google Cloud project using the Google Cloud console, the gcloud CLI, the Pub/Sub API, or the Cloud Client Libraries.

Console

  • In the Google Cloud console, go to the Pub/Sub schemaspage.

    Go to Schemas

    The list of schemas is displayed.

gcloud

gcloud pubsub schemas list

Use the gcloud pubsub schemas list --view=FULL command to see the latest definition for each schema.

REST

To list the schemas in a project, send a GET request like the following:

GET https://pubsub.googleapis.com/v1/projects/ PROJECT_ID 
/schemas

If successful, the response body contains a JSON object containing the latest revision for all schemas in the project .

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 .

  namespace 
  
 pubsub 
  
 = 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 pubsub 
 ; 
 []( 
 pubsub 
 :: 
 SchemaServiceClient 
  
 client 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 project_id 
 ) 
  
 { 
  
 auto 
  
 const 
  
 parent 
  
 = 
  
 google 
 :: 
 cloud 
 :: 
 Project 
 ( 
 project_id 
 ). 
 FullName 
 (); 
  
 for 
  
 ( 
 auto 
&  
 s 
  
 : 
  
 client 
 . 
 ListSchemas 
 ( 
 parent 
 )) 
  
 { 
  
 if 
  
 ( 
 ! 
 s 
 ) 
  
 throw 
  
 std 
 :: 
 move 
 ( 
 s 
 ). 
 status 
 (); 
  
 std 
 :: 
 cout 
 << 
 "Schema: " 
 << 
 s 
 - 
> DebugString 
 () 
 << 
 " 
 \n 
 " 
 ; 
  
 } 
 } 
 

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 .

  using 
  
  Google.Api.Gax.ResourceNames 
 
 ; 
 using 
  
  Google.Cloud.PubSub.V1 
 
 ; 
 using 
  
 System.Collections.Generic 
 ; 
 public 
  
 class 
  
 ListSchemasSample 
 { 
  
 public 
  
 IEnumerable<Schema> 
  
 ListSchemas 
 ( 
 string 
  
 projectId 
 ) 
  
 { 
  
  SchemaServiceClient 
 
  
 schemaService 
  
 = 
  
  SchemaServiceClient 
 
 . 
  Create 
 
 (); 
  
  ProjectName 
 
  
 projectName 
  
 = 
  
  ProjectName 
 
 . 
  FromProject 
 
 ( 
 projectId 
 ); 
  
 var 
  
 schemas 
  
 = 
  
 schemaService 
 . 
  ListSchemas 
 
 ( 
 projectName 
 ); 
  
 return 
  
 schemas 
 ; 
  
 } 
 } 
 

Go

The following sample uses the major version of the Go Pub/Sub client library (v2). If you are still using the v1 library, see the migration guide to v2 . To see a list of v1 code samples, see the deprecated code samples .

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 .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 pubsub 
  
 "cloud.google.com/go/pubsub/v2/apiv1" 
  
 "cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" 
  
 "google.golang.org/api/iterator" 
 ) 
 func 
  
 listSchemas 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
  
 string 
 ) 
  
 ([] 
 * 
 pubsubpb 
 . 
 Schema 
 , 
  
 error 
 ) 
  
 { 
  
 // projectID := "my-project-id" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
 NewSchemaClient 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 nil 
 , 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub.NewSchemaClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 var 
  
 schemas 
  
 [] 
 * 
 pubsubpb 
 . 
 Schema 
  
 req 
  
 := 
  
& pubsubpb 
 . 
 ListSchemasRequest 
 { 
  
 Parent 
 : 
  
 fmt 
 . 
 Sprintf 
 ( 
 "projects/%s" 
 , 
  
 projectID 
 ), 
  
 View 
 : 
  
 pubsubpb 
 . 
 SchemaView_FULL 
 , 
  
 } 
  
 schemaIter 
  
 := 
  
 client 
 . 
 ListSchemas 
 ( 
 ctx 
 , 
  
 req 
 ) 
  
 for 
  
 { 
  
 sc 
 , 
  
 err 
  
 := 
  
 schemaIter 
 . 
 Next 
 () 
  
 if 
  
 err 
  
 == 
  
 iterator 
 . 
 Done 
  
 { 
  
 break 
  
 } 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 nil 
 , 
  
 fmt 
 . 
 Errorf 
 ( 
 "schemaIter.Next: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Got schema: %#v\n" 
 , 
  
 sc 
 ) 
  
 schemas 
  
 = 
  
 append 
 ( 
 schemas 
 , 
  
 sc 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Got %d schemas" 
 , 
  
 len 
 ( 
 schemas 
 )) 
  
 return 
  
 schemas 
 , 
  
 nil 
 } 
 

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 .

  import 
  
 com.google.cloud.pubsub.v1. SchemaServiceClient 
 
 ; 
 import 
  
 com.google.pubsub.v1. ProjectName 
 
 ; 
 import 
  
 com.google.pubsub.v1. Schema 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 public 
  
 class 
 ListSchemasExample 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 ... 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 String 
  
 projectId 
  
 = 
  
 "your-project-id" 
 ; 
  
 listSchemasExample 
 ( 
 projectId 
 ); 
  
 } 
  
 public 
  
 static 
  
 void 
  
 listSchemasExample 
 ( 
 String 
  
 projectId 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
  ProjectName 
 
  
 projectName 
  
 = 
  
  ProjectName 
 
 . 
 of 
 ( 
 projectId 
 ); 
  
 try 
  
 ( 
  SchemaServiceClient 
 
  
 schemaServiceClient 
  
 = 
  
  SchemaServiceClient 
 
 . 
 create 
 ()) 
  
 { 
  
 for 
  
 ( 
  Schema 
 
  
 schema 
  
 : 
  
 schemaServiceClient 
 . 
 listSchemas 
 ( 
 projectName 
 ). 
 iterateAll 
 ()) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 schema 
 ); 
  
 } 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Listed schemas." 
 ); 
  
 } 
  
 } 
 } 
 

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 .

  // Imports the Google Cloud client library 
 const 
  
 { 
 PubSub 
 } 
  
 = 
  
 require 
 ( 
 ' @google-cloud/pubsub 
' 
 ); 
 // Creates a client; cache this for further use 
 const 
  
 pubSubClient 
  
 = 
  
 new 
  
  PubSub 
 
 (); 
 async 
  
 function 
  
 listSchemas 
 () 
  
 { 
  
 for 
  
 await 
  
 ( 
 const 
  
 s 
  
 of 
  
 pubSubClient 
 . 
 listSchemas 
 ()) 
  
 { 
  
 console 
 . 
 log 
 ( 
 s 
 . 
 name 
 ); 
  
 } 
  
 console 
 . 
 log 
 ( 
 'Listed schemas.' 
 ); 
 } 
 

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 .

  // Imports the Google Cloud client library 
 import 
  
 { 
 PubSub 
 } 
  
 from 
  
 '@google-cloud/pubsub' 
 ; 
 // Creates a client; cache this for further use 
 const 
  
 pubSubClient 
  
 = 
  
 new 
  
 PubSub 
 (); 
 async 
  
 function 
  
 listSchemas 
 () 
  
 { 
  
 for 
  
 await 
  
 ( 
 const 
  
 s 
  
 of 
  
 pubSubClient 
 . 
 listSchemas 
 ()) 
  
 { 
  
 console 
 . 
 log 
 ( 
 s 
 . 
 name 
 ); 
  
 } 
  
 console 
 . 
 log 
 ( 
 'Listed schemas.' 
 ); 
 } 
 

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 .

  use Google\Cloud\PubSub\PubSubClient; 
 /** 
 * List schemas in the project. 
 * 
 * @param string $projectId 
 */ 
 function list_schemas($projectId) 
 { 
 $pubsub = new PubSubClient([ 
 'projectId' => $projectId, 
 ]); 
 $schemas = $pubsub->schemas(); 
 foreach ($schemas as $schema) { 
 printf('Schema name: %s' . PHP_EOL, $schema->name()); 
 } 
 } 
 

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 .

  from 
  
 google.cloud.pubsub 
  
 import 
 SchemaServiceClient 
 # TODO(developer): Replace these variables before running the sample. 
 # project_id = "your-project-id" 
 project_path 
 = 
 f 
 "projects/ 
 { 
 project_id 
 } 
 " 
 schema_client 
 = 
 SchemaServiceClient 
 () 
 for 
 schema 
 in 
 schema_client 
 . 
 list_schemas 
 ( 
 request 
 = 
 { 
 "parent" 
 : 
 project_path 
 }): 
 print 
 ( 
 schema 
 ) 
 print 
 ( 
 "Listed schemas." 
 ) 
 

Ruby

The following sample uses Ruby Pub/Sub client library v3. If you are still using the v2 library, see the migration guide to v3 . To see a list of Ruby v2 code samples, see the deprecated code samples .

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 .

  pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  PubSub 
 
 . 
  new 
 
 schema_service 
  
 = 
  
 pubsub 
 . 
 schemas 
 schemas 
  
 = 
  
 schema_service 
 . 
 list_schemas 
  
 \ 
  
 parent 
 : 
  
 pubsub 
 . 
 project_path 
 , 
  
 view 
 : 
  
 Google 
 :: 
 Cloud 
 :: 
  PubSub 
 
 :: 
  V1 
 
 :: 
 SchemaView 
 :: 
 FULL 
 puts 
  
 "Schemas in project:" 
 schemas 
 . 
 each 
  
 do 
  
 | 
 schema 
 | 
  
 puts 
  
 schema 
 . 
 name 
 end 
 

What's next

Create a Mobile Website
View Site in Mobile | Classic
Share by: