|
| 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