Als Protobuf-Text gespeicherte Ergebnisse parsen

Dieses Codebeispiel zeigt, wie Sie Details aus Inspektionsergebnissen extrahieren, die im Protobuf-Textformat gespeichert sind. Führen Sie diese Aufgabe aus, wenn Sie die Ergebnisse der Überprüfung in Cloud Storage gespeichert haben. In diesem Beispiel wird das exportierte SaveToGcsFindingsOutput-Objekt in ein für Menschen lesbares Format konvertiert. Sie können die Ausgabe dann verwenden, um einzelne Inspektionsergebnisse zu analysieren.

Java

Informationen zum Installieren und Verwenden der Clientbibliothek für Sensitive Data Protection finden Sie unter Sensitive Data Protection-Clientbibliotheken.

Richten Sie die Standardanmeldedaten für Anwendungen ein, um sich bei Sensitive Data Protection zu authentifizieren. Weitere Informationen finden Sie unter Authentifizierung für eine lokale Entwicklungsumgebung einrichten.


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());
    }
  }
}