1515 */
1616package com .google .swarm .tokenization .common ;
1717
18+ import com .google .api .gax .retrying .RetrySettings ;
1819import com .google .auto .value .AutoValue ;
1920import com .google .cloud .dlp .v2 .DlpServiceClient ;
21+ import com .google .cloud .dlp .v2 .DlpServiceSettings ;
2022import com .google .privacy .dlp .v2 .ContentItem ;
2123import com .google .privacy .dlp .v2 .InspectContentRequest ;
2224import com .google .privacy .dlp .v2 .InspectContentResponse ;
4345import org .apache .beam .sdk .values .PCollectionTuple ;
4446import org .apache .beam .sdk .values .Row ;
4547import org .apache .beam .sdk .values .TupleTagList ;
46- import org .joda .time .Duration ;
4748import org .slf4j .Logger ;
4849import org .slf4j .LoggerFactory ;
50+ import org .threeten .bp .Duration ;
4951
5052@ AutoValue
5153public abstract class DLPTransform
@@ -88,8 +90,8 @@ public PCollectionTuple expand(PCollection<KV<String, String>> input) {
8890 public static class BatchRequest extends DoFn <KV <String , String >, KV <String , Iterable <String >>> {
8991
9092 private static final long serialVersionUID = 1L ;
91- // private final Counter numberOfRowsBagged =
92- // Metrics.counter(BatchRequest.class, "numberOfRowsBagged");
93+ // private final Counter numberOfRowsBagged =
94+ // Metrics.counter(BatchRequest.class, "numberOfRowsBagged");
9395 private Integer batchSize ;
9496
9597 public BatchRequest (Integer batchSize ) {
@@ -110,7 +112,7 @@ public void process(
110112 @ StateId ("elementsBag" ) BagState <KV <String , String >> elementsBag ) {
111113 elementsBag .add (element );
112114 // eventTimer.set(w.maxTimestamp());
113- eventTimer .offset (Duration .standardSeconds (30 )).setRelative ();
115+ eventTimer .offset (org . joda . time . Duration .standardSeconds (10 )).setRelative ();
114116 }
115117
116118 @ OnTimer ("eventTimer" )
@@ -127,7 +129,7 @@ public void onTimer(
127129 Integer elementSize = element .getValue ().getBytes ().length ;
128130 boolean clearBuffer = bufferSize .intValue () + elementSize .intValue () > batchSize ;
129131 if (clearBuffer ) {
130- //numberOfRowsBagged.inc(rows.size());
132+ // numberOfRowsBagged.inc(rows.size());
131133 LOG .debug ("Clear Buffer {} , Key {}" , bufferSize .intValue (), key );
132134 output .output (KV .of (key , rows ));
133135 // clean up in a method
@@ -145,7 +147,7 @@ public void onTimer(
145147 // must be a better way
146148 if (!rows .isEmpty ()) {
147149 LOG .debug ("Remaining buffer {}, key{}" , rows .size (), key );
148- //numberOfRowsBagged.inc(rows.size());
150+ // numberOfRowsBagged.inc(rows.size());
149151 output .output (KV .of (key , rows ));
150152 }
151153 }
@@ -157,6 +159,7 @@ public static class InspectData extends DoFn<KV<String, Iterable<String>>, Row>
157159 private InspectContentRequest .Builder requestBuilder ;
158160 private final Counter numberOfBytesInspected =
159161 Metrics .counter (InspectData .class , "NumberOfBytesInspected" );
162+ private DlpServiceClient dlpServiceClient ;
160163
161164 public InspectData (String projectId , String inspectTemplateName ) {
162165 this .projectId = projectId ;
@@ -171,63 +174,83 @@ public void setup() {
171174 .setInspectTemplateName (this .inspectTemplateName );
172175 }
173176
177+ @ StartBundle
178+ public void startBundle () throws IOException {
179+
180+ // DlpServiceSettings.Builder settingsBuilder = DlpServiceSettings.newBuilder();
181+ // settingsBuilder
182+ // .inspectContentSettings()
183+ // .setRetrySettings(
184+ // RetrySettings.newBuilder()
185+ // .setInitialRpcTimeout(Duration.ofSeconds(60))
186+ // .setMaxRpcTimeout(Duration.ofSeconds(60))
187+ // .build());
188+ dlpServiceClient = DlpServiceClient .create ();
189+ }
190+
191+ @ FinishBundle
192+ public void finishBundle () {
193+ if (dlpServiceClient != null ) {
194+ dlpServiceClient .close ();
195+ }
196+ }
197+
174198 @ ProcessElement
175199 public void processElement (ProcessContext c ) throws IOException {
176- try (DlpServiceClient dlpServiceClient = DlpServiceClient .create ()) {
177- String fileName = c .element ().getKey ();
178- ContentItem contentItem =
179- ContentItem .newBuilder ().setValue (emitResult (c .element ().getValue ())).build ();
180- this .requestBuilder .setItem (contentItem );
181- InspectContentResponse response =
182- dlpServiceClient .inspectContent (this .requestBuilder .build ());
183- String timeStamp = Util .getTimeStamp ();
184-
185- boolean hasErrors = response .findInitializationErrors ().stream ().count () > 0 ;
186- if (response .hasResult () && !hasErrors ) {
187- long bytesInspected = contentItem .getValue ().getBytes ().length ;
188- response
189- .getResult ()
190- .getFindingsList ()
191- .forEach (
192- finding -> {
193- Row row =
194- Row .withSchema (Util .bqDataSchema )
195- .addValues (
196- fileName ,
197- timeStamp ,
198- finding .getInfoType ().getName (),
199- finding .getLikelihood ().name (),
200- finding .getQuote (),
201- finding .getLocation ().getCodepointRange ().getStart (),
202- finding .getLocation ().getCodepointRange ().getEnd ())
203- .build ();
204- LOG .debug ("Row {}" , row );
205-
206- c .output (Util .inspectData , row );
207- });
208- numberOfBytesInspected .inc (bytesInspected );
209- c .output (
210- Util .auditData ,
211- Row .withSchema (Util .bqAuditSchema )
212- .addValues (fileName , timeStamp , bytesInspected , Util .INSPECTED )
213- .build ());
214- } else {
215- response
216- .findInitializationErrors ()
217- .forEach (
218- error -> {
219- c .output (
220- Util .errorData ,
221- Row .withSchema (Util .errorSchema )
222- .addValues (fileName , timeStamp , error .toString ())
223- .build ());
224- });
225- c .output (
226- Util .auditData ,
227- Row .withSchema (Util .bqAuditSchema )
228- .addValues (fileName , timeStamp , 0 , Util .FAILED )
229- .build ());
230- }
200+
201+ String fileName = c .element ().getKey ();
202+ String contents = emitResult (c .element ().getValue ());
203+ ContentItem contentItem = ContentItem .newBuilder ().setValue (contents ).build ();
204+ this .requestBuilder .setItem (contentItem );
205+ InspectContentResponse response =
206+ dlpServiceClient .inspectContent (this .requestBuilder .build ());
207+ String timeStamp = Util .getTimeStamp ();
208+
209+ boolean hasErrors = response .findInitializationErrors ().stream ().count () > 0 ;
210+ if (response .hasResult () && !hasErrors ) {
211+ long bytesInspected = contents .getBytes ().length ;
212+ response
213+ .getResult ()
214+ .getFindingsList ()
215+ .forEach (
216+ finding -> {
217+ Row row =
218+ Row .withSchema (Util .bqDataSchema )
219+ .addValues (
220+ fileName ,
221+ timeStamp ,
222+ finding .getInfoType ().getName (),
223+ finding .getLikelihood ().name (),
224+ finding .getQuote (),
225+ finding .getLocation ().getCodepointRange ().getStart (),
226+ finding .getLocation ().getCodepointRange ().getEnd ())
227+ .build ();
228+ LOG .debug ("Row {}" , row );
229+
230+ c .output (Util .inspectData , row );
231+ });
232+ numberOfBytesInspected .inc (bytesInspected );
233+ c .output (
234+ Util .auditData ,
235+ Row .withSchema (Util .bqAuditSchema )
236+ .addValues (fileName , timeStamp , bytesInspected , Util .INSPECTED )
237+ .build ());
238+ } else {
239+ response
240+ .findInitializationErrors ()
241+ .forEach (
242+ error -> {
243+ c .output (
244+ Util .errorData ,
245+ Row .withSchema (Util .errorSchema )
246+ .addValues (fileName , timeStamp , error .toString ())
247+ .build ());
248+ });
249+ c .output (
250+ Util .auditData ,
251+ Row .withSchema (Util .bqAuditSchema )
252+ .addValues (fileName , timeStamp , 0 , Util .FAILED )
253+ .build ());
231254 }
232255 }
233256 }
0 commit comments