github.com/parquet-go/parquet-go@v0.21.1-0.20240501160520-b3c3a0c3ed6f/example_test.go (about)

     1  package parquet_test
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"io/ioutil"
     7  	"log"
     8  	"os"
     9  
    10  	"github.com/parquet-go/parquet-go"
    11  )
    12  
    13  func Example() {
    14  	// parquet-go uses the same struct-tag definition style as JSON and XML
    15  	type Contact struct {
    16  		Name string `parquet:"name"`
    17  		// "zstd" specifies the compression for this column
    18  		PhoneNumber string `parquet:"phoneNumber,optional,zstd"`
    19  	}
    20  
    21  	type AddressBook struct {
    22  		Owner             string    `parquet:"owner,zstd"`
    23  		OwnerPhoneNumbers []string  `parquet:"ownerPhoneNumbers,gzip"`
    24  		Contacts          []Contact `parquet:"contacts"`
    25  	}
    26  
    27  	f, _ := ioutil.TempFile("", "parquet-example-")
    28  	writer := parquet.NewWriter(f)
    29  	rows := []AddressBook{
    30  		{Owner: "UserA", Contacts: []Contact{
    31  			{Name: "Alice", PhoneNumber: "+15505551234"},
    32  			{Name: "Bob"},
    33  		}},
    34  		// Add more rows here.
    35  	}
    36  	for _, row := range rows {
    37  		if err := writer.Write(row); err != nil {
    38  			log.Fatal(err)
    39  		}
    40  	}
    41  	_ = writer.Close()
    42  	_ = f.Close()
    43  
    44  	// Now, we can read from the file.
    45  	rf, _ := os.Open(f.Name())
    46  	pf := parquet.NewReader(rf)
    47  	addrs := make([]AddressBook, 0)
    48  	for {
    49  		var addr AddressBook
    50  		err := pf.Read(&addr)
    51  		if err == io.EOF {
    52  			break
    53  		}
    54  		if err != nil {
    55  			log.Fatal(err)
    56  		}
    57  		addrs = append(addrs, addr)
    58  	}
    59  	fmt.Println(addrs[0].Owner)
    60  	// Output: UserA
    61  }