forked from parquet-go/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
61 lines (55 loc) · 1.26 KB
/
example_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
package parquet_test
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"github.com/parquet-go/parquet-go"
)
func Example() {
// parquet-go uses the same struct-tag definition style as JSON and XML
type Contact struct {
Name string `parquet:"name"`
// "zstd" specifies the compression for this column
PhoneNumber string `parquet:"phoneNumber,optional,zstd"`
}
type AddressBook struct {
Owner string `parquet:"owner,zstd"`
OwnerPhoneNumbers []string `parquet:"ownerPhoneNumbers,gzip"`
Contacts []Contact `parquet:"contacts"`
}
f, _ := ioutil.TempFile("", "parquet-example-")
writer := parquet.NewWriter(f)
rows := []AddressBook{
{Owner: "UserA", Contacts: []Contact{
{Name: "Alice", PhoneNumber: "+15505551234"},
{Name: "Bob"},
}},
// Add more rows here.
}
for _, row := range rows {
if err := writer.Write(row); err != nil {
log.Fatal(err)
}
}
_ = writer.Close()
_ = f.Close()
// Now, we can read from the file.
rf, _ := os.Open(f.Name())
pf := parquet.NewReader(rf)
addrs := make([]AddressBook, 0)
for {
var addr AddressBook
err := pf.Read(&addr)
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
addrs = append(addrs, addr)
}
fmt.Println(addrs[0].Owner)
// Output: UserA
}