Parse findings stored as Protobuf text

This code sample shows how to extract details from inspection findings that are saved in Protobuf text format. Do this task if you saved your inspection findings to Cloud Storage. This example converts the exported SaveToGcsFindingsOutput object into a human-readable format. You can then use the output to analyze individual inspection findings.

Java

To learn how to install and use the client library for Sensitive Data Protection, see Sensitive Data Protection client libraries.

To authenticate to Sensitive Data Protection, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.


import com.google.privacy.dlp.v2.Finding;
import com.google.privacy.dlp.v2.SaveToGcsFindingsOutput;
import com.google.protobuf.ByteString;
import com.google.protobuf.TextFormat;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;

public class ProcessInspectFindingsSavedToGcs {

  public static void main(String[] args) throws Exception {
    // TODO(developer): Replace these variables before running the sample.
    String inputPath = "src/test/resources/save_to_gcs_findings.txt";
    processFindingsGcsFile(inputPath);
  }

  // Processes a file containing findings from a DLP inspect job.
  public static void processFindingsGcsFile(String inputPath)
      throws IOException {
    SaveToGcsFindingsOutput.Builder builder = SaveToGcsFindingsOutput.newBuilder();
    try (Reader reader =
        new InputStreamReader(new FileInputStream(inputPath), StandardCharsets.UTF_8)) {
      TextFormat.merge(reader, builder);
    }
    SaveToGcsFindingsOutput output = builder.build();

    // Parse the converted proto and process results
    System.out.println("Findings: " + output.getFindingsCount());
    for (Finding f : output.getFindingsList()) {
      System.out.println("\tInfo type: " + f.getInfoType().getName());
      System.out.println("\tLikelihood: " + f.getLikelihood());
    }
  }
}