-
Notifications
You must be signed in to change notification settings - Fork 22
/
examples_test.go
92 lines (75 loc) · 1.97 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package avro_test
import (
"bytes"
"fmt"
"log"
avro "gopkg.in/avro.v0"
)
var someSchema avro.Schema
type SomeStruct struct {
Name string
}
func ExampleSpecificDatumWriter_basic() {
writer := avro.NewSpecificDatumWriter()
writer.SetSchema(someSchema)
var buf bytes.Buffer
v := &SomeStruct{Name: "abc"}
if err := writer.Write(v, avro.NewBinaryEncoder(&buf)); err != nil {
log.Fatal(err) // i/o errors OR encoding errors
}
// buf.Bytes() now contains the encoded value of 'v'
}
func ExampleSpecificDatumWriter_full() {
// Parse a schema from JSON to get the schema object.
schema, err := avro.ParseSchema(`{
"type": "record",
"name": "Person",
"fields": [
{"name": "first_name", "type": "string"},
{"name": "age", "type": "int"}
]
}`)
if err != nil {
log.Fatal(err)
}
// This is a struct which supports the above schema.
type Person struct {
FirstName string `avro:"first_name"` // the avro: tag specifies which avro field matches this field.
Age int32 `avro:"age"`
}
// Create a SpecificDatumWriter, which you can re-use multiple times.
writer := avro.NewSpecificDatumWriter()
writer.SetSchema(schema)
// Write a person to a byte buffer as avro.
person := &Person{
FirstName: "Bob",
Age: 48,
}
var buf bytes.Buffer
encoder := avro.NewBinaryEncoder(&buf)
err = writer.Write(person, encoder)
if err != nil {
log.Fatal(err)
}
fmt.Println(buf.Bytes())
// Output: [6 66 111 98 96]
}
func ExampleDataFileReader() {
// Create a reader open for reading on a data file.
reader, err := avro.NewDataFileReader("filename.avro")
if err != nil {
log.Fatal(err)
}
defer reader.Close()
for reader.HasNext() {
var dest SomeStruct // or a *avro.GenericRecord
if err := reader.Next(&dest); err != nil {
// Error specific to decoding a single record
}
log.Printf("Decoded record %v", dest)
}
// If there was any error that stopped the reader loop, this is how we know
if err := reader.Err(); err != nil {
log.Fatal(err)
}
}