Skip to content

Commit aa83cfe

Browse files
authored
Add Java code samples for de-id tabular data (GoogleCloudPlatform#3170)
To be linked from https://cloud.google.com/dlp/docs/examples-deid-tables
1 parent 8378c67 commit aa83cfe

6 files changed

+964
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2020 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_table_bucketing]
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.FixedSizeBucketingConfig;
29+
import com.google.privacy.dlp.v2.LocationName;
30+
import com.google.privacy.dlp.v2.PrimitiveTransformation;
31+
import com.google.privacy.dlp.v2.RecordTransformations;
32+
import com.google.privacy.dlp.v2.Table;
33+
import com.google.privacy.dlp.v2.Table.Row;
34+
import com.google.privacy.dlp.v2.Value;
35+
import java.io.IOException;
36+
37+
public class DeIdentifyTableBucketing {
38+
39+
public static void deIdentifyTableBucketing() throws IOException {
40+
// TODO(developer): Replace these variables before running the sample.
41+
String projectId = "your-project-id";
42+
Table tableToDeIdentify = Table.newBuilder()
43+
.addHeaders(FieldId.newBuilder().setName("AGE").build())
44+
.addHeaders(FieldId.newBuilder().setName("PATIENT").build())
45+
.addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build())
46+
.addRows(Row.newBuilder()
47+
.addValues(Value.newBuilder().setStringValue("101").build())
48+
.addValues(Value.newBuilder().setStringValue("Charles Dickens").build())
49+
.addValues(Value.newBuilder().setStringValue("95").build())
50+
.build())
51+
.addRows(Row.newBuilder()
52+
.addValues(Value.newBuilder().setStringValue("22").build())
53+
.addValues(Value.newBuilder().setStringValue("Jane Austen").build())
54+
.addValues(Value.newBuilder().setStringValue("21").build())
55+
.build())
56+
.addRows(Row.newBuilder()
57+
.addValues(Value.newBuilder().setStringValue("55").build())
58+
.addValues(Value.newBuilder().setStringValue("Mark Twain").build())
59+
.addValues(Value.newBuilder().setStringValue("75").build())
60+
.build())
61+
.build();
62+
63+
deIdentifyTableBucketing(projectId, tableToDeIdentify);
64+
}
65+
66+
public static Table deIdentifyTableBucketing(String projectId, Table tableToDeIdentify)
67+
throws IOException {
68+
// Initialize client that will be used to send requests. This client only needs to be created
69+
// once, and can be reused for multiple requests. After completing all of your requests, call
70+
// the "close" method on the client to safely clean up any remaining background resources.
71+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
72+
// Specify what content you want the service to de-identify.
73+
ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build();
74+
75+
// Specify how the content should be de-identified.
76+
FixedSizeBucketingConfig fixedSizeBucketingConfig =
77+
FixedSizeBucketingConfig.newBuilder()
78+
.setBucketSize(10)
79+
.setLowerBound(Value.newBuilder().setIntegerValue(0).build())
80+
.setUpperBound(Value.newBuilder().setIntegerValue(100).build())
81+
.build();
82+
PrimitiveTransformation primitiveTransformation =
83+
PrimitiveTransformation.newBuilder()
84+
.setFixedSizeBucketingConfig(fixedSizeBucketingConfig)
85+
.build();
86+
87+
// Specify field to be encrypted.
88+
FieldId fieldId = FieldId.newBuilder().setName("HAPPINESS SCORE").build();
89+
90+
// Associate the encryption with the specified field.
91+
FieldTransformation fieldTransformation =
92+
FieldTransformation.newBuilder()
93+
.setPrimitiveTransformation(primitiveTransformation)
94+
.addFields(fieldId)
95+
.build();
96+
RecordTransformations transformations =
97+
RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build();
98+
99+
DeidentifyConfig deidentifyConfig =
100+
DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build();
101+
102+
// Combine configurations into a request for the service.
103+
DeidentifyContentRequest request =
104+
DeidentifyContentRequest.newBuilder()
105+
.setParent(LocationName.of(projectId, "global").toString())
106+
.setItem(contentItem)
107+
.setDeidentifyConfig(deidentifyConfig)
108+
.build();
109+
110+
// Send the request and receive response from the service.
111+
DeidentifyContentResponse response = dlp.deidentifyContent(request);
112+
113+
// Print the results.
114+
System.out.println(
115+
"Table after de-identification: " + response.getItem().getTable());
116+
117+
return response.getItem().getTable();
118+
}
119+
}
120+
}
121+
// [END dlp_deidentify_table_bucketing]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Copyright 2020 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_table_condition_infotypes]
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.InfoType;
29+
import com.google.privacy.dlp.v2.InfoTypeTransformations;
30+
import com.google.privacy.dlp.v2.InfoTypeTransformations.InfoTypeTransformation;
31+
import com.google.privacy.dlp.v2.LocationName;
32+
import com.google.privacy.dlp.v2.PrimitiveTransformation;
33+
import com.google.privacy.dlp.v2.RecordCondition;
34+
import com.google.privacy.dlp.v2.RecordCondition.Condition;
35+
import com.google.privacy.dlp.v2.RecordCondition.Conditions;
36+
import com.google.privacy.dlp.v2.RecordCondition.Expressions;
37+
import com.google.privacy.dlp.v2.RecordTransformations;
38+
import com.google.privacy.dlp.v2.RelationalOperator;
39+
import com.google.privacy.dlp.v2.ReplaceWithInfoTypeConfig;
40+
import com.google.privacy.dlp.v2.Table;
41+
import com.google.privacy.dlp.v2.Table.Row;
42+
import com.google.privacy.dlp.v2.Value;
43+
import java.io.IOException;
44+
import java.util.List;
45+
import java.util.stream.Collectors;
46+
import java.util.stream.Stream;
47+
48+
public class DeIdentifyTableConditionInfoTypes {
49+
50+
public static void deIdentifyTableConditionInfoTypes() throws IOException {
51+
// TODO(developer): Replace these variables before running the sample.
52+
String projectId = "your-project-id";
53+
Table tableToDeIdentify = Table.newBuilder()
54+
.addHeaders(FieldId.newBuilder().setName("AGE").build())
55+
.addHeaders(FieldId.newBuilder().setName("PATIENT").build())
56+
.addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build())
57+
.addHeaders(FieldId.newBuilder().setName("FACTOID").build())
58+
.addRows(Row.newBuilder()
59+
.addValues(Value.newBuilder().setStringValue("101").build())
60+
.addValues(Value.newBuilder().setStringValue("Charles Dickens").build())
61+
.addValues(Value.newBuilder().setStringValue("95").build())
62+
.addValues(Value.newBuilder().setStringValue(
63+
"Charles Dickens name was a curse, possibly invented by Shakespeare.").build())
64+
.build())
65+
.addRows(Row.newBuilder()
66+
.addValues(Value.newBuilder().setStringValue("22").build())
67+
.addValues(Value.newBuilder().setStringValue("Jane Austen").build())
68+
.addValues(Value.newBuilder().setStringValue("21").build())
69+
.addValues(Value.newBuilder().setStringValue(
70+
"There are 14 kisses in Jane Austen's novels.").build())
71+
.build())
72+
.addRows(Row.newBuilder()
73+
.addValues(Value.newBuilder().setStringValue("55").build())
74+
.addValues(Value.newBuilder().setStringValue("Mark Twain").build())
75+
.addValues(Value.newBuilder().setStringValue("75").build())
76+
.addValues(Value.newBuilder().setStringValue("Mark Twain loved cats.").build())
77+
.build())
78+
.build();
79+
80+
deIdentifyTableConditionInfoTypes(projectId, tableToDeIdentify);
81+
}
82+
83+
public static Table deIdentifyTableConditionInfoTypes(String projectId, Table tableToDeIdentify)
84+
throws IOException {
85+
// Initialize client that will be used to send requests. This client only needs to be created
86+
// once, and can be reused for multiple requests. After completing all of your requests, call
87+
// the "close" method on the client to safely clean up any remaining background resources.
88+
try (DlpServiceClient dlp = DlpServiceClient.create()) {
89+
// Specify what content you want the service to de-identify.
90+
ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build();
91+
92+
// Specify how the content should be de-identified.
93+
// Select type of info to be replaced.
94+
InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build();
95+
// Specify that findings should be replaced with corresponding info type name.
96+
ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig =
97+
ReplaceWithInfoTypeConfig.getDefaultInstance();
98+
PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder()
99+
.setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig).build();
100+
// Associate info type with the replacement strategy
101+
InfoTypeTransformation infoTypeTransformation =
102+
InfoTypeTransformation.newBuilder()
103+
.addInfoTypes(infoType)
104+
.setPrimitiveTransformation(primitiveTransformation)
105+
.build();
106+
InfoTypeTransformations infoTypeTransformations =
107+
InfoTypeTransformations.newBuilder()
108+
.addTransformations(infoTypeTransformation)
109+
.build();
110+
111+
// Specify fields to be de-identified.
112+
List<FieldId> fieldIds = Stream.of("PATIENT", "FACTOID")
113+
.map(id -> FieldId.newBuilder().setName(id).build())
114+
.collect(Collectors.toList());
115+
116+
// Specify when the above fields should be de-identified.
117+
Condition condition = Condition.newBuilder()
118+
.setField(FieldId.newBuilder().setName("AGE").build())
119+
.setOperator(RelationalOperator.GREATER_THAN)
120+
.setValue(Value.newBuilder().setIntegerValue(89).build())
121+
.build();
122+
// Apply the condition to records
123+
RecordCondition recordCondition = RecordCondition.newBuilder()
124+
.setExpressions(Expressions.newBuilder()
125+
.setConditions(Conditions.newBuilder()
126+
.addConditions(condition)
127+
.build())
128+
.build())
129+
.build();
130+
131+
// Associate the de-identification and conditions with the specified fields.
132+
FieldTransformation fieldTransformation =
133+
FieldTransformation.newBuilder()
134+
.setInfoTypeTransformations(infoTypeTransformations)
135+
.addAllFields(fieldIds)
136+
.setCondition(recordCondition)
137+
.build();
138+
RecordTransformations transformations =
139+
RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build();
140+
141+
DeidentifyConfig deidentifyConfig =
142+
DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build();
143+
144+
// Combine configurations into a request for the service.
145+
DeidentifyContentRequest request =
146+
DeidentifyContentRequest.newBuilder()
147+
.setParent(LocationName.of(projectId, "global").toString())
148+
.setItem(contentItem)
149+
.setDeidentifyConfig(deidentifyConfig)
150+
.build();
151+
152+
// Send the request and receive response from the service.
153+
DeidentifyContentResponse response = dlp.deidentifyContent(request);
154+
155+
// Print the results.
156+
System.out.println(
157+
"Table after de-identification: " + response.getItem().getTable());
158+
159+
return response.getItem().getTable();
160+
}
161+
}
162+
}
163+
// [END dlp_deidentify_table_condition_infotypes]

0 commit comments

Comments
 (0)