Commit a revision for an Avro schema

Commit a revision for an Avro 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 
 , 
  
 std 
 :: 
 string 
  
 const 
&  
 schema_definition_file 
 ) 
  
 { 
  
 std 
 :: 
 string 
  
 const 
  
 definition 
  
 = 
  
 ReadFile 
 ( 
 schema_definition_file 
 ); 
  
 google 
 :: 
 pubsub 
 :: 
 v1 
 :: 
 CommitSchemaRequest 
  
 request 
 ; 
  
 std 
 :: 
 string 
  
 const 
  
 name 
  
 = 
  
 google 
 :: 
 cloud 
 :: 
 pubsub 
 :: 
 Schema 
 ( 
 project_id 
 , 
  
 schema_id 
 ). 
 FullName 
 (); 
  
 request 
 . 
 set_name 
 ( 
 name 
 ); 
  
 request 
 . 
 mutable_schema 
 () 
 - 
> set_name 
 ( 
 name 
 ); 
  
 request 
 . 
 mutable_schema 
 () 
 - 
> set_type 
 ( 
 google 
 :: 
 pubsub 
 :: 
 v1 
 :: 
 Schema 
 :: 
 AVRO 
 ); 
  
 request 
 . 
 mutable_schema 
 () 
 - 
> set_definition 
 ( 
 definition 
 ); 
  
 auto 
  
 schema 
  
 = 
  
 client 
 . 
 CommitSchema 
 ( 
 request 
 ); 
  
 if 
  
 ( 
 schema 
 . 
 status 
 (). 
 code 
 () 
  
 == 
  
 google 
 :: 
 cloud 
 :: 
 StatusCode 
 :: 
 kAlreadyExists 
 ) 
  
 { 
  
 std 
 :: 
 cout 
 << 
 "The schema revision already exists 
 \n 
 " 
 ; 
  
 return 
 ; 
  
 } 
  
 if 
  
 ( 
 ! 
 schema 
 ) 
  
 throw 
  
 std 
 :: 
 move 
 ( 
 schema 
 ). 
 status 
 (); 
  
 std 
 :: 
 cout 
 << 
 "Schema revision successfully committed: " 
 << 
 schema 
 - 
> 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 
  
 System 
 ; 
 using 
  
 System.IO 
 ; 
 public 
  
 class 
  
 CommitAvroSchemaSample 
 { 
  
 public 
  
 Schema 
  
 CommitAvroSchema 
 ( 
 string 
  
 projectId 
 , 
  
 string 
  
 schemaId 
 , 
  
 string 
  
 pathToNewDefinition 
 ) 
  
 { 
  
  SchemaServiceClient 
 
  
 schemaService 
  
 = 
  
  SchemaServiceClient 
 
 . 
  Create 
 
 (); 
  
 var 
  
 schemaName 
  
 = 
  
  SchemaName 
 
 . 
  FromProjectSchema 
 
 ( 
 projectId 
 , 
  
 schemaId 
 ); 
  
 string 
  
 schemaDefinition 
  
 = 
  
 File 
 . 
 ReadAllText 
 ( 
 pathToNewDefinition 
 ); 
  
  Schema 
 
  
 schema 
  
 = 
  
 new 
  
  Schema 
 
  
 { 
  
 SchemaName 
  
 = 
  
 schemaName 
 , 
  
 Type 
  
 = 
  
  Schema 
 
 . 
  Type 
 
s . 
  Type 
 
 . 
  Avro 
 
 , 
  
 Definition 
  
 = 
  
 schemaDefinition 
  
 }; 
  
 schema 
  
 = 
  
 schemaService 
 . 
  CommitSchema 
 
 ( 
 schemaName 
 , 
  
 schema 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 $"Schema {schema. Name 
} committed." 
 ); 
  
 return 
  
 schema 
 ; 
  
 } 
 } 
 

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" 
  
 "os" 
  
 pubsub 
  
 "cloud.google.com/go/pubsub/v2/apiv1" 
  
 "cloud.google.com/go/pubsub/v2/apiv1/pubsubpb" 
 ) 
 // commitAvroSchema commits a new Avro schema revision to an existing schema. 
 func 
  
 commitAvroSchema 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 schemaID 
 , 
  
 avscFile 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // schemaID := "my-schema-id" 
  
 // avscFile = "path/to/an/avro/schema/file(.avsc)/formatted/in/json" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
 NewSchemaClient 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub.NewSchemaClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 // Read an Avro schema file formatted in JSON as a byte slice. 
  
 avscSource 
 , 
  
 err 
  
 := 
  
 os 
 . 
 ReadFile 
 ( 
 avscFile 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "error reading from file: %s" 
 , 
  
 avscFile 
 ) 
  
 } 
  
 schema 
  
 := 
  
& pubsubpb 
 . 
 Schema 
 { 
  
 Name 
 : 
  
 fmt 
 . 
 Sprintf 
 ( 
 "projects/%s/schemas/%s" 
 , 
  
 projectID 
 , 
  
 schemaID 
 ), 
  
 Type 
 : 
  
 pubsubpb 
 . 
 Schema_AVRO 
 , 
  
 Definition 
 : 
  
 string 
 ( 
 avscSource 
 ), 
  
 } 
  
 req 
  
 := 
  
& pubsubpb 
 . 
 CommitSchemaRequest 
 { 
  
 Name 
 : 
  
 fmt 
 . 
 Sprintf 
 ( 
 "projects/%s/schemas/%s" 
 , 
  
 projectID 
 , 
  
 schemaID 
 ), 
  
 Schema 
 : 
  
 schema 
 , 
  
 } 
  
 s 
 , 
  
 err 
  
 := 
  
 client 
 . 
 CommitSchema 
 ( 
 ctx 
 , 
  
 req 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "error calling CommitSchema: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Committed a schema using an Avro schema: %#v\n" 
 , 
  
 s 
 ) 
  
 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. NotFoundException 
 
 ; 
 import 
  
 com.google.cloud.pubsub.v1. SchemaServiceClient 
 
 ; 
 import 
  
 com.google.pubsub.v1. ProjectName 
 
 ; 
 import 
  
 com.google.pubsub.v1. Schema 
 
 ; 
 import 
  
 com.google.pubsub.v1. SchemaName 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 import 
  
 java.nio.file.Files 
 ; 
 import 
  
 java.nio.file.Paths 
 ; 
 public 
  
 class 
 CommitAvroSchemaExample 
  
 { 
  
 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" 
 ; 
  
 String 
  
 avscFile 
  
 = 
  
 "path/to/an/avro/schema/file/(.avsc)/formatted/in/json" 
 ; 
  
 commitAvroSchemaExample 
 ( 
 projectId 
 , 
  
 schemaId 
 , 
  
 avscFile 
 ); 
  
 } 
  
 public 
  
 static 
  
  Schema 
 
  
 commitAvroSchemaExample 
 ( 
 String 
  
 projectId 
 , 
  
 String 
  
 schemaId 
 , 
  
 String 
  
 avscFile 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
  ProjectName 
 
  
 projectName 
  
 = 
  
  ProjectName 
 
 . 
 of 
 ( 
 projectId 
 ); 
  
  SchemaName 
 
  
 schemaName 
  
 = 
  
  SchemaName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 schemaId 
 ); 
  
 // Read an Avro schema file formatted in JSON as a string. 
  
 String 
  
 avscSource 
  
 = 
  
 new 
  
 String 
 ( 
 Files 
 . 
 readAllBytes 
 ( 
 Paths 
 . 
 get 
 ( 
 avscFile 
 ))); 
  
 try 
  
 ( 
  SchemaServiceClient 
 
  
 schemaServiceClient 
  
 = 
  
  SchemaServiceClient 
 
 . 
 create 
 ()) 
  
 { 
  
  Schema 
 
  
 schema 
  
 = 
  
 schemaServiceClient 
 . 
 commitSchema 
 ( 
  
 schemaName 
 . 
  toString 
 
 (), 
  
  Schema 
 
 . 
 newBuilder 
 () 
  
 . 
 setName 
 ( 
 schemaName 
 . 
  toString 
 
 ()) 
  
 . 
  setType 
 
 ( 
  Schema 
 
 . 
 Type 
 . 
 AVRO 
 ) 
  
 . 
  setDefinition 
 
 ( 
 avscSource 
 ) 
  
 . 
 build 
 ()); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Committed a schema using an Avro schema:\n" 
  
 + 
  
 schema 
 ); 
  
 return 
  
 schema 
 ; 
  
 } 
  
 catch 
  
 ( 
  NotFoundException 
 
  
 e 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 schemaName 
  
 + 
  
 "does not exist." 
 ); 
  
 return 
  
 null 
 ; 
  
 } 
  
 } 
 } 
 

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\Core\Exception\NotFoundException; 
 use Google\Cloud\PubSub\PubSubClient; 
 /** 
 * Commit a new AVRO schema revision to an existing schema. 
 * 
 * @param string $projectId The ID of your Google Cloud Platform project. 
 * @param string $schemaId The ID of the schema to commit. 
 * @param string $avscFile The path to the Avro schema file. 
 */ 
 function commit_avro_schema(string $projectId, string $schemaId, string $avscFile): void 
 { 
 $client = new PubSubClient([ 
 'projectId' => $projectId 
 ]); 
 try { 
 $schema = $client->schema($schemaId); 
 $definition = file_get_contents($avscFile); 
 $info = $schema->commit($definition, 'AVRO'); 
 printf("Committed a schema using an Avro schema: %s\n", $info['name']); 
 } catch (NotFoundException $e) { 
 printf("%s does not exist.\n", $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" 
 # avsc_file = "path/to/a/avsc_file.avsc" 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  Pubsub 
 
 . 
 new 
 schemas 
  
 = 
  
 pubsub 
 . 
 schemas 
 schema 
  
 = 
  
 schemas 
 . 
 get_schema 
  
 name 
 : 
  
 pubsub 
 . 
 schema_path 
 ( 
 schema_id 
 ) 
 schema 
 . 
 definition 
  
 = 
  
 File 
 . 
 read 
  
 avsc_file 
 result 
  
 = 
  
 schemas 
 . 
 commit_schema 
  
 name 
 : 
  
 schema 
 . 
 name 
 , 
  
 schema 
 : 
  
 schema 
 puts 
  
 "Schema committed with revision 
 #{ 
 result 
 . 
 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: