Protobuf テキストとして保存された検出結果を解析する

このコードサンプルは、Protobuf テキスト形式で保存された検査結果から詳細情報を抽出する方法を示しています。このタスクは、検査結果を Cloud Storage に保存した場合に実行します。この例では、エクスポートされた SaveToGcsFindingsOutput オブジェクトを人が読める形式に変換します。この出力を使用して、個々の検査結果を分析できます。

Java

機密データの保護用のクライアント ライブラリをインストールして使用する方法については、機密データの保護のクライアント ライブラリをご覧ください。

機密データの保護のために認証するには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。


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