-
Notifications
You must be signed in to change notification settings - Fork 1
/
parquet_writer.go
75 lines (64 loc) · 1.77 KB
/
parquet_writer.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
package exports
import (
"fmt"
"log"
"os"
"path/filepath"
"sync"
"github.com/innosat-mats/rac-extract-payload/internal/common"
"github.com/innosat-mats/rac-extract-payload/internal/timeseries"
)
func parquetName(dir string, pkg *common.DataRecord, stream timeseries.OutStream) string {
name := timeseries.ParquetName(pkg, stream)
return filepath.Join(dir, name)
}
func parquetFileWriterFactoryCreator(
dir string,
) timeseries.ParquetFactory {
return func(pkg *common.DataRecord, stream timeseries.OutStream) (timeseries.ParquetWriter, error) {
outPath := parquetName(dir, pkg, stream)
err := os.MkdirAll(filepath.Dir(outPath), os.ModePerm)
if err != nil {
return nil, fmt.Errorf("could not create output prefix '%v'", outPath)
}
return timeseries.NewParquet(outPath, pkg), nil
}
}
// DiskCallbackFactory returns a callback for disk writes
func ParquetCallbackFactory(
output string,
wg *sync.WaitGroup,
) (common.Callback, common.CallbackTeardown) {
var err error
timeseriesCollection := timeseries.NewParquetCollection(parquetFileWriterFactoryCreator(output))
errorStats := common.NewErrorStats()
// Create Directory and File
err = os.MkdirAll(output, os.ModePerm)
if err != nil {
log.Printf("Could not create output directory '%v'", output)
}
callback := func(pkg common.DataRecord) {
errorStats.Register(pkg.Error)
if pkg.Error != nil {
pkg.Error = fmt.Errorf(
"%s %s",
pkg.Error,
common.MakePackageInfo(&pkg),
)
log.Println(pkg.Error)
}
if pkg.Data != nil {
// Write to the dedicated target stream
err = timeseriesCollection.Write(&pkg)
if err != nil {
log.Println(err)
}
}
}
teardown := func() {
timeseriesCollection.CloseAll()
wg.Wait()
log.Println(errorStats.Summarize())
}
return callback, teardown
}