This repository was archived by the owner on Oct 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathDLPS3ScannerPipeline.java
More file actions
100 lines (88 loc) · 3.78 KB
/
Copy pathDLPS3ScannerPipeline.java
File metadata and controls
100 lines (88 loc) · 3.78 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
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
/*
* Copyright 2020 Google LLC
*
* Licensed 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.swarm.tokenization;
import com.google.swarm.tokenization.common.AuditInspectDataTransform;
import com.google.swarm.tokenization.common.BQWriteTransform;
import com.google.swarm.tokenization.common.DLPTransform;
import com.google.swarm.tokenization.common.FileReaderTransform;
import com.google.swarm.tokenization.common.RowToJson;
import com.google.swarm.tokenization.common.S3ReaderOptions;
import com.google.swarm.tokenization.common.Util;
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.PipelineResult;
import org.apache.beam.sdk.io.gcp.bigquery.BigQueryIO;
import org.apache.beam.sdk.io.gcp.pubsub.PubsubIO;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.values.KV;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.PCollectionTuple;
import org.apache.beam.sdk.values.Row;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DLPS3ScannerPipeline {
public static final Logger LOG = LoggerFactory.getLogger(DLPS3ScannerPipeline.class);
public static void main(String[] args) {
S3ReaderOptions options =
PipelineOptionsFactory.fromArgs(args).withValidation().as(S3ReaderOptions.class);
run(options);
}
public static PipelineResult run(S3ReaderOptions options) {
Pipeline p = Pipeline.create(options);
PCollectionTuple nonInspectedContents =
p.apply(
"File Read Transform",
FileReaderTransform.newBuilder()
.setSubscriber(options.getSubscriber())
.setBatchSize(options.getBatchSize())
.build());
PCollectionTuple inspectedData =
nonInspectedContents
.get(Util.readRowSuccess)
.apply(
"DLPScanner",
DLPTransform.newBuilder()
.setInspectTemplateName(options.getInspectTemplateName())
.setProjectId(options.getProject())
.build());
PCollection<Row> inspectedContents =
inspectedData.get(Util.inspectData).setRowSchema(Util.bqDataSchema);
PCollection<Row> inspectedStats =
inspectedData.get(Util.auditData).setRowSchema(Util.bqAuditSchema);
PCollection<Row> auditData =
inspectedStats
.apply("FileTrackerTransform", new AuditInspectDataTransform())
.setRowSchema(Util.bqAuditSchema);
auditData.apply(
"WriteAuditData",
BigQueryIO.<Row>write()
.to(options.getAuditTableSpec())
.withMethod(BigQueryIO.Write.Method.STREAMING_INSERTS)
.useBeamSchema()
.withoutValidation()
.withWriteDisposition(BigQueryIO.Write.WriteDisposition.WRITE_APPEND)
.withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_NEVER));
auditData
.apply("RowToJson", new RowToJson())
.apply("WriteToTopic", PubsubIO.writeStrings().to(options.getTopic()));
inspectedContents.apply(
"WriteInspectData",
BQWriteTransform.newBuilder()
.setTableSpec(options.getTableSpec())
.setMethod(options.getWriteMethod())
.build());
return p.run();
}
}