github.com/fraugster/parquet-go@v0.12.0/examples/write-low-level/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"os"
     6  
     7  	goparquet "github.com/fraugster/parquet-go"
     8  	"github.com/fraugster/parquet-go/parquet"
     9  	"github.com/fraugster/parquet-go/parquetschema"
    10  )
    11  
    12  func main() {
    13  	f, err := os.OpenFile("output.parquet", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
    14  	if err != nil {
    15  		log.Fatalf("Opening output file failed: %v", err)
    16  	}
    17  	defer f.Close()
    18  
    19  	schemaDef, err := parquetschema.ParseSchemaDefinition(
    20  		`message test {
    21  			required int64 id;
    22  			required binary city (STRING);
    23  			optional int64 population;
    24  		}`)
    25  	if err != nil {
    26  		log.Fatalf("Parsing schema definition failed: %v", err)
    27  	}
    28  
    29  	fw := goparquet.NewFileWriter(f,
    30  		goparquet.WithCompressionCodec(parquet.CompressionCodec_SNAPPY),
    31  		goparquet.WithSchemaDefinition(schemaDef),
    32  		goparquet.WithCreator("write-lowlevel"),
    33  	)
    34  
    35  	inputData := []struct {
    36  		ID   int
    37  		City string
    38  		Pop  int
    39  	}{
    40  		{ID: 1, City: "Berlin", Pop: 3520031},
    41  		{ID: 2, City: "Hamburg", Pop: 1787408},
    42  		{ID: 3, City: "Munich", Pop: 1450381},
    43  		{ID: 4, City: "Cologne", Pop: 1060582},
    44  		{ID: 5, City: "Frankfurt", Pop: 732688},
    45  	}
    46  
    47  	for _, input := range inputData {
    48  		if err := fw.AddData(map[string]interface{}{
    49  			"id":         int64(input.ID),
    50  			"city":       []byte(input.City),
    51  			"population": int64(input.Pop),
    52  		}); err != nil {
    53  			log.Fatalf("Failed to add input %v to parquet file: %v", input, err)
    54  		}
    55  	}
    56  
    57  	if err := fw.Close(); err != nil {
    58  		log.Fatalf("Closing parquet file writer failed: %v", err)
    59  	}
    60  }