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

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"reflect"
     6  
     7  	goparquet "github.com/fraugster/parquet-go"
     8  	"github.com/fraugster/parquet-go/floor"
     9  	"github.com/fraugster/parquet-go/parquet"
    10  	"github.com/fraugster/parquet-go/parquetschema"
    11  )
    12  
    13  func main() {
    14  	schemaDef, err := parquetschema.ParseSchemaDefinition(
    15  		`message test {
    16  			required binary name (STRING);
    17  			required binary data;
    18  			required double score;
    19  		}`)
    20  	if err != nil {
    21  		log.Fatalf("Parsing schema definition failed: %v", err)
    22  	}
    23  
    24  	parquetFilename := "output.parquet"
    25  
    26  	fw, err := floor.NewFileWriter(parquetFilename,
    27  		goparquet.WithSchemaDefinition(schemaDef),
    28  		goparquet.WithCompressionCodec(parquet.CompressionCodec_SNAPPY),
    29  	)
    30  	if err != nil {
    31  		log.Fatalf("Opening parquet file for writing failed: %v", err)
    32  	}
    33  
    34  	type record struct {
    35  		Name  string  `parquet:"name"`
    36  		Data  []byte  `parquet:"data"`
    37  		Score float64 `parquet:"score"`
    38  	}
    39  
    40  	input := []record{
    41  		{
    42  			Name:  "Test",
    43  			Data:  []byte{0xFF, 0x0A, 0x8E, 0x00, 0x12},
    44  			Score: 23.5,
    45  		},
    46  	}
    47  
    48  	for _, rec := range input {
    49  		if err := fw.Write(rec); err != nil {
    50  			log.Fatalf("Writing record failed: %v", err)
    51  		}
    52  	}
    53  
    54  	if err := fw.Close(); err != nil {
    55  		log.Fatalf("Closing parquet writer failed: %v", err)
    56  	}
    57  
    58  	fr, err := floor.NewFileReader(parquetFilename)
    59  	if err != nil {
    60  		log.Fatalf("Opening parquet file failed: %v", err)
    61  	}
    62  
    63  	var fileContent []record
    64  
    65  	for fr.Next() {
    66  		var rec record
    67  		if err := fr.Scan(&rec); err != nil {
    68  			log.Fatalf("Scanning record failed: %v", err)
    69  		}
    70  		fileContent = append(fileContent, rec)
    71  	}
    72  
    73  	equal := reflect.DeepEqual(input, fileContent)
    74  	if equal {
    75  		log.Printf("Congratulations! The input and the data read back are identical!")
    76  	} else {
    77  		log.Printf("This is strange... the data read back does not back what has been written to the file.")
    78  	}
    79  }