|
| 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 = |
| 43 | + Table.newBuilder() |
| 44 | + .addHeaders(FieldId.newBuilder().setName("AGE").build()) |
| 45 | + .addHeaders(FieldId.newBuilder().setName("PATIENT").build()) |
| 46 | + .addHeaders(FieldId.newBuilder().setName("HAPPINESS SCORE").build()) |
| 47 | + .addRows( |
| 48 | + Row.newBuilder() |
| 49 | + .addValues(Value.newBuilder().setStringValue("101").build()) |
| 50 | + .addValues(Value.newBuilder().setStringValue("Charles Dickens").build()) |
| 51 | + .addValues(Value.newBuilder().setStringValue("95").build()) |
| 52 | + .build()) |
| 53 | + .addRows( |
| 54 | + Row.newBuilder() |
| 55 | + .addValues(Value.newBuilder().setStringValue("22").build()) |
| 56 | + .addValues(Value.newBuilder().setStringValue("Jane Austen").build()) |
| 57 | + .addValues(Value.newBuilder().setStringValue("21").build()) |
| 58 | + .build()) |
| 59 | + .addRows( |
| 60 | + Row.newBuilder() |
| 61 | + .addValues(Value.newBuilder().setStringValue("55").build()) |
| 62 | + .addValues(Value.newBuilder().setStringValue("Mark Twain").build()) |
| 63 | + .addValues(Value.newBuilder().setStringValue("75").build()) |
| 64 | + .build()) |
| 65 | + .build(); |
| 66 | + |
| 67 | + deIdentifyTableBucketing(projectId, tableToDeIdentify); |
| 68 | + } |
| 69 | + |
| 70 | + public static Table deIdentifyTableBucketing(String projectId, Table tableToDeIdentify) |
| 71 | + throws IOException { |
| 72 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 73 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 74 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 75 | + try (DlpServiceClient dlp = DlpServiceClient.create()) { |
| 76 | + // Specify what content you want the service to de-identify. |
| 77 | + ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); |
| 78 | + |
| 79 | + // Specify how the content should be de-identified. |
| 80 | + FixedSizeBucketingConfig fixedSizeBucketingConfig = |
| 81 | + FixedSizeBucketingConfig.newBuilder() |
| 82 | + .setBucketSize(10) |
| 83 | + .setLowerBound(Value.newBuilder().setIntegerValue(0).build()) |
| 84 | + .setUpperBound(Value.newBuilder().setIntegerValue(100).build()) |
| 85 | + .build(); |
| 86 | + PrimitiveTransformation primitiveTransformation = |
| 87 | + PrimitiveTransformation.newBuilder() |
| 88 | + .setFixedSizeBucketingConfig(fixedSizeBucketingConfig) |
| 89 | + .build(); |
| 90 | + |
| 91 | + // Specify field to be encrypted. |
| 92 | + FieldId fieldId = FieldId.newBuilder().setName("HAPPINESS SCORE").build(); |
| 93 | + |
| 94 | + // Associate the encryption with the specified field. |
| 95 | + FieldTransformation fieldTransformation = |
| 96 | + FieldTransformation.newBuilder() |
| 97 | + .setPrimitiveTransformation(primitiveTransformation) |
| 98 | + .addFields(fieldId) |
| 99 | + .build(); |
| 100 | + RecordTransformations transformations = |
| 101 | + RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); |
| 102 | + |
| 103 | + DeidentifyConfig deidentifyConfig = |
| 104 | + DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); |
| 105 | + |
| 106 | + // Combine configurations into a request for the service. |
| 107 | + DeidentifyContentRequest request = |
| 108 | + DeidentifyContentRequest.newBuilder() |
| 109 | + .setParent(LocationName.of(projectId, "global").toString()) |
| 110 | + .setItem(contentItem) |
| 111 | + .setDeidentifyConfig(deidentifyConfig) |
| 112 | + .build(); |
| 113 | + |
| 114 | + // Send the request and receive response from the service. |
| 115 | + DeidentifyContentResponse response = dlp.deidentifyContent(request); |
| 116 | + |
| 117 | + // Print the results. |
| 118 | + System.out.println("Table after de-identification: " + response.getItem().getTable()); |
| 119 | + |
| 120 | + return response.getItem().getTable(); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | +// [END dlp_deidentify_table_bucketing] |
0 commit comments