forked from actgardner/gogen-avro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
81 lines (69 loc) · 1.96 KB
/
example.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
// This example shows serializing and deserializing records in a object container file
package main
import (
"fmt"
"io"
"os"
"github.com/fruiting/gogen-avro/v10/container"
"github.com/fruiting/gogen-avro/v10/example/avro"
)
func main() {
// Create a new DemoSchema struct
demoStruct := avro.DemoSchema{
IntField: 1,
DoubleField: 2.3,
StringField: "A string",
BoolField: true,
BytesField: []byte{1, 2, 3, 4},
}
// Open a file to write
fileWriter, err := os.Create("example_avro_container.avro")
if err != nil {
fmt.Printf("Error opening file writer: %v\n", err)
return
}
// Create a container.Writer which can write any generated Avro struct to a file
// Note that all the objects written to the file must be the same type
// Using the Null codec means blocks are uncompressed - other options are Snappy and Deflate
containerWriter, err := avro.NewDemoSchemaWriter(fileWriter, container.Null, 10)
if err != nil {
fmt.Printf("Error opening container writer: %v\n", err)
return
}
// Write the record to the container file
err = containerWriter.WriteRecord(&demoStruct)
if err != nil {
fmt.Printf("Error writing record to file: %v\n", err)
return
}
// Flush the buffers to ensure the last block has been written
err = containerWriter.Flush()
if err != nil {
fmt.Printf("Error flushing last block to file: %v\n", err)
return
}
fileWriter.Close()
// Open the container file
fileReader, err := os.Open("example_avro_container.avro")
if err != nil {
fmt.Printf("Error opening file reader: %v\n", err)
return
}
// Create a new OCF reader
ocfReader, err := avro.NewDemoSchemaReader(fileReader)
if err != nil {
fmt.Printf("Error creating OCF file reader: %v\n", err)
return
}
// Read the records back until the file is finished
for {
record, err := ocfReader.Read()
if err != nil {
if err == io.EOF {
return
}
fmt.Printf("Error reading OCF file: %v", err)
}
fmt.Printf("Read record: %v\n", record)
}
}