-
Notifications
You must be signed in to change notification settings - Fork 58
/
ValidateAndCorrectCSEvt.java
142 lines (121 loc) · 5.81 KB
/
ValidateAndCorrectCSEvt.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.dataflow.sample.retail.businesslogic.core.transforms.clickstream;
import com.google.common.collect.ImmutableList;
import com.google.dataflow.sample.retail.businesslogic.core.transforms.DeadLetterSink;
import com.google.dataflow.sample.retail.businesslogic.core.transforms.DeadLetterSink.SinkType;
import com.google.dataflow.sample.retail.businesslogic.core.transforms.ErrorMsg;
import com.google.dataflow.sample.retail.businesslogic.core.transforms.clickstream.validation.EventDateTimeCorrectionService;
import com.google.dataflow.sample.retail.businesslogic.core.transforms.clickstream.validation.EventItemCorrectionService;
import com.google.dataflow.sample.retail.businesslogic.core.transforms.clickstream.validation.ValidateEventDateTime;
import com.google.dataflow.sample.retail.businesslogic.core.transforms.clickstream.validation.ValidateEventItems;
import com.google.dataflow.sample.retail.businesslogic.core.transforms.clickstream.validation.ValidationUtils;
import javax.annotation.Nullable;
import org.apache.beam.sdk.annotations.Experimental;
import org.apache.beam.sdk.schemas.Schema;
import org.apache.beam.sdk.transforms.Flatten;
import org.apache.beam.sdk.transforms.MapElements;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.PCollectionList;
import org.apache.beam.sdk.values.PCollectionTuple;
import org.apache.beam.sdk.values.Row;
import org.apache.beam.sdk.values.TupleTag;
import org.apache.beam.sdk.values.TupleTagList;
import org.apache.beam.sdk.values.TypeDescriptors;
/**
* Clean clickstream:
*
* <p>Check if uid is null, check that there is a session-id, if both are missing send to dead
* letter queue.
*
* <p>Check if lat / long are missing, if they are look up user in the user table.
*/
@Experimental
public class ValidateAndCorrectCSEvt extends PTransform<PCollection<Row>, PCollection<Row>> {
public static final TupleTag<Row> MAIN = new TupleTag<Row>() {
};
public static final TupleTag<ErrorMsg> DEAD_LETTER = new TupleTag<ErrorMsg>() {
};
public static final TupleTag<Row> NEEDS_CORRECTIONS = new TupleTag<Row>() {
};
private final DeadLetterSink.SinkType sinkType;
public ValidateAndCorrectCSEvt(SinkType sinkType) {
this.sinkType = sinkType;
}
public ValidateAndCorrectCSEvt(@Nullable String name, SinkType sinkType) {
super(name);
this.sinkType = sinkType;
}
@Override
public PCollection<Row> expand(PCollection<Row> input) {
// Chain the validation steps
Schema wrapperSchema = ValidationUtils.getValidationWrapper(input.getSchema());
// First we wrap the object
PCollection<Row> wrappedInput =
input
.apply(
"AddWrapper",
MapElements.into(TypeDescriptors.rows())
.via(x -> Row.withSchema(wrapperSchema).withFieldValue("data", x).build()))
.setRowSchema(wrapperSchema);
// Next we chain the object through the validation transforms
PCollectionTuple validateEventItems =
wrappedInput.apply(
"ValidateEventDateTime",
ParDo.of(new ValidateEventItems())
.withOutputTags(MAIN, TupleTagList.of(ImmutableList.of(DEAD_LETTER))));
PCollection<Row> validateItems =
validateEventItems
.get(MAIN)
.setRowSchema(wrapperSchema)
.apply("ValidateEventItems", ParDo.of(new ValidateEventDateTime()))
.setRowSchema(wrapperSchema);
// DeadLetter issues are not fixable
PCollectionList.of(validateEventItems.get(DEAD_LETTER))
.apply("FlattenDeadLetter", Flatten.pCollections())
.apply("OutputDeadLetterEvents", DeadLetterSink.createSink(sinkType));
// Next we chain the items through the correction transforms, if they have errors
PCollectionTuple validatedCollections =
validateItems.apply(
"SplitEventsToBeCorrected",
ParDo.of(new ValidationUtils.ValidationRouter())
.withOutputTags(MAIN, TupleTagList.of(ImmutableList.of(NEEDS_CORRECTIONS))));
// Fix bad Items
PCollection<Row> eventItemsFixed =
validatedCollections
.get(NEEDS_CORRECTIONS)
.setRowSchema(wrapperSchema)
.apply("CorrectEventItems", ParDo.of(new EventItemCorrectionService()))
.setRowSchema(wrapperSchema);
// Fix Timestamp
PCollection<Row> eventDateTimeFixed =
eventItemsFixed
.apply("CorrectEventDateTime", ParDo.of(new EventDateTimeCorrectionService()))
.setRowSchema(wrapperSchema);
PCollection<Row> extractCleanedRows =
PCollectionList.of(validatedCollections.get(MAIN).setRowSchema(wrapperSchema))
.and(eventDateTimeFixed)
.apply("CombineValidAndCorrectedEvents", Flatten.pCollections())
.apply("RestoreOriginalSchema",
MapElements.into(TypeDescriptors.rows()).via(x -> x.getRow("data")))
.setRowSchema(input.getSchema());
return extractCleanedRows;
}
}