v1 Publish messages of Avro schema type (DEPRECATED)

(DEPRECATED) Publish messages of Avro schema type

Code sample

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" 
  
 "cloud.google.com/go/pubsub" 
  
 "github.com/linkedin/goavro/v2" 
 ) 
 func 
  
 publishAvroRecords 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 topicID 
 , 
  
 avscFile 
  
 string 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // topicID := "my-topic" 
  
 // avscFile = "path/to/an/avro/schema/file(.avsc)/formatted/in/json" 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 pubsub 
 . 
  NewClient 
 
 ( 
 ctx 
 , 
  
 projectID 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "pubsub.NewClient: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 avroSource 
 , 
  
 err 
  
 := 
  
 os 
 . 
 ReadFile 
 ( 
 avscFile 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "os.ReadFile err: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 codec 
 , 
  
 err 
  
 := 
  
 goavro 
 . 
 NewCodec 
 ( 
 string 
 ( 
 avroSource 
 )) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "goavro.NewCodec err: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 record 
  
 := 
  
 map 
 [ 
 string 
 ] 
 interface 
 {}{ 
 "name" 
 : 
  
 "Alaska" 
 , 
  
 "post_abbr" 
 : 
  
 "AK" 
 } 
  
 // Get the topic encoding type. 
  
 t 
  
 := 
  
 client 
 . 
 Topic 
 ( 
 topicID 
 ) 
  
 cfg 
 , 
  
 err 
  
 := 
  
 t 
 . 
 Config 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "topic.Config err: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 encoding 
  
 := 
  
 cfg 
 . 
 SchemaSettings 
 . 
  Encoding 
 
  
 var 
  
 msg 
  
 [] 
 byte 
  
 switch 
  
 encoding 
  
 { 
  
 case 
  
 pubsub 
 . 
  EncodingBinary 
 
 : 
  
 msg 
 , 
  
 err 
  
 = 
  
 codec 
 . 
 BinaryFromNative 
 ( 
 nil 
 , 
  
 record 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "codec.BinaryFromNative err: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 case 
  
 pubsub 
 . 
  EncodingJSON 
 
 : 
  
 msg 
 , 
  
 err 
  
 = 
  
 codec 
 . 
 TextualFromNative 
 ( 
 nil 
 , 
  
 record 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "codec.TextualFromNative err: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 default 
 : 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "invalid encoding: %v" 
 , 
  
 encoding 
 ) 
  
 } 
  
 result 
  
 := 
  
 t 
 . 
 Publish 
 ( 
 ctx 
 , 
  
& pubsub 
 . 
 Message 
 { 
  
 Data 
 : 
  
 msg 
 , 
  
 }) 
  
 _ 
 , 
  
 err 
  
 = 
  
 result 
 . 
 Get 
 ( 
 ctx 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "result.Get: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Published avro record: %s\n" 
 , 
  
 string 
 ( 
 msg 
 )) 
  
 return 
  
 nil 
 } 
 

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" 
 # avsc_file = "path/to/an/avro/schema/file/(.avsc)/formatted/in/json" 
 pubsub 
  
 = 
  
 Google 
 :: 
 Cloud 
 :: 
  Pubsub 
 
 . 
 new 
 topic 
  
 = 
  
 pubsub 
 . 
 topic 
  
 topic_id 
 record 
  
 = 
  
 { 
  
 "name" 
  
 = 
>  
 "Alaska" 
 , 
  
 "post_abbr" 
  
 = 
>  
 "AK" 
  
 } 
 if 
  
 topic 
 . 
 message_encoding_binary? 
  
 require 
  
 "avro" 
  
 avro_schema 
  
 = 
  
 Avro 
 :: 
 Schema 
 . 
 parse 
  
 File 
 . 
 read 
 ( 
 avsc_file 
 ) 
  
 writer 
  
 = 
  
 Avro 
 :: 
 IO 
 :: 
 DatumWriter 
 . 
 new 
  
 avro_schema 
  
 buffer 
  
 = 
  
 StringIO 
 . 
 new 
  
 encoder 
  
 = 
  
 Avro 
 :: 
 IO 
 :: 
 BinaryEncoder 
 . 
 new 
  
 buffer 
  
 writer 
 . 
 write 
  
 record 
 , 
  
 encoder 
  
 topic 
 . 
 publish 
  
 buffer 
  
 puts 
  
 "Published binary-encoded AVRO message." 
 elsif 
  
 topic 
 . 
 message_encoding_json? 
  
 require 
  
 "json" 
  
 topic 
 . 
 publish 
  
 record 
 . 
 to_json 
  
 puts 
  
 "Published JSON-encoded AVRO message." 
 else 
  
 raise 
  
 "No encoding specified in 
 #{ 
 topic 
 . 
 name 
 } 
 ." 
 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: