Skip to content

Commit

Permalink
Add example for OCF reader and writer
Browse files Browse the repository at this point in the history
Add example for OCF reader and writer in readme
  • Loading branch information
kishaningithub authored Jun 20, 2021
1 parent 9f4e906 commit e5c707c
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,76 @@ func main() {
Also please see the example programs in the `examples` directory for
reference.

## OCF file reading and writing

This library supports reading and writing data in [Object Container File (OCF)](https://avro.apache.org/docs/current/spec.html#Object+Container+Files) format

```go
package main

import (
"bytes"
"fmt"
"strings"

"github.com/linkedin/goavro/v2"
)

func main() {
avroSchema := `
{
"type": "record",
"name": "test_schema",
"fields": [
{
"name": "time",
"type": "long"
},
{
"name": "customer",
"type": "string"
}
]
}`

// Writing OCF data
var ocfFileContents bytes.Buffer
writer, err := goavro.NewOCFWriter(goavro.OCFConfig{
W: &ocfFileContents,
Schema: avroSchema,
})
if err != nil {
fmt.Println(err)
}
err = writer.Append([]map[string]interface{}{
{
"time": 1617104831727,
"customer": "customer1",
},
{
"time": 1717104831727,
"customer": "customer2",
},
})
fmt.Println("ocfFileContents", ocfFileContents.String())

// Reading OCF data
ocfReader, err := goavro.NewOCFReader(strings.NewReader(ocfFileContents.String()))
if err != nil {
fmt.Println(err)
}
fmt.Println("Records in OCF File");
for ocfReader.Scan() {
record, err := ocfReader.Read()
if err != nil {
fmt.Println(err)
}
fmt.Println("record", record)
}
}
```
The above code in [go playground](https://play.golang.org/p/RuHQONqBXeg)

### ab2t

The `ab2t` program is similar to the reference standard
Expand Down

0 comments on commit e5c707c

Please sign in to comment.