Edit a topic to associate a schema

Edit a topic. Associate a schema and specify revisions.

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 
 ; 
 namespace 
  
 pubsub_admin 
  
 = 
  
 :: 
 google 
 :: 
 cloud 
 :: 
 pubsub_admin 
 ; 
 []( 
 pubsub_admin 
 :: 
 TopicAdminClient 
  
 client 
 , 
  
 std 
 :: 
 string 
  
 project_id 
 , 
  
 std 
 :: 
 string 
  
 topic_id 
 , 
  
 std 
 :: 
 string 
  
 schema_id 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 encoding 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 first_revision_id 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 last_revision_id 
 ) 
  
 { 
  
 google 
 :: 
 pubsub 
 :: 
 v1 
 :: 
 Topic 
  
 request 
 ; 
  
 request 
 . 
 set_name 
 ( 
 pubsub 
 :: 
 Topic 
 ( 
 project_id 
 , 
  
 std 
 :: 
 move 
 ( 
 topic_id 
 )). 
 FullName 
 ()); 
  
 request 
 . 
 mutable_schema_settings 
 () 
 - 
> set_schema 
 ( 
  
 pubsub 
 :: 
 Schema 
 ( 
 std 
 :: 
 move 
 ( 
 project_id 
 ), 
  
 std 
 :: 
 move 
 ( 
 schema_id 
 )). 
 FullName 
 ()); 
  
 request 
 . 
 mutable_schema_settings 
 () 
 - 
> set_encoding 
 ( 
  
 encoding 
  
 == 
  
 "JSON" 
  
 ? 
  
 google 
 :: 
 pubsub 
 :: 
 v1 
 :: 
 JSON 
  
 : 
  
 google 
 :: 
 pubsub 
 :: 
 v1 
 :: 
 BINARY 
 ); 
  
 request 
 . 
 mutable_schema_settings 
 () 
 - 
> set_first_revision_id 
 ( 
 first_revision_id 
 ); 
  
 request 
 . 
 mutable_schema_settings 
 () 
 - 
> set_last_revision_id 
 ( 
 last_revision_id 
 ); 
  
 auto 
  
 topic 
  
 = 
  
 client 
 . 
 CreateTopic 
 ( 
 request 
 ); 
  
 // Note that kAlreadyExists is a possible error when the 
  
 // library retries. 
  
 if 
  
 ( 
 topic 
 . 
 status 
 (). 
 code 
 () 
  
 == 
  
 google 
 :: 
 cloud 
 :: 
 StatusCode 
 :: 
 kAlreadyExists 
 ) 
  
 { 
  
 std 
 :: 
 cout 
 << 
 "The topic already exists 
 \n 
 " 
 ; 
  
 return 
 ; 
  
 } 
  
 if 
  
 ( 
 ! 
 topic 
 ) 
  
 throw 
  
 std 
 :: 
 move 
 ( 
 topic 
 ). 
 status 
 (); 
  
 std 
 :: 
 cout 
 << 
 "The topic was successfully created: " 
 << 
 topic 
 - 
> 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.Cloud.PubSub.V1 
 
 ; 
 using 
  
  Grpc.Core 
 
 ; 
 using 
  
 System 
 ; 
 public 
  
 class 
  
 CreateTopicWithSchemaRevisionsSample 
 { 
  
 public 
  
 Topic 
  
 CreateTopicWithSchemaRevisions 
 ( 
 string 
  
 projectId 
 , 
  
 string 
  
 topicId 
 , 
  
 string 
  
 schemaId 
 , 
  
 string 
  
 firstRevisionId 
 , 
  
 string 
  
 lastRevisionId 
 , 
  
  Encoding 
 
  
 encoding 
 ) 
  
 { 
  
  PublisherServiceApiClient 
 
  
 publisher 
  
 = 
  
  PublisherServiceApiClient 
 
 . 
  Create 
 
 (); 
  
 var 
  
 topicName 
  
 = 
  
  TopicName 
 
 . 
  FromProjectTopic 
 
 ( 
 projectId 
 , 
  
 topicId 
 ); 
  
  Topic 
 
  
 topic 
  
 = 
  
 new 
  
  Topic 
 
  
 { 
  
 TopicName 
  
 = 
  
 topicName 
 , 
  
 SchemaSettings 
  
 = 
  
 new 
  
  SchemaSettings 
 
  
 { 
  
 SchemaAsSchemaName 
  
 = 
  
  SchemaName 
 
 . 
  FromProjectSchema 
 
 ( 
 projectId 
 , 
  
 schemaId 
 ), 
  
 FirstRevisionId 
  
 = 
  
 firstRevisionId 
 , 
  
 LastRevisionId 
  
 = 
  
 lastRevisionId 
 , 
  
 Encoding 
  
 = 
  
 encoding 
 , 
  
 } 
  
 }; 
  
  Topic 
 
  
 receivedTopic 
  
 = 
  
 null 
 ; 
  
 try 
  
 { 
  
 receivedTopic 
  
 = 
  
 publisher 
 . 
  CreateTopic 
 
 ( 
 topic 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 $"Topic {topic. Name 
} created." 
 ); 
  
 } 
  
 catch 
  
 ( 
  RpcException 
 
  
 e 
 ) 
  
 when 
  
 ( 
 e 
 . 
  Status 
 
 . 
  StatusCode 
 
  
 == 
  
  StatusCode 
 
 . 
  AlreadyExists 
 
 ) 
  
 { 
  
 Console 
 . 
 WriteLine 
 ( 
 $"Topic {topicName} already exists." 
 ); 
  
 } 
  
 return 
  
 receivedTopic 
 ; 
  
 } 
 } 
 

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" 
  
 "cloud.google.com/go/pubsub/v2" 
  
 "cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" 
 ) 
 func 
  
 createTopicWithSchemaRevisions 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 topicID 
 , 
  
 schemaID 
 , 
  
 firstRevisionID 
 , 
  
 lastRevisionID 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // topicID := "my-topic" 
  
 // schemaID := "my-schema-id" 
  
 // firstRevisionID := "my-revision-id" 
  
 // lastRevisionID := "my-revision-id" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
 NewClient 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 topic 
  
 := 
  
& pubsubpb 
 . 
 Topic 
 { 
  
 Name 
 : 
  
 fmt 
 . 
 Sprintf 
 ( 
 "projects/%s/topics/%s" 
 , 
  
 projectID 
 , 
  
 topicID 
 ), 
  
 SchemaSettings 
 : 
  
& pubsubpb 
 . 
 SchemaSettings 
 { 
  
 Schema 
 : 
  
 fmt 
 . 
 Sprintf 
 ( 
 "projects/%s/schemas/%s" 
 , 
  
 projectID 
 , 
  
 schemaID 
 ), 
  
 FirstRevisionId 
 : 
  
 firstRevisionID 
 , 
  
 LastRevisionId 
 : 
  
 lastRevisionID 
 , 
  
 // Alternative encoding is pubsubpb.Encoding_JSON 
  
 Encoding 
 : 
  
 pubsubpb 
 . 
 Encoding_BINARY 
 , 
  
 }, 
  
 } 
  
 t 
 , 
  
 err 
  
 := 
  
 client 
 . 
 TopicAdminClient 
 . 
 CreateTopic 
 ( 
 ctx 
 , 
  
 topic 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "CreateTopicWithConfig: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Created topic with schema revision: %#v\n" 
 , 
  
 t 
 ) 
  
 return 
  
 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.api.gax.rpc. AlreadyExistsException 
 
 ; 
 import 
  
 com.google.cloud.pubsub.v1. TopicAdminClient 
 
 ; 
 import 
  
 com.google.pubsub.v1. Encoding 
 
 ; 
 import 
  
 com.google.pubsub.v1. SchemaName 
 
 ; 
 import 
  
 com.google.pubsub.v1. SchemaSettings 
 
 ; 
 import 
  
 com.google.pubsub.v1. Topic 
 
 ; 
 import 
  
 com.google.pubsub.v1. TopicName 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 public 
  
 class 
 CreateTopicWithSchemaRevisionsExample 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 ... 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 // TODO(developer): Replace these variables before running the sample. 
  
 String 
  
 projectId 
  
 = 
  
 "your-project-id" 
 ; 
  
 String 
  
 topicId 
  
 = 
  
 "your-topic-id" 
 ; 
  
 // Use an existing schema. 
  
 String 
  
 schemaId 
  
 = 
  
 "your-schema-id" 
 ; 
  
 // Choose either BINARY or JSON message serialization in this topic. 
  
  Encoding 
 
  
 encoding 
  
 = 
  
  Encoding 
 
 . 
 BINARY 
 ; 
  
 // Set the minimum and maximum revsion ID 
  
 String 
  
 firstRevisionId 
  
 = 
  
 "your-revision-id" 
 ; 
  
 String 
  
 lastRevisionId 
  
 = 
  
 "your-revision-id" 
 ; 
  
 createTopicWithSchemaRevisionsExample 
 ( 
  
 projectId 
 , 
  
 topicId 
 , 
  
 schemaId 
 , 
  
 firstRevisionId 
 , 
  
 lastRevisionId 
 , 
  
 encoding 
 ); 
  
 } 
  
 public 
  
 static 
  
 void 
  
 createTopicWithSchemaRevisionsExample 
 ( 
  
 String 
  
 projectId 
 , 
  
 String 
  
 topicId 
 , 
  
 String 
  
 schemaId 
 , 
  
 String 
  
 firstRevisionid 
 , 
  
 String 
  
 lastRevisionId 
 , 
  
  Encoding 
 
  
 encoding 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
  TopicName 
 
  
 topicName 
  
 = 
  
  TopicName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 topicId 
 ); 
  
  SchemaName 
 
  
 schemaName 
  
 = 
  
  SchemaName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 schemaId 
 ); 
  
  SchemaSettings 
 
  
 schemaSettings 
  
 = 
  
  SchemaSettings 
 
 . 
 newBuilder 
 () 
  
 . 
 setSchema 
 ( 
 schemaName 
 . 
  toString 
 
 ()) 
  
 . 
  setFirstRevisionId 
 
 ( 
 firstRevisionid 
 ) 
  
 . 
  setLastRevisionId 
 
 ( 
 lastRevisionId 
 ) 
  
 . 
 setEncoding 
 ( 
 encoding 
 ) 
  
 . 
 build 
 (); 
  
 try 
  
 ( 
  TopicAdminClient 
 
  
 topicAdminClient 
  
 = 
  
  TopicAdminClient 
 
 . 
 create 
 ()) 
  
 { 
  
  Topic 
 
  
 topic 
  
 = 
  
 topicAdminClient 
 . 
 createTopic 
 ( 
  
  Topic 
 
 . 
 newBuilder 
 () 
  
 . 
 setName 
 ( 
 topicName 
 . 
  toString 
 
 ()) 
  
 . 
  setSchemaSettings 
 
 ( 
 schemaSettings 
 ) 
  
 . 
 build 
 ()); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Created topic with schema: " 
  
 + 
  
 topic 
 . 
  getName 
 
 ()); 
  
 } 
  
 catch 
  
 ( 
  AlreadyExistsException 
 
  
 e 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 schemaName 
  
 + 
  
 "already exists." 
 ); 
  
 } 
  
 } 
 } 
 

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 .

  use Google\Cloud\PubSub\PubSubClient; 
 use Google\Cloud\PubSub\Schema; 
 /** 
 * Create a topic with schema revisions. 
 * 
 * @param string $projectId Your Google Cloud Project ID. 
 * @param string $topicId Topic ID for the new topic. 
 * @param string $schemaId Existing schema id 
 * @param string $firstRevisionId Minimum revision Id 
 * @param string $lastRevisionId Maximum revision Id 
 * @param string $encoding Either BINARY or JSON 
 */ 
 function create_topic_with_schema_revisions( 
 string $projectId, 
 string $topicId, 
 string $schemaId, 
 string $firstRevisionId, 
 string $lastRevisionId, 
 string $encoding 
 ) { 
 $pubsub = new PubSubClient([ 
 'projectId' => $projectId, 
 ]); 
 $schema = $pubsub->schema($schemaId); 
 $topic = $pubsub->createTopic($topicId, [ 
 'schemaSettings' => [ 
 'schema' => $schema, 
 'encoding' => $encoding, 
 'firstRevisionId' => $firstRevisionId, 
 'lastRevisionId' => $lastRevisionId, 
 ] 
 ]); 
 printf('Topic %s created', $topic->name()); 
 } 
 

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 .

  # topic_id = "your-topic-id" 
 # schema_id = "your-schema-id" 
 # Choose either BINARY or JSON as valid message encoding in this topic. 
 # message_encoding = :BINARY 
 # first_revision_id = "your-first-revision-id" 
 # last_revision_id = "your-last-revision-id" 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  PubSub 
 
 . 
  new 
 
 topic_admin 
  
 = 
  
 pubsub 
 . 
  topic_admin 
 
 topic 
  
 = 
  
 topic_admin 
 . 
 create_topic 
  
 name 
 : 
  
 pubsub 
 . 
 topic_path 
 ( 
 topic_id 
 ), 
  
 schema_settings 
 : 
  
 { 
  
 schema 
 : 
  
 pubsub 
 . 
 schema_path 
 ( 
 schema_id 
 ), 
  
 encoding 
 : 
  
 message_encoding 
 , 
  
 first_revision_id 
 : 
  
 first_revision_id 
 , 
  
 last_revision_id 
 : 
  
 last_revision_id 
  
 } 
 puts 
  
 "Topic 
 #{ 
 topic 
 . 
 name 
 } 
 created with revision interval " 
  
 \ 
  
 "[ 
 #{ 
 first_revision_id 
 } 
 , 
 #{ 
 last_revision_id 
 } 
 ]." 
 

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: