-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3_writer.go
More file actions
71 lines (68 loc) · 1.61 KB
/
s3_writer.go
File metadata and controls
71 lines (68 loc) · 1.61 KB
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
package iceberg
import (
"context"
"fmt"
"github.com/apache/arrow-go/v18/parquet/pqarrow"
"github.com/apache/iceberg-go/io"
"github.com/apache/iceberg-go/table"
"github.com/google/uuid"
"github.com/transferia/transferia/library/go/core/xerrors"
"github.com/transferia/transferia/pkg/abstract"
)
func fileName(prefix string, wNum, iNum int, tbl *table.Table) string {
return fmt.Sprintf(
"%s/%s/%s/data/%05d-%d-%s-%d-%05d.parquet",
prefix,
tbl.Identifier()[0],
tbl.Identifier()[1],
iNum/10,
iNum%10,
uuid.New().String(),
wNum/10000,
wNum%10000,
)
}
func writeFile(fName string, tbl *table.Table, items []abstract.ChangeItem) error {
if len(items) == 0 {
return nil
}
fs, err := tbl.FS(context.Background())
if err != nil {
return xerrors.Errorf("get filesystem: %w", err)
}
fileIO, ok := fs.(io.WriteFileIO)
if !ok {
return xerrors.Errorf("%T does not implement io.WriteFileIO", fs)
}
fw, err := fileIO.Create(fName)
if err != nil {
return xerrors.Errorf("create file writer: %w", err)
}
arrSchema, err := table.SchemaToArrowSchema(
tbl.Schema(),
map[string]string{},
false,
false,
)
if err != nil {
return xerrors.Errorf("convert to ArrowSchema: %w", err)
}
pw, err := pqarrow.NewFileWriter(
arrSchema,
fw,
nil,
pqarrow.DefaultWriterProps(),
)
if err != nil {
return xerrors.Errorf("create array writer: %w", err)
}
record := ToArrowRows(items, arrSchema)
defer record.Release()
if err := pw.Write(record); err != nil {
return xerrors.Errorf("write rows: %w", err)
}
if err := pw.Close(); err != nil {
return xerrors.Errorf("close writer: %w", err)
}
return nil
}