-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.go
179 lines (164 loc) · 4.22 KB
/
pipe.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// 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 (
"fmt"
"github.com/CloudWise-OpenSource/GoCrab/Libs/goavro"
"io"
"log"
"math/rand"
"os"
"os/signal"
"time"
)
const innerSchema = `
{
"type": "record",
"name": "user",
"namespace": "com.example",
"doc": "User information",
"fields": [
{
"type": "string",
"name": "account",
"doc": "The user's account name"
},
{
"type": "long",
"name": "creationDate",
"doc": "Unix epoch time"
}
]
}
`
var (
outerSchema string
codec goavro.Codec
)
func init() {
outerSchema = fmt.Sprintf(`
{
"type": "record",
"name": "comments",
"doc:": "A basic schema for storing blog comments",
"namespace": "com.example",
"fields": [
{
"name": "user",
"type": %s
},
{
"doc": "The content of the user's message",
"type": "string",
"name": "comment"
},
{
"doc": "Unix epoch time in nanoseconds",
"type": "long",
"name": "timestamp"
}
]
}
`, innerSchema)
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(outerSchema)
if err != nil {
log.Fatal(err)
}
}
func main() {
pr, pw, err := os.Pipe()
if err != nil {
log.Fatal(err)
}
go dumpWriter(pw, codec)
dumpReader(pr)
}
func dumpWriter(w io.Writer, codec goavro.Codec) {
fw, err := codec.NewWriter(
goavro.BlockSize(5), // queue up no more than 5 items
goavro.BlockTick(3*time.Second), // but flush at least every 3 seconds
goavro.Compression(goavro.CompressionDeflate),
goavro.ToWriter(w))
if err != nil {
log.Fatal(err)
}
defer fw.Close()
sigs := make(chan os.Signal)
signal.Notify(sigs)
defer func() {
signal.Stop(sigs)
}()
writeLoop:
for {
select {
case <-time.After(time.Duration(rand.Intn(500)) * time.Millisecond):
sendRecord(fw)
case <-sigs:
break writeLoop
}
}
}
func sendRecord(fw *goavro.Writer) {
// If we want to encode data, we need to put it in an actual
// goavro.Record instance corresponding to the schema we wish
// to encode against.
//
// NewRecord will create a goavro.Record instance
// corresponding to the specified schema.
innerRecord, err := goavro.NewRecord(goavro.RecordSchema(innerSchema))
if err != nil {
log.Fatal(err)
}
innerRecord.Set("account", "Aquaman")
innerRecord.Set("creationDate", int64(1082196484))
// We create both an innerRecord and an outerRecord.
outerRecord, err := goavro.NewRecord(goavro.RecordSchema(outerSchema))
if err != nil {
log.Fatal(err)
}
// innerRecord is a completely seperate record instance from
// outerRecord. Once we have an innerRecord instance it can be
// assigned to the appropriate Datum item of the outerRecord.
outerRecord.Set("user", innerRecord)
// Other fields are set on the outerRecord.
outerRecord.Set("comment", "The Atlantic is oddly cold this morning!")
outerRecord.Set("timestamp", int64(time.Now().UnixNano()))
fw.Write(outerRecord)
}
func dumpReader(r io.Reader) {
fr, err := goavro.NewReader(goavro.FromReader(r))
if err != nil {
log.Fatal(err)
}
defer func() {
if err := fr.Close(); err != nil {
log.Fatal(err)
}
}()
for fr.Scan() {
datum, err := fr.Read()
if err != nil {
log.Println(err)
continue
}
fmt.Println(datum)
}
}