-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathparquet.go
167 lines (139 loc) · 4.65 KB
/
parquet.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2018-21 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package exports
import (
"context"
"fmt"
"io"
"reflect"
"runtime"
"strings"
"time"
dynamicstruct "github.com/ompluscator/dynamic-struct"
dataframe "github.com/rocketlaunchr/dataframe-go"
"github.com/xitongsys/parquet-go-source/writerfile"
"github.com/xitongsys/parquet-go/parquet"
"github.com/xitongsys/parquet-go/writer"
)
// ParquetExportOptions contains options for ExportToParquet function.
type ParquetExportOptions struct {
// Range is used to export a subset of rows from the dataframe.
Range dataframe.Range
// PageSize defaults to 8K if not set set.
//
// See: https://godoc.org/github.com/xitongsys/parquet-go/writer#ParquetWriter
PageSize *int64
// CompressionType defaults to CompressionCodec_SNAPPY if not set.
//
// See: https://godoc.org/github.com/xitongsys/parquet-go/writer#ParquetWriter
CompressionType *parquet.CompressionCodec
// Offset defaults to 4 if not set.
//
// See: https://godoc.org/github.com/xitongsys/parquet-go/writer#ParquetWriter
Offset *int64
}
// ExportToParquet exports a Dataframe as a Parquet file.
// Series names are escaped by replacing spaces with underscores and removing ",;{}()=" (excluding quotes)
// and then lower-casing for maximum cross-compatibility.
func ExportToParquet(ctx context.Context, w io.Writer, df *dataframe.DataFrame, options ...ParquetExportOptions) error {
df.Lock()
defer df.Unlock()
var (
r dataframe.Range
compressionType *parquet.CompressionCodec
offset *int64
pageSize *int64
)
if len(options) > 0 {
r = options[0].Range
compressionType = options[0].CompressionType
pageSize = options[0].PageSize
offset = options[0].Offset
}
// Create Schema
dataSchema := dynamicstruct.NewStruct()
for _, aSeries := range df.Series {
fieldName := "Z" + strings.Title(strings.ToLower(aSeries.Name())) // Make it validly exported
seriesName := santizeColumnName(aSeries.Name())
switch aSeries.(type) {
case *dataframe.SeriesFloat64:
tag := fmt.Sprintf(`parquet:"name=%s, type=DOUBLE, repetitiontype=OPTIONAL"`, seriesName)
dataSchema.AddField(fieldName, (*float64)(nil), tag)
case *dataframe.SeriesInt64:
tag := fmt.Sprintf(`parquet:"name=%s, type=INT64, repetitiontype=OPTIONAL"`, seriesName)
dataSchema.AddField(fieldName, (*int64)(nil), tag)
case *dataframe.SeriesTime:
tag := fmt.Sprintf(`parquet:"name=%s, type=TIME_MICROS, repetitiontype=OPTIONAL"`, seriesName)
dataSchema.AddField(fieldName, (*int64)(nil), tag)
case *dataframe.SeriesString:
tag := fmt.Sprintf(`parquet:"name=%s, type=UTF8, encoding=PLAIN_DICTIONARY, repetitiontype=OPTIONAL"`, seriesName)
dataSchema.AddField(fieldName, (*string)(nil), tag)
default:
tag := fmt.Sprintf(`parquet:"name=%s, type=UTF8, encoding=PLAIN_DICTIONARY, repetitiontype=OPTIONAL"`, seriesName)
dataSchema.AddField(fieldName, (*string)(nil), tag)
}
}
schemaStruct := dataSchema.Build()
fw := writerfile.NewWriterFile(w)
defer fw.Close()
pw, err := writer.NewParquetWriter(fw, schemaStruct.New(), int64(runtime.NumCPU()))
if err != nil {
return err
}
if compressionType != nil {
pw.CompressionType = *compressionType
}
if offset != nil {
pw.Offset = *offset
}
if pageSize != nil {
pw.PageSize = *pageSize
}
nRows := df.NRows(dataframe.DontLock)
if nRows > 0 {
s, e, err := r.Limits(nRows)
if err != nil {
return err
}
for row := s; row <= e; row++ {
if err := ctx.Err(); err != nil {
return err
}
rec := schemaStruct.New()
for _, aSeries := range df.Series {
fieldName := "Z" + strings.Title(strings.ToLower(aSeries.Name()))
v := reflect.ValueOf(rec).Elem().FieldByName(fieldName)
if v.IsValid() {
val := aSeries.Value(row) // returns an interface{}
if val != nil {
switch vl := val.(type) {
case float64:
v.Set(reflect.ValueOf(&vl))
case int64:
v.Set(reflect.ValueOf(&vl))
case string:
v.Set(reflect.ValueOf(&vl))
case time.Time:
t := vl.UnixNano() / 1e3 // Store as microseconds
v.Set(reflect.ValueOf(&t))
default: // interface{}
str := aSeries.ValueString(row)
v.Set(reflect.ValueOf(&str))
}
}
}
}
if err := pw.Write(rec); err != nil {
return err
}
}
}
if err := pw.WriteStop(); err != nil {
return err
}
return nil
}
// See: https://html.developreference.com/article/11087043/Spark+dataframe+column+naming+conventions+++restrictions
func santizeColumnName(s string) string {
r := strings.NewReplacer(" ", "_", ",", "", ";", "", "{", "", "}", "", "(", "", ")", "", "=", "")
return strings.ToLower(r.Replace(s))
}