/
write.go
130 lines (113 loc) · 3.32 KB
/
write.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
package client
import (
"context"
"encoding/json"
"io"
"regexp"
"time"
"github.com/apache/arrow/go/v15/arrow"
"github.com/apache/arrow/go/v15/arrow/array"
"github.com/apache/arrow/go/v15/arrow/memory"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/cloudquery/filetypes/v4"
"github.com/cloudquery/plugin-sdk/v4/message"
"github.com/cloudquery/plugin-sdk/v4/types"
"github.com/google/uuid"
)
var reInvalidJSONKey = regexp.MustCompile(`\W`)
func (c *Client) WriteTable(ctx context.Context, msgs <-chan *message.WriteInsert) error {
var s *filetypes.Stream
for msg := range msgs {
if s == nil {
table := msg.GetTable()
objKey := c.spec.ReplacePathVariables(table.Name, uuid.NewString(), time.Now().UTC(), c.syncID)
var err error
s, err = c.Client.StartStream(table, func(r io.Reader) error {
params := &s3.PutObjectInput{
Bucket: aws.String(c.spec.Bucket),
Key: aws.String(objKey),
Body: r,
ContentType: aws.String(c.spec.GetContentType()),
}
sseConfiguration := c.spec.ServerSideEncryptionConfiguration
if sseConfiguration != nil {
params.SSEKMSKeyId = &sseConfiguration.SSEKMSKeyId
params.ServerSideEncryption = sseConfiguration.ServerSideEncryption
}
_, err := c.uploader.Upload(ctx, params)
return err
})
if err != nil {
return err
}
}
if c.spec.Athena {
var err error
msg.Record, err = sanitizeRecordJSONKeys(msg.Record)
if err != nil {
_ = s.FinishWithError(err)
return err
}
}
if err := s.Write([]arrow.Record{msg.Record}); err != nil {
_ = s.FinishWithError(err)
return err
}
}
return s.Finish()
}
func (c *Client) Write(ctx context.Context, msgs <-chan message.WriteMessage) error {
return c.writer.Write(ctx, msgs)
}
// sanitizeRecordJSONKeys replaces all invalid characters in JSON keys with underscores. This is required
// for compatibility with Athena.
func sanitizeRecordJSONKeys(record arrow.Record) (arrow.Record, error) {
cols := make([]arrow.Array, record.NumCols())
for i, col := range record.Columns() {
if arrow.TypeEqual(col.DataType(), types.NewJSONType()) {
b := types.NewJSONBuilder(array.NewExtensionBuilder(memory.DefaultAllocator, types.NewJSONType()))
for r := 0; r < int(record.NumRows()); r++ {
if col.IsNull(r) {
b.AppendNull()
continue
}
data, err := sanitizeJSONRawMessage(col.GetOneForMarshal(r).(json.RawMessage))
if err != nil {
return nil, err
}
b.Append(data)
}
cols[i] = b.NewArray()
continue
}
cols[i] = col
}
return array.NewRecord(record.Schema(), cols, record.NumRows()), nil
}
func sanitizeJSONRawMessage(rawMessage json.RawMessage) (any, error) {
var data any
err := json.Unmarshal(rawMessage, &data)
if err != nil {
return nil, err
}
return sanitizeJSONKeysForObject(data), nil
}
func sanitizeJSONKeysForObject(data any) any {
// we only care about objects that have keys: present either while nesting or in arrays
switch data := data.(type) {
case map[string]any:
res := make(map[string]any, len(data))
for k, v := range data {
res[reInvalidJSONKey.ReplaceAllString(k, "_")] = sanitizeJSONKeysForObject(v)
}
return res
case []any:
for i, el := range data {
data[i] = sanitizeJSONKeysForObject(el)
}
return data
default:
return data
}
}