List the revisions for a schema

List the revisions for a schema

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

C++

Before trying this sample, follow the C++ setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub C++ API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

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

C#

Before trying this sample, follow the C# setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub C# API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  using 
  
  Google.Api.Gax.ResourceNames 
 
 ; 
 using 
  
  Google.Cloud.PubSub.V1 
 
 ; 
 using 
  
 System.Collections.Generic 
 ; 
 using 
  
 System.Linq 
 ; 
 public 
  
 class 
  
 ListSchemaRevisionsSample 
 { 
  
 public 
  
 IEnumerable<Schema> 
  
 ListSchemaRevisions 
 ( 
 string 
  
 projectId 
 , 
  
 string 
  
 schemaId 
 ) 
  
 { 
  
  SchemaServiceClient 
 
  
 schemaService 
  
 = 
  
  SchemaServiceClient 
 
 . 
  Create 
 
 (); 
  
  SchemaName 
 
  
 schemaName 
  
 = 
  
  SchemaName 
 
 . 
  FromProjectSchema 
 
 ( 
 projectId 
 , 
  
 schemaId 
 ); 
  
 return 
  
 schemaService 
 . 
  ListSchemaRevisions 
 
 ( 
 schemaName 
 ). 
 ToList 
 (); 
  
 } 
 } 
 

Go

Before trying this sample, follow the Go setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Go API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  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 
  
 listSchemaRevisions 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 schemaID 
  
 string 
 ) 
  
 ([] 
 * 
 pubsubpb 
 . 
 Schema 
 , 
  
 error 
 ) 
  
 { 
  
 // projectID := "my-project-id" 
  
 // schemaID := "my-schema-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 
 . 
 ListSchemaRevisionsRequest 
 { 
  
 Name 
 : 
  
 fmt 
 . 
 Sprintf 
 ( 
 "projects/%s/schemas/%s" 
 , 
  
 projectID 
 , 
  
 schemaID 
 ), 
  
 View 
 : 
  
 pubsubpb 
 . 
 SchemaView_FULL 
 , 
  
 } 
  
 schemaIter 
  
 := 
  
 client 
 . 
 ListSchemaRevisions 
 ( 
 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 revision: %#v\n" 
 , 
  
 sc 
 ) 
  
 schemas 
  
 = 
  
 append 
 ( 
 schemas 
 , 
  
 sc 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Got %d schema revisions" 
 , 
  
 len 
 ( 
 schemas 
 )) 
  
 return 
  
 schemas 
 , 
  
 nil 
 } 
 

Java

Before trying this sample, follow the Java setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Java API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 com.google.cloud.pubsub.v1. SchemaServiceClient 
 
 ; 
 import 
  
 com.google.pubsub.v1. Schema 
 
 ; 
 import 
  
 com.google.pubsub.v1. SchemaName 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 public 
  
 class 
 ListSchemaRevisionsExample 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 ... 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 String 
  
 projectId 
  
 = 
  
 "your-project-id" 
 ; 
  
 String 
  
 schemaId 
  
 = 
  
 "your-schema-id" 
 ; 
  
 listSchemaRevisionsExample 
 ( 
 projectId 
 , 
  
 schemaId 
 ); 
  
 } 
  
 public 
  
 static 
  
 void 
  
 listSchemaRevisionsExample 
 ( 
 String 
  
 projectId 
 , 
  
 String 
  
 schemaId 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
  SchemaName 
 
  
 schemaName 
  
 = 
  
  SchemaName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 schemaId 
 ); 
  
 try 
  
 ( 
  SchemaServiceClient 
 
  
 schemaServiceClient 
  
 = 
  
  SchemaServiceClient 
 
 . 
 create 
 ()) 
  
 { 
  
 for 
  
 ( 
  Schema 
 
  
 schema 
  
 : 
  
 schemaServiceClient 
 . 
 listSchemaRevisions 
 ( 
 schemaName 
 ). 
 iterateAll 
 ()) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 schema 
 ); 
  
 } 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Listed schema revisions." 
 ); 
  
 } 
  
 } 
 } 
 

PHP

Before trying this sample, follow the PHP setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub PHP API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  /** 
 * Lists all schema revisions for the named schema. 
 * 
 * @param string $projectId The ID of your Google Cloud Platform project. 
 * @param string $schemaId The ID of the schema. 
 * @return void $name 
 */ 
 function list_schema_revisions(string $projectId, string $schemaId): void 
 { 
 $client = new PubSubClient([ 
 'projectId' => $projectId 
 ]); 
 try { 
 $schema = $client->schema($schemaId); 
 $revisions = $schema->listRevisions(); 
 foreach ($revisions['schemas'] as $revision) { 
 printf('Got a schema revision: %s' . PHP_EOL, $revision['revisionId']); 
 } 
 print('Listed schema revisions.' . PHP_EOL); 
 } catch (NotFoundException $ex) { 
 printf('%s not found' . PHP_EOL, $schemaId); 
 } 
 } 
 

Ruby

Before trying this sample, follow the Ruby setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Ruby API reference documentation .

To authenticate to Pub/Sub, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  # schema_id = "your-schema-id" 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  PubSub 
 
 . 
  new 
 
 schemas 
  
 = 
  
 pubsub 
 . 
 schemas 
 response 
  
 = 
  
 schemas 
 . 
 list_schema_revisions 
  
 \ 
  
 name 
 : 
  
 pubsub 
 . 
 schema_path 
 ( 
 schema_id 
 ), 
  
 view 
 : 
  
 Google 
 :: 
 Cloud 
 :: 
  PubSub 
 
 :: 
  V1 
 
 :: 
 SchemaView 
 :: 
 FULL 
 puts 
  
 "Listed revisions of schema 
 #{ 
 schema_id 
 } 
 " 
 response 
 . 
 each 
  
 do 
  
 | 
 revision_schema 
 | 
  
 puts 
  
 revision_schema 
 . 
 revision_id 
 end 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

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