Skip to content

Commit fea5536

Browse files
[DLP] Implemented sample for DeIdentify With Time Extraction (GoogleCloudPlatform#7852)
Implemented the [sample for de-identification of date with time extraction](https://cloud.google.com/dlp/docs/transformations-reference#time-extract) - [X] I have followed [Sample Format Guide](https://togithub.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md) - [X] `pom.xml` parent set to latest `shared-configuration` - [ ] Appropriate changes to README are included in PR - [ ] API's need to be enabled to test (tell us) - [ ] Environment Variables need to be set (ask us to set them) - [X] **Tests** pass: `mvn clean verify` **required** - [X] **Lint** passes: `mvn -P lint checkstyle:check` **required** - [ ] **Static Analysis**: `mvn -P lint clean compile pmd:cpd-check spotbugs:check` **advisory only** - [X] Please **merge** this PR for me once it is approved.
1 parent 64f1df4 commit fea5536

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dlp.snippets;
18+
19+
// [START dlp_deidentify_time_extract]
20+
21+
import com.google.cloud.dlp.v2.DlpServiceClient;
22+
import com.google.privacy.dlp.v2.ContentItem;
23+
import com.google.privacy.dlp.v2.DeidentifyConfig;
24+
import com.google.privacy.dlp.v2.DeidentifyContentRequest;
25+
import com.google.privacy.dlp.v2.DeidentifyContentResponse;
26+
import com.google.privacy.dlp.v2.FieldId;
27+
import com.google.privacy.dlp.v2.FieldTransformation;
28+
import com.google.privacy.dlp.v2.LocationName;
29+
import com.google.privacy.dlp.v2.PrimitiveTransformation;
30+
import com.google.privacy.dlp.v2.RecordTransformations;
31+
import com.google.privacy.dlp.v2.Table;
32+
import com.google.privacy.dlp.v2.TimePartConfig;
33+
import com.google.privacy.dlp.v2.Value;
34+
import java.io.IOException;
35+
import java.util.Arrays;
36+
import java.util.List;
37+
38+
public class DeIdentifyWithTimeExtraction {
39+
40+
public static void main(String[] args) throws Exception {
41+
// TODO(developer): Replace these variables before running the sample.
42+
// The Google Cloud project id to use as a parent resource.
43+
String projectId = "your-project-id";
44+
Table tableToDeIdentify =
45+
Table.newBuilder()
46+
.addHeaders(FieldId.newBuilder().setName("Name").build())
47+
.addHeaders(FieldId.newBuilder().setName("Birth Date").build())
48+
.addHeaders(FieldId.newBuilder().setName("Credit Card").build())
49+
.addHeaders(FieldId.newBuilder().setName("Register Date").build())
50+
.addRows(
51+
Table.Row.newBuilder()
52+
.addValues(Value.newBuilder().setStringValue("Ann").build())
53+
.addValues(Value.newBuilder().setStringValue("01/01/1970").build())
54+
.addValues(Value.newBuilder().setStringValue("4532908762519852").build())
55+
.addValues(Value.newBuilder().setStringValue("07/21/1996").build())
56+
.build())
57+
.addRows(
58+
Table.Row.newBuilder()
59+
.addValues(Value.newBuilder().setStringValue("James").build())
60+
.addValues(Value.newBuilder().setStringValue("03/06/1988").build())
61+
.addValues(Value.newBuilder().setStringValue("4301261899725540").build())
62+
.addValues(Value.newBuilder().setStringValue("04/09/2001").build())
63+
.build())
64+
.build();
65+
deIdentifyWithDateShift(projectId, tableToDeIdentify);
66+
}
67+
68+
public static Table deIdentifyWithDateShift(String projectId, Table tableToDeIdentify)
69+
throws IOException {
70+
// Initialize client that will be used to send requests. This client only needs to be created
71+
// once, and can be reused for multiple requests. After completing all of your requests, call
72+
// the "close" method on the client to safely clean up any remaining background resources.
73+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
74+
// Read the contents of the Table
75+
76+
ContentItem item = ContentItem.newBuilder().setTable(tableToDeIdentify).build();
77+
78+
// Specify the time part to extract
79+
TimePartConfig timePartConfig =
80+
TimePartConfig.newBuilder().setPartToExtract(TimePartConfig.TimePart.YEAR).build();
81+
82+
PrimitiveTransformation transformation =
83+
PrimitiveTransformation.newBuilder().setTimePartConfig(timePartConfig).build();
84+
85+
// Specify which fields the TimePart should apply too
86+
List<FieldId> dateFields =
87+
Arrays.asList(
88+
FieldId.newBuilder().setName("Birth Date").build(),
89+
FieldId.newBuilder().setName("Register Date").build());
90+
91+
FieldTransformation fieldTransformation =
92+
FieldTransformation.newBuilder()
93+
.addAllFields(dateFields)
94+
.setPrimitiveTransformation(transformation)
95+
.build();
96+
97+
RecordTransformations recordTransformations =
98+
RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build();
99+
100+
// Specify the config for the de-identify request
101+
DeidentifyConfig deidentifyConfig =
102+
DeidentifyConfig.newBuilder().setRecordTransformations(recordTransformations).build();
103+
104+
// Combine configurations into a request for the service.
105+
DeidentifyContentRequest request =
106+
DeidentifyContentRequest.newBuilder()
107+
.setParent(LocationName.of(projectId, "global").toString())
108+
.setItem(item)
109+
.setDeidentifyConfig(deidentifyConfig)
110+
.build();
111+
112+
// Send the request and receive response from the service
113+
DeidentifyContentResponse response = dlp.deidentifyContent(request);
114+
System.out.println("Table after de-identification: " + response.getItem().getTable());
115+
return response.getItem().getTable();
116+
}
117+
}
118+
}
119+
120+
// [END dlp_deidentify_time_extract]

dlp/snippets/src/test/java/dlp/snippets/DeIdentificationTests.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,57 @@ public void testDeIdentifyWithFpeSurrogate() throws IOException, NoSuchAlgorithm
573573
assertThat(output).contains("Text after de-identification: ");
574574
}
575575

576+
@Test
577+
public void testDeIdentifyWithTimeExtraction() throws IOException {
578+
Table tableToDeIdentify =
579+
Table.newBuilder()
580+
.addHeaders(FieldId.newBuilder().setName("Name").build())
581+
.addHeaders(FieldId.newBuilder().setName("Birth Date").build())
582+
.addHeaders(FieldId.newBuilder().setName("Credit Card").build())
583+
.addHeaders(FieldId.newBuilder().setName("Register Date").build())
584+
.addRows(
585+
Table.Row.newBuilder()
586+
.addValues(Value.newBuilder().setStringValue("Ann").build())
587+
.addValues(Value.newBuilder().setStringValue("01/01/1970").build())
588+
.addValues(Value.newBuilder().setStringValue("4532908762519852").build())
589+
.addValues(Value.newBuilder().setStringValue("07/21/1996").build())
590+
.build())
591+
.addRows(
592+
Table.Row.newBuilder()
593+
.addValues(Value.newBuilder().setStringValue("James").build())
594+
.addValues(Value.newBuilder().setStringValue("03/06/1988").build())
595+
.addValues(Value.newBuilder().setStringValue("4301261899725540").build())
596+
.addValues(Value.newBuilder().setStringValue("04/09/2001").build())
597+
.build())
598+
.build();
599+
Table expectedTable =
600+
Table.newBuilder()
601+
.addHeaders(FieldId.newBuilder().setName("Name").build())
602+
.addHeaders(FieldId.newBuilder().setName("Birth Date").build())
603+
.addHeaders(FieldId.newBuilder().setName("Credit Card").build())
604+
.addHeaders(FieldId.newBuilder().setName("Register Date").build())
605+
.addRows(
606+
Table.Row.newBuilder()
607+
.addValues(Value.newBuilder().setStringValue("Ann").build())
608+
.addValues(Value.newBuilder().setStringValue("1970").build())
609+
.addValues(Value.newBuilder().setStringValue("4532908762519852").build())
610+
.addValues(Value.newBuilder().setStringValue("1996").build())
611+
.build())
612+
.addRows(
613+
Table.Row.newBuilder()
614+
.addValues(Value.newBuilder().setStringValue("James").build())
615+
.addValues(Value.newBuilder().setStringValue("1988").build())
616+
.addValues(Value.newBuilder().setStringValue("4301261899725540").build())
617+
.addValues(Value.newBuilder().setStringValue("2001").build())
618+
.build())
619+
.build();
620+
Table table =
621+
DeIdentifyWithTimeExtraction.deIdentifyWithDateShift(PROJECT_ID, tableToDeIdentify);
622+
String output = bout.toString();
623+
assertThat(output).contains("Table after de-identification:");
624+
assertThat(table).isEqualTo(expectedTable);
625+
}
626+
576627
@Test
577628
public void testDeIdentifyDataReplaceWithDictionary() throws IOException {
578629
DeIdentifyDataReplaceWithDictionary.deidentifyDataReplaceWithDictionary(

0 commit comments

Comments
 (0)