|
| 1 | +package dlp.snippets; |
| 2 | + |
| 3 | +// [START dlp_deidentify_date_shift] |
| 4 | + |
| 5 | +import com.google.cloud.dlp.v2.DlpServiceClient; |
| 6 | +import com.google.common.base.Splitter; |
| 7 | +import com.google.privacy.dlp.v2.ContentItem; |
| 8 | +import com.google.privacy.dlp.v2.DateShiftConfig; |
| 9 | +import com.google.privacy.dlp.v2.DeidentifyConfig; |
| 10 | +import com.google.privacy.dlp.v2.DeidentifyContentRequest; |
| 11 | +import com.google.privacy.dlp.v2.DeidentifyContentResponse; |
| 12 | +import com.google.privacy.dlp.v2.FieldId; |
| 13 | +import com.google.privacy.dlp.v2.FieldTransformation; |
| 14 | +import com.google.privacy.dlp.v2.PrimitiveTransformation; |
| 15 | +import com.google.privacy.dlp.v2.ProjectName; |
| 16 | +import com.google.privacy.dlp.v2.RecordTransformations; |
| 17 | +import com.google.privacy.dlp.v2.Table; |
| 18 | +import com.google.privacy.dlp.v2.Value; |
| 19 | +import com.google.type.Date; |
| 20 | +import java.io.BufferedReader; |
| 21 | +import java.io.BufferedWriter; |
| 22 | +import java.io.IOException; |
| 23 | +import java.nio.file.Files; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.nio.file.Paths; |
| 26 | +import java.time.LocalDate; |
| 27 | +import java.time.format.DateTimeFormatter; |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.List; |
| 30 | +import java.util.stream.Collectors; |
| 31 | + |
| 32 | +public class DeIdentifyWithDateShift { |
| 33 | + |
| 34 | + public static void deIdentifyWithDateShift() throws IOException { |
| 35 | + // TODO(developer): Replace these variables before running the sample. |
| 36 | + String projectId = "YOUR_PROJECT_ID"; |
| 37 | + Path inputCsvFile = Paths.get("path/to/your/input/file.csv"); |
| 38 | + Path outputCsvFile = Paths.get("path/to/your/output/file.csv"); |
| 39 | + deIdentifyWithDateShift(projectId, inputCsvFile, outputCsvFile); |
| 40 | + } |
| 41 | + |
| 42 | + public static void deIdentifyWithDateShift( |
| 43 | + String projectId, Path inputCsvFile, Path outputCsvFile) throws IOException { |
| 44 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 45 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 46 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 47 | + try (DlpServiceClient dlp = DlpServiceClient.create()) { |
| 48 | + // Read the contents of the CSV file into a Table |
| 49 | + List<FieldId> headers; |
| 50 | + List<Table.Row> rows; |
| 51 | + try (BufferedReader input = Files.newBufferedReader(inputCsvFile)) { |
| 52 | + // Parse and convert the first line into header names |
| 53 | + headers = |
| 54 | + Arrays.stream(input.readLine().split(",")) |
| 55 | + .map(header -> FieldId.newBuilder().setName(header).build()) |
| 56 | + .collect(Collectors.toList()); |
| 57 | + // Parse the remainder of the file as Table.Rows |
| 58 | + rows = |
| 59 | + input.lines().map(DeIdentifyWithDateShift::parseLineAsRow).collect(Collectors.toList()); |
| 60 | + } |
| 61 | + Table table = Table.newBuilder().addAllHeaders(headers).addAllRows(rows).build(); |
| 62 | + ContentItem item = ContentItem.newBuilder().setTable(table).build(); |
| 63 | + |
| 64 | + // Set the maximum days to shift dates backwards (lower bound) or forward (upper bound) |
| 65 | + DateShiftConfig dateShiftConfig = |
| 66 | + DateShiftConfig.newBuilder().setLowerBoundDays(5).setUpperBoundDays(5).build(); |
| 67 | + PrimitiveTransformation transformation = |
| 68 | + PrimitiveTransformation.newBuilder().setDateShiftConfig(dateShiftConfig).build(); |
| 69 | + // Specify which fields the DateShift should apply too |
| 70 | + List<FieldId> dateFields = Arrays.asList(headers.get(1), headers.get(3)); |
| 71 | + FieldTransformation fieldTransformation = |
| 72 | + FieldTransformation.newBuilder() |
| 73 | + .addAllFields(dateFields) |
| 74 | + .setPrimitiveTransformation(transformation) |
| 75 | + .build(); |
| 76 | + RecordTransformations recordTransformations = |
| 77 | + RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); |
| 78 | + // Specify the config for the de-identify request |
| 79 | + DeidentifyConfig deidentifyConfig = |
| 80 | + DeidentifyConfig.newBuilder().setRecordTransformations(recordTransformations).build(); |
| 81 | + |
| 82 | + // Combine configurations into a request for the service. |
| 83 | + DeidentifyContentRequest request = |
| 84 | + DeidentifyContentRequest.newBuilder() |
| 85 | + .setParent(ProjectName.of(projectId).toString()) |
| 86 | + .setItem(item) |
| 87 | + .setDeidentifyConfig(deidentifyConfig) |
| 88 | + .build(); |
| 89 | + |
| 90 | + // Send the request and receive response from the service |
| 91 | + DeidentifyContentResponse response = dlp.deidentifyContent(request); |
| 92 | + |
| 93 | + // Write the results to the target CSV file |
| 94 | + try (BufferedWriter writer = Files.newBufferedWriter(outputCsvFile)) { |
| 95 | + Table outTable = response.getItem().getTable(); |
| 96 | + String headerOut = |
| 97 | + outTable.getHeadersList().stream() |
| 98 | + .map(FieldId::getName) |
| 99 | + .collect(Collectors.joining(",")); |
| 100 | + writer.write(headerOut + "\n"); |
| 101 | + |
| 102 | + List<String> rowOutput = |
| 103 | + outTable.getRowsList().stream() |
| 104 | + .map(row -> joinRow(row.getValuesList())) |
| 105 | + .collect(Collectors.toList()); |
| 106 | + for (String line : rowOutput) { |
| 107 | + writer.write(line + "\n"); |
| 108 | + } |
| 109 | + System.out.println("Content written to file: " + outputCsvFile.toString()); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + // Convert the string from the csv file into com.google.type.Date |
| 115 | + public static Date parseAsDate(String s) { |
| 116 | + LocalDate date = LocalDate.parse(s, DateTimeFormatter.ofPattern("MM/dd/yyyy")); |
| 117 | + return Date.newBuilder() |
| 118 | + .setDay(date.getDayOfMonth()) |
| 119 | + .setMonth(date.getMonthValue()) |
| 120 | + .setYear(date.getYear()) |
| 121 | + .build(); |
| 122 | + } |
| 123 | + |
| 124 | + // Each row is in the format: Name,BirthDate,CreditCardNumber,RegisterDate |
| 125 | + public static Table.Row parseLineAsRow(String line) { |
| 126 | + List<String> values = Splitter.on(",").splitToList(line); |
| 127 | + Value name = Value.newBuilder().setStringValue(values.get(0)).build(); |
| 128 | + Value birthDate = Value.newBuilder().setDateValue(parseAsDate(values.get(1))).build(); |
| 129 | + Value creditCardNumber = Value.newBuilder().setStringValue(values.get(2)).build(); |
| 130 | + Value registerDate = Value.newBuilder().setDateValue(parseAsDate(values.get(3))).build(); |
| 131 | + return Table.Row.newBuilder() |
| 132 | + .addValues(name) |
| 133 | + .addValues(birthDate) |
| 134 | + .addValues(creditCardNumber) |
| 135 | + .addValues(registerDate) |
| 136 | + .build(); |
| 137 | + } |
| 138 | + |
| 139 | + public static String formatDate(Date d) { |
| 140 | + return String.format("%s/%s/%s", d.getMonth(), d.getDay(), d.getYear()); |
| 141 | + } |
| 142 | + |
| 143 | + public static String joinRow(List<Value> values) { |
| 144 | + String name = values.get(0).getStringValue(); |
| 145 | + String birthDate = formatDate(values.get(1).getDateValue()); |
| 146 | + String creditCardNumber = values.get(2).getStringValue(); |
| 147 | + String registerDate = formatDate(values.get(3).getDateValue()); |
| 148 | + return String.join(",", name, birthDate, creditCardNumber, registerDate); |
| 149 | + } |
| 150 | +} |
| 151 | +// [END dlp_deidentify_date_shift] |
0 commit comments