Create Avro-typed schema

Create a schema resource using an Avro schema file.

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 
 :: 
 CreateSchemaRequest 
  
 request 
 ; 
  
 request 
 . 
 set_parent 
 ( 
 google 
 :: 
 cloud 
 :: 
 Project 
 ( 
 project_id 
 ). 
 FullName 
 ()); 
  
 request 
 . 
 set_schema_id 
 ( 
 schema_id 
 ); 
  
 request 
 . 
 mutable_schema 
 () 
 - 
> set_type 
 ( 
 google 
 :: 
 pubsub 
 :: 
 v1 
 :: 
 Schema 
 :: 
 AVRO 
 ); 
  
 request 
 . 
 mutable_schema 
 () 
 - 
> set_definition 
 ( 
 definition 
 ); 
  
 auto 
  
 schema 
  
 = 
  
 client 
 . 
 CreateSchema 
 ( 
 request 
 ); 
  
 if 
  
 ( 
 schema 
 . 
 status 
 (). 
 code 
 () 
  
 == 
  
 google 
 :: 
 cloud 
 :: 
 StatusCode 
 :: 
 kAlreadyExists 
 ) 
  
 { 
  
 std 
 :: 
 cout 
 << 
 "The schema already exists 
 \n 
 " 
 ; 
  
 return 
 ; 
  
 } 
  
 if 
  
 ( 
 ! 
 schema 
 ) 
  
 throw 
  
 std 
 :: 
 move 
 ( 
 schema 
 ). 
 status 
 (); 
  
 std 
 :: 
 cout 
 << 
 "Schema successfully created: " 
 << 
 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.Api.Gax.ResourceNames 
 
 ; 
 using 
  
  Google.Cloud.PubSub.V1 
 
 ; 
 using 
  
  Grpc.Core 
 
 ; 
 using 
  
 System 
 ; 
 using 
  
 System.IO 
 ; 
 public 
  
 class 
  
 CreateAvroSchemaSample 
 { 
  
 public 
  
 Schema 
  
 CreateAvroSchema 
 ( 
 string 
  
 projectId 
 , 
  
 string 
  
 schemaId 
 , 
  
 string 
  
 pathToDefinition 
 ) 
  
 { 
  
  SchemaServiceClient 
 
  
 schemaService 
  
 = 
  
  SchemaServiceClient 
 
 . 
  Create 
 
 (); 
  
 var 
  
 schemaName 
  
 = 
  
  SchemaName 
 
 . 
  FromProjectSchema 
 
 ( 
 projectId 
 , 
  
 schemaId 
 ); 
  
 string 
  
 schemaDefinition 
  
 = 
  
 File 
 . 
 ReadAllText 
 ( 
 pathToDefinition 
 ); 
  
  Schema 
 
  
 schema 
  
 = 
  
 new 
  
  Schema 
 
  
 { 
  
 SchemaName 
  
 = 
  
 schemaName 
 , 
  
 Type 
  
 = 
  
  Schema 
 
 . 
  Type 
 
s . 
  Type 
 
 . 
  Avro 
 
 , 
  
 Definition 
  
 = 
  
 schemaDefinition 
  
 }; 
  
  CreateSchemaRequest 
 
  
 createSchemaRequest 
  
 = 
  
 new 
  
  CreateSchemaRequest 
 
  
 { 
  
 ParentAsProjectName 
  
 = 
  
  ProjectName 
 
 . 
  FromProject 
 
 ( 
 projectId 
 ), 
  
 SchemaId 
  
 = 
  
 schemaId 
 , 
  
 Schema 
  
 = 
  
 schema 
  
 }; 
  
 try 
  
 { 
  
 schema 
  
 = 
  
 schemaService 
 . 
  CreateSchema 
 
 ( 
 createSchemaRequest 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 $"Schema {schema. Name 
} created." 
 ); 
  
 } 
  
 catch 
  
 ( 
  RpcException 
 
  
 e 
 ) 
  
 when 
  
 ( 
 e 
 . 
  Status 
 
 . 
  StatusCode 
 
  
 == 
  
  StatusCode 
 
 . 
  AlreadyExists 
 
 ) 
  
 { 
  
 Console 
 . 
 WriteLine 
 ( 
 $"Schema {schemaName} already exists." 
 ); 
  
 } 
  
 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" 
 ) 
 // createAvroSchema creates a schema resource from a JSON-formatted Avro schema file. 
 func 
  
 createAvroSchema 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 schemaID 
 , 
  
 avscFile 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // schemaID := "my-schema" 
  
 // 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 
 () 
  
 avscSource 
 , 
  
 err 
  
 := 
  
 os 
 . 
 ReadFile 
 ( 
 avscFile 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "error reading from file: %s" 
 , 
  
 avscFile 
 ) 
  
 } 
  
 req 
  
 := 
  
& pubsubpb 
 . 
 CreateSchemaRequest 
 { 
  
 Parent 
 : 
  
 fmt 
 . 
 Sprintf 
 ( 
 "projects/%s" 
 , 
  
 projectID 
 ), 
  
 Schema 
 : 
  
& pubsubpb 
 . 
 Schema 
 { 
  
 Type 
 : 
  
 pubsubpb 
 . 
 Schema_AVRO 
 , 
  
 Definition 
 : 
  
 string 
 ( 
 avscSource 
 ), 
  
 }, 
  
 SchemaId 
 : 
  
 schemaID 
 , 
  
 } 
  
 s 
 , 
  
 err 
  
 := 
  
 client 
 . 
 CreateSchema 
 ( 
 ctx 
 , 
  
 req 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "error calling CreateSchema: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Schema created: %#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. AlreadyExistsException 
 
 ; 
 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 
 CreateAvroSchemaExample 
  
 { 
  
 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" 
 ; 
  
 createAvroSchemaExample 
 ( 
 projectId 
 , 
  
 schemaId 
 , 
  
 avscFile 
 ); 
  
 } 
  
 public 
  
 static 
  
  Schema 
 
  
 createAvroSchemaExample 
 ( 
 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 
 . 
 createSchema 
 ( 
  
 projectName 
 , 
  
  Schema 
 
 . 
 newBuilder 
 () 
  
 . 
 setName 
 ( 
 schemaName 
 . 
  toString 
 
 ()) 
  
 . 
  setType 
 
 ( 
  Schema 
 
 . 
 Type 
 . 
 AVRO 
 ) 
  
 . 
  setDefinition 
 
 ( 
 avscSource 
 ) 
  
 . 
 build 
 (), 
  
 schemaId 
 ); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "Created a schema using an Avro schema:\n" 
  
 + 
  
 schema 
 ); 
  
 return 
  
 schema 
 ; 
  
 } 
  
 catch 
  
 ( 
  AlreadyExistsException 
 
  
 e 
 ) 
  
 { 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 schemaName 
  
 + 
  
 "already exists." 
 ); 
  
 return 
  
 null 
 ; 
  
 } 
  
 } 
 } 
 

Node.js

Before trying this sample, follow the Node.js setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Node.js 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 .

  /** 
  
 * 
  
 TODO 
 ( 
 developer 
 ): 
  
 Uncomment 
  
 these 
  
 variables 
  
 before 
  
 running 
  
 the 
  
 sample 
 . 
  
 */ 
 // 
  
 const 
  
 schemaNameOrId 
  
 = 
  
 'YOUR_SCHEMA_NAME_OR_ID' 
 ; 
 // 
  
 const 
  
 avscFile 
  
 = 
  
 'path/to/an/avro/schema/file/(.avsc)/formatted/in/json' 
 ; 
 // 
  
 Imports 
  
 the 
  
 Google 
  
 Cloud 
  
 client 
  
 library 
 const 
  
 { 
 PubSub 
 , 
  
 SchemaTypes 
 } 
  
 = 
  
 require 
 ( 
 '@google-cloud/pubsub' 
 ); 
 const 
  
 fs 
  
 = 
  
 require 
 ( 
 'fs' 
 ); 
 // 
  
 Creates 
  
 a 
  
 client 
 ; 
  
 cache 
  
 this 
  
 for 
  
 further 
  
 use 
 const 
  
 pubSubClient 
  
 = 
  
 new 
  
 PubSub 
 (); 
 async 
  
 function 
  
 createAvroSchema 
 ( 
 schemaNameOrId 
 , 
  
 avscFile 
 ) 
  
 { 
  
 const 
  
 definition 
  
 = 
  
 fs 
 . 
 readFileSync 
 ( 
 avscFile 
 ) 
 . 
 toString 
 (); 
  
 const 
  
 schema 
  
 = 
  
 await 
  
 pubSubClient 
 . 
 createSchema 
 ( 
  
 schemaNameOrId 
 , 
  
 SchemaTypes 
 . 
 Avro 
 , 
  
 definition 
 , 
  
 ); 
  
 const 
  
 name 
  
 = 
  
 await 
  
 schema 
 . 
 getName 
 (); 
  
 console 
 . 
 log 
 ( 
 ` 
 Schema 
  
 $ 
 { 
 name 
 } 
  
 created 
 . 
 ` 
 ); 
 } 
 

Node.js

Before trying this sample, follow the Node.js setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Node.js 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 .

  /** 
 * 
 TODO 
 ( 
 developer 
 ): 
 Uncomment 
 these 
 variables 
 before 
 running 
 the 
 sample 
 . 
 */ 
 // 
 const 
 schemaNameOrId 
 = 
 'YOUR_SCHEMA_NAME_OR_ID' 
 ; 
 // 
 const 
 avscFile 
 = 
 'path/to/an/avro/schema/file/(.avsc)/formatted/in/json' 
 ; 
 // 
 Imports 
 the 
 Google 
 Cloud 
 client 
 library 
 import 
  
 { 
 PubSub 
 , 
 SchemaTypes 
 } 
 from 
  
 '@google-cloud/pubsub' 
 ; 
 import 
  
 * 
 as 
 fs 
 from 
  
 'fs' 
 ; 
 // 
 Creates 
 a 
 client 
 ; 
 cache 
 this 
 for 
 further 
 use 
 const 
 pubSubClient 
 = 
 new 
 PubSub 
 (); 
 async 
 function 
 createAvroSchema 
 ( 
 schemaNameOrId 
 : 
 string 
 , 
 avscFile 
 : 
 string 
 ) 
 { 
 const 
 definition 
 : 
 string 
 = 
 fs 
 . 
 readFileSync 
 ( 
 avscFile 
 ) 
 . 
 toString 
 (); 
 const 
 schema 
 = 
 await 
 pubSubClient 
 . 
 createSchema 
 ( 
 schemaNameOrId 
 , 
 SchemaTypes 
 . 
 Avro 
 , 
 definition 
 , 
 ); 
 const 
 name 
 = 
 await 
 schema 
 . 
 getName 
 (); 
 console 
 . 
 log 
 ( 
 ` 
 Schema 
 $ 
 { 
 name 
 } 
 created 
 . 
 ` 
 ); 
 } 
 

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; 
 /** 
 * Create a Schema with an AVRO definition. 
 * 
 * @param string $projectId 
 * @param string $schemaId 
 * @param string $avscFile 
 */ 
 function create_avro_schema(string $projectId, string $schemaId, string $avscFile): void 
 { 
 $pubsub = new PubSubClient([ 
 'projectId' => $projectId, 
 ]); 
 $definition = (string) file_get_contents($avscFile); 
 $schema = $pubsub->createSchema($schemaId, 'AVRO', $definition); 
 printf('Schema %s created.', $schema->name()); 
 } 
 

Python

Before trying this sample, follow the Python setup instructions in the Pub/Sub quickstart using client libraries . For more information, see the Pub/Sub Python 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 .

  from 
  
 google.api_core.exceptions 
  
 import 
 AlreadyExists 
 from 
  
 google.cloud.pubsub 
  
 import 
 SchemaServiceClient 
 from 
  
 google.pubsub_v1.types 
  
 import 
 Schema 
 # TODO(developer): Replace these variables before running the sample. 
 # project_id = "your-project-id" 
 # schema_id = "your-schema-id" 
 # avsc_file = "path/to/an/avro/schema/file/(.avsc)/formatted/in/json" 
 project_path 
 = 
 f 
 "projects/ 
 { 
 project_id 
 } 
 " 
 # Read a JSON-formatted Avro schema file as a string. 
 with 
 open 
 ( 
 avsc_file 
 , 
 "rb" 
 ) 
 as 
 f 
 : 
 avsc_source 
 = 
 f 
 . 
 read 
 () 
 . 
 decode 
 ( 
 "utf-8" 
 ) 
 schema_client 
 = 
 SchemaServiceClient 
 () 
 schema_path 
 = 
 schema_client 
 . 
  schema_path 
 
 ( 
 project_id 
 , 
 schema_id 
 ) 
 schema 
 = 
 Schema 
 ( 
 name 
 = 
 schema_path 
 , 
 type_ 
 = 
 Schema 
 . 
 Type 
 . 
 AVRO 
 , 
 definition 
 = 
 avsc_source 
 ) 
 try 
 : 
 result 
 = 
 schema_client 
 . 
 create_schema 
 ( 
 request 
 = 
 { 
 "parent" 
 : 
 project_path 
 , 
 "schema" 
 : 
 schema 
 , 
 "schema_id" 
 : 
 schema_id 
 } 
 ) 
 print 
 ( 
 f 
 "Created a schema using an Avro schema file: 
 \n 
 { 
 result 
 } 
 " 
 ) 
 return 
 result 
 except 
 AlreadyExists 
 : 
 print 
 ( 
 f 
 " 
 { 
 schema_id 
 } 
 already exists." 
 ) 
 

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/an/avro/schema/file/(.avsc)/formatted/in/json" 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  PubSub 
 
 . 
  new 
 
 schemas 
  
 = 
  
 pubsub 
 . 
 schemas 
 schema 
  
 = 
  
 schemas 
 . 
 create_schema 
  
 parent 
 : 
  
 pubsub 
 . 
 project_path 
 , 
  
 schema 
 : 
  
 { 
  
 name 
 : 
  
 schema_id 
 , 
  
 type 
 : 
  
 :AVRO 
 , 
  
 definition 
 : 
  
 File 
 . 
 read 
 ( 
 avsc_file 
 ) 
  
 }, 
  
 schema_id 
 : 
  
 schema_id 
 puts 
  
 "Schema 
 #{ 
 schema 
 . 
 name 
 } 
 created." 
 

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: