Skip to content

Commit

Permalink
Add examples for an Primitive type array and Primitive type map.
Browse files Browse the repository at this point in the history
As someone new to Avro it was not immediately apparent how to implement these use cases.
  • Loading branch information
corsc committed Jun 8, 2016
1 parent b9868e4 commit 25ea210
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 0 deletions.
100 changes: 100 additions & 0 deletions examples/arraySimpleField/arraySimpleField.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2015 LinkedIn Corp. Licensed under the Apache License,
// Version 2.0 (the "License"); you may not use this file except in
// compliance with the License.
 You may obtain a copy of the License
// at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// 
distributed under the License is distributed on an "AS IS" BASIS,
// 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.Copyright [201X] LinkedIn Corp. Licensed under the Apache
// License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License.
 You may obtain a copy of
// the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// 
distributed under the License is distributed on an "AS IS" BASIS,
// 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.

package main

import (
"bytes"
"fmt"
"log"

"github.com/linkedin/goavro"
)

const schema = `{
"type": "record",
"name": "wordList",
"namespace": "com.example",
"doc:": "List of words",
"fields": [
{
"type": {
"items": "string",
"type": "array"
},
"name": "words"
}
]
}
`

var codec goavro.Codec

func init() {
var err error
// If you want speed, create the codec one time for each
// schema and reuse it to create multiple Writer instances.
codec, err = goavro.NewCodec(schema)
if err != nil {
log.Fatal(err)
}
}

func main() {
// Simple array's do not need to be enclosed in goavro.NewRecord
// but only passed in a slice of interface{}
innerRecords := []interface{}{
"Hello",
"World",
}

// Simply add the array directly to the record
record, err := goavro.NewRecord(goavro.RecordSchema(schema))
if err != nil {
log.Fatal(err)
}
record.Set("words", innerRecords)

// Encode the record into a bytes.Buffer
bb := new(bytes.Buffer)
if err = codec.Encode(bb, record); err != nil {
log.Fatal(err)
}

// Compare encoded bytes against the expected bytes.
actual := bb.Bytes()
expected := []byte(
"\x04" + // array of two elements
"\x0aHello" + // first element
"\x0aWorld\x00") // second element
if bytes.Compare(actual, expected) != 0 {
log.Printf("Actual: %#v; Expected: %#v", actual, expected)
}

// Let's decode the blob and print the output in JSON format
// using goavro.Record's String() method.
decoded, err := codec.Decode(bytes.NewReader(actual))
fmt.Println(decoded)
// we only need to perform type assertion if we want to access inside
result := decoded.(*goavro.Record)
fmt.Println("Record Name:", result.Name)
fmt.Println("Record Fields:")
for i, field := range result.Fields {
fmt.Println(" field", i, field.Name, ":", field.Datum)
}
}
99 changes: 99 additions & 0 deletions examples/mapSimpleField/mapSimpleField.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Copyright 2015 LinkedIn Corp. Licensed under the Apache License,
// Version 2.0 (the "License"); you may not use this file except in
// compliance with the License.
 You may obtain a copy of the License
// at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// 
distributed under the License is distributed on an "AS IS" BASIS,
// 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.Copyright [201X] LinkedIn Corp. Licensed under the Apache
// License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License.
 You may obtain a copy of
// the License at http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// 
distributed under the License is distributed on an "AS IS" BASIS,
// 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.

package main

import (
"bytes"
"fmt"
"log"

"github.com/linkedin/goavro"
)

const schema = `{
"type": "record",
"name": "wordCount",
"namespace": "com.example",
"doc:": "Count of words",
"fields": [
{
"type": {
"values": "int",
"type": "map"
},
"name": "counts"
}
]
}
`

var codec goavro.Codec

func init() {
var err error
// If you want speed, create the codec one time for each
// schema and reuse it to create multiple Writer instances.
codec, err = goavro.NewCodec(schema)
if err != nil {
log.Fatal(err)
}
}

func main() {
// Map's do not need to be enclosed in goavro.NewRecord
// but only passed in as map[string]interface{}
// According Avro specification the key must be a string but
// the values can be any valid Avro type.
// Simple types do not need to be added as goavro.Record but
// Complex types do.
// Type Reference: https://avro.apache.org/docs/current/spec.html
innerRecords := map[string]interface{}{
"Hello": int32(3),
"World": int32(66),
}

// Simply add the map directly to the record
record, err := goavro.NewRecord(goavro.RecordSchema(schema))
if err != nil {
log.Fatal(err)
}
record.Set("counts", innerRecords)

// Encode the record into a bytes.Buffer
bb := new(bytes.Buffer)
if err = codec.Encode(bb, record); err != nil {
log.Fatal(err)
}

// We cannot compare encoded bytes against the expected bytes
// as we do in other examples due to Go's nondeterministic map ordering

// Let's decode the blob and print the output in JSON format
// using goavro.Record's String() method.
actual := bb.Bytes()
decoded, err := codec.Decode(bytes.NewReader(actual))
fmt.Println(decoded)
// we only need to perform type assertion if we want to access inside
result := decoded.(*goavro.Record)
fmt.Println("Record Name:", result.Name)
fmt.Println("Record Fields:")
for i, field := range result.Fields {
fmt.Println(" field", i, field.Name, ":", field.Datum)
}
}

0 comments on commit 25ea210

Please sign in to comment.