Skip to content

Commit

Permalink
added an example
Browse files Browse the repository at this point in the history
  • Loading branch information
cswank committed Aug 8, 2019
1 parent ca4685f commit 913e1df
Show file tree
Hide file tree
Showing 5 changed files with 728 additions and 0 deletions.
22 changes: 22 additions & 0 deletions examples/embedded2x/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Parquetgen example

To generate the code needed to run this example:

cd cmd/parquetgen
go get ./...
go install
cd ../../examples/people
go generate

Go generate calls (see the top of main.go for the go:generate command):

//parquetgen -input main.go -type Person -package main

which produces a file called parquet.go. Now run:

go run .

You should now have a parquet file that encodes a Person struct. To
read the values back run:

go run . -read people.parquet
72 changes: 72 additions & 0 deletions examples/embedded2x/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

//go:generate parquetgen -input main.go -type C -package main

import (
"encoding/json"
"log"
"os"
)

func main() {
f, err := os.Create("parquet")
if err != nil {
log.Fatal(err)
}

defer f.Close()

w, err := NewParquetWriter(f, MaxPageSize(2))
if err != nil {
log.Fatal(err)
}

w.Add(C{B: B{Name: "a", A: A{ID: 1}}})
w.Add(C{B: B{Name: "b", A: A{ID: 2}}})
w.Add(C{B: B{Name: "c", A: A{ID: 3}}})

if err := w.Write(); err != nil {
log.Fatal(err)
}

if err := w.Close(); err != nil {
log.Fatal(err)
}

f2, err := os.Open("parquet")
if err != nil {
log.Fatal(err)
}
defer f2.Close()

r, err := NewParquetReader(f2)
if err != nil {
log.Fatal(err)
}

enc := json.NewEncoder(os.Stdout)
for r.Next() {
var c C
r.Scan(&c)
enc.Encode(c)
}

if err := r.Error(); err != nil {
log.Fatal(err)
}
}

// Being is split out only to show how embedded structs
// are handled.
type A struct {
ID int32 `parquet:"id"`
}

type B struct {
A
Name string `parquet:"name"`
}

type C struct {
B
}
Binary file added examples/embedded2x/parquet
Binary file not shown.
Loading

0 comments on commit 913e1df

Please sign in to comment.