-
Notifications
You must be signed in to change notification settings - Fork 5
/
job.go
340 lines (311 loc) · 9.39 KB
/
job.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package redshiftloader
import (
"fmt"
"github.com/practo/klog/v2"
"github.com/practo/tipoca-stream/pkg/serializer"
"strings"
)
var JobAvroSchema string = `{
"type": "record",
"name": "redshiftloader",
"fields": [
{"name": "upstreamTopic", "type": "string"},
{"name": "startOffset", "type": "long"},
{"name": "endOffset", "type": "long"},
{"name": "csvDialect", "type": "string"},
{"name": "s3Path", "type": "string"},
{"name": "schemaId", "type": "int"},
{"name": "schemaIdKey", "type": "int", "default": -1},
{"name": "maskSchema", "type": "string"},
{"name": "extraMaskSchema", "type": "string", "default": ""},
{"name": "skipMerge", "type": "string", "default": ""},
{"name": "batchBytes", "type": "long", "default": 0},
{"name": "createEvents", "type": "long", "default": 0},
{"name": "updateEvents", "type": "long", "default": 0},
{"name": "deleteEvents", "type": "long", "default": 0}
]
}`
type Job struct {
UpstreamTopic string `json:"upstreamTopic"` // batcher topic
StartOffset int64 `json:"startOffset"`
EndOffset int64 `json:"endOffset"`
CsvDialect string `json:"csvDialect"`
S3Path string `json:"s3Path"`
SchemaId int `json:"schemaId"` // schema id of debezium event for the value for upstream topic (batcher topic)
SchemaIdKey int `json:"schemaIdKey"` // schema id of debezium event for the key for upstream topic (batcher topic)
MaskSchema map[string]serializer.MaskInfo `json:"maskSchema"`
ExtraMaskSchema map[string]serializer.ExtraMaskInfo `json:"extraMaskSchema"`
SkipMerge bool `json:"skipMerge"` // deprecated in favour of createEvents, updateEvents and deleteEvents
BatchBytes int64 `json:"batchBytes"` // batch bytes store sum of all message bytes in this batch
CreateEvents int64 `json:"createEvents"` // stores count of create events
UpdateEvents int64 `json:"updateEvents"` // stores count of update events
DeleteEvents int64 `json:"deleteEvents"` // stores count of delete events
}
func NewJob(
upstreamTopic string, startOffset int64, endOffset int64,
csvDialect string, s3Path string, schemaId int, schemaIdKey int,
maskSchema map[string]serializer.MaskInfo,
extraMaskSchema map[string]serializer.ExtraMaskInfo,
skipMerge bool,
batchBytes, createEvents, updateEvents, deleteEvents int64) Job {
return Job{
UpstreamTopic: upstreamTopic,
StartOffset: startOffset,
EndOffset: endOffset,
CsvDialect: csvDialect,
S3Path: s3Path,
SchemaId: schemaId,
SchemaIdKey: schemaIdKey,
MaskSchema: maskSchema,
ExtraMaskSchema: extraMaskSchema,
SkipMerge: skipMerge, // deprecated
BatchBytes: batchBytes,
CreateEvents: createEvents,
UpdateEvents: updateEvents,
DeleteEvents: deleteEvents,
}
}
// StringMapToUser returns a User from a map representation of the User.
func StringMapToJob(data map[string]interface{}) Job {
job := Job{}
for k, v := range data {
switch k {
case "upstreamTopic":
if value, ok := v.(string); ok {
job.UpstreamTopic = value
}
case "startOffset":
if value, ok := v.(int64); ok {
job.StartOffset = value
}
case "endOffset":
if value, ok := v.(int64); ok {
job.EndOffset = value
}
case "csvDialect":
if value, ok := v.(string); ok {
job.CsvDialect = value
}
case "s3Path":
if value, ok := v.(string); ok {
job.S3Path = value
}
case "schemaId":
if value, ok := v.(int32); ok {
job.SchemaId = int(value)
} else if value, ok := v.(int); ok {
job.SchemaId = value
}
case "schemaIdKey":
if value, ok := v.(int32); ok {
job.SchemaIdKey = int(value)
} else if value, ok := v.(int); ok {
job.SchemaIdKey = value
} else {
job.SchemaIdKey = -1 // backward compatibility
}
case "skipMerge":
if value, ok := v.(string); ok {
if value == "true" {
job.SkipMerge = true
} else {
job.SkipMerge = false
}
}
case "maskSchema":
schema := make(map[string]serializer.MaskInfo)
if value, ok := v.(string); ok {
schema = ToMaskSchemaMap(value)
}
job.MaskSchema = schema
case "extraMaskSchema":
extraSchema := make(map[string]serializer.ExtraMaskInfo)
if value, ok := v.(string); ok {
extraSchema = ToExtraMaskSchemaMap(value)
}
job.ExtraMaskSchema = extraSchema
case "batchBytes":
if value, ok := v.(int64); ok {
job.BatchBytes = value
} else { // backward compatibility
job.BatchBytes = 0
}
case "createEvents":
if value, ok := v.(int64); ok {
job.CreateEvents = value
} else { // backward compatibility
job.CreateEvents = -1
}
case "updateEvents":
if value, ok := v.(int64); ok {
job.UpdateEvents = value
} else { // backward compatibility
job.UpdateEvents = -1
}
case "deleteEvents":
if value, ok := v.(int64); ok {
job.DeleteEvents = value
} else { // backward compatibility
job.DeleteEvents = -1
}
}
}
// backward compatibility
if job.SchemaIdKey == 0 {
job.SchemaIdKey = -1
}
return job
}
// TODO: hack, to release fast, found unwanted complications in
// using map[string]interface in goavro(will revisit)
func ToMaskSchemaMap(r string) map[string]serializer.MaskInfo {
m := make(map[string]serializer.MaskInfo)
columns := strings.Split(r, "|")
if len(columns) == 0 {
return m
}
for _, col := range columns {
if col == "" {
continue
}
info := strings.Split(col, ",")
name := info[0]
var masked, sortCol, distCol, lengthCol, mobileCol, mappingPIICol, conditionalNonPIICol, dependentNonPIICol, regexPatternBooleanCol bool
if info[1] == "true" {
masked = true
}
if info[2] == "true" {
sortCol = true
}
if info[3] == "true" {
distCol = true
}
if info[4] == "true" {
lengthCol = true
}
if info[5] == "true" {
mobileCol = true
}
if len(info) >= 7 {
if info[6] == "true" {
mappingPIICol = true
}
}
if len(info) >= 8 {
if info[7] == "true" {
conditionalNonPIICol = true
}
}
if len(info) >= 9 {
if info[8] == "true" {
dependentNonPIICol = true
}
}
if len(info) >= 10 {
if info[9] == "true" {
regexPatternBooleanCol = true
}
}
m[name] = serializer.MaskInfo{
Masked: masked,
SortCol: sortCol,
DistCol: distCol,
LengthCol: lengthCol,
MobileCol: mobileCol,
MappingPIICol: mappingPIICol,
ConditionalNonPIICol: conditionalNonPIICol,
DependentNonPIICol: dependentNonPIICol,
RegexPatternBooleanCol: regexPatternBooleanCol,
}
}
return m
}
// TODO: hack, to release fast, found unwanted complications in
// using map[string]interface in goavro (may revisit if required)
func ToMaskSchemaString(m map[string]serializer.MaskInfo) string {
var r string
for name, info := range m {
col := fmt.Sprintf(
"%s,%t,%t,%t,%t,%t,%t,%t,%t,%t",
name,
info.Masked,
info.SortCol,
info.DistCol,
info.LengthCol,
info.MobileCol,
info.MappingPIICol,
info.ConditionalNonPIICol,
info.DependentNonPIICol,
info.RegexPatternBooleanCol,
)
r = r + col + "|"
}
return r
}
// TODO: hack, to release fast, found unwanted complications in
// using map[string]interface in goavro (may revisit if required)
func ToExtraMaskSchemaString(m map[string]serializer.ExtraMaskInfo) string {
var r string
for name, info := range m {
col := fmt.Sprintf(
"%s,%t,%s,%s",
name,
info.Masked,
info.ColumnType,
info.DefaultVal,
)
r = r + col + "|"
}
return r
}
// TODO: hack, to release fast, found unwanted complications in
// using map[string]interface in goavro(will revisit)
func ToExtraMaskSchemaMap(r string) map[string]serializer.ExtraMaskInfo {
m := make(map[string]serializer.ExtraMaskInfo)
columns := strings.Split(r, "|")
if len(columns) == 0 {
return m
}
for _, col := range columns {
if col == "" {
continue
}
info := strings.Split(col, ",")
if len(info) != 4 {
klog.Fatalf("expecting extra mask schema to be length 3, got:%+v, schema:%v", len(info), r)
}
var masked bool
if info[1] == "true" {
masked = true
}
m[info[0]] = serializer.ExtraMaskInfo{
Masked: masked,
ColumnType: info[2],
DefaultVal: info[3],
}
}
return m
}
// ToStringMap returns a map representation of the Job
func (c Job) ToStringMap() map[string]interface{} {
skipMerge := "false" // deprecated not used anymore, backward compatibility
if c.SkipMerge {
skipMerge = "true"
}
return map[string]interface{}{
"upstreamTopic": c.UpstreamTopic,
"startOffset": c.StartOffset,
"endOffset": c.EndOffset,
"csvDialect": c.CsvDialect,
"s3Path": c.S3Path,
"schemaId": c.SchemaId,
"schemaIdKey": c.SchemaIdKey,
"skipMerge": skipMerge,
"maskSchema": ToMaskSchemaString(c.MaskSchema),
"extraMaskSchema": ToExtraMaskSchemaString(c.ExtraMaskSchema),
"batchBytes": c.BatchBytes,
"createEvents": c.CreateEvents,
"updateEvents": c.UpdateEvents,
"deleteEvents": c.DeleteEvents,
}
}