Java Code Examples for com.google.cloud.dlp.v2.DlpServiceClient#create()
The following examples show how to use
com.google.cloud.dlp.v2.DlpServiceClient#create() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JobsList.java From java-docs-samples with Apache License 2.0 | 6 votes |
public static void listJobs(String projectId) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { // Construct the request to be sent by the client. // For more info on filters and job types, // see https://cloud.google.com/dlp/docs/reference/rest/v2/projects.dlpJobs/list ListDlpJobsRequest listDlpJobsRequest = ListDlpJobsRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setFilter("state=DONE") .setType(DlpJobType.valueOf("INSPECT_JOB")) .build(); // Send the request to list jobs and process the response DlpServiceClient.ListDlpJobsPagedResponse response = dlpServiceClient.listDlpJobs(listDlpJobsRequest); System.out.println("DLP jobs found:"); for (DlpJob dlpJob : response.getPage().getValues()) { System.out.println(dlpJob.getName() + " -- " + dlpJob.getState()); } } }
Example 2
Source File: TemplatesTests.java From java-docs-samples with Apache License 2.0 | 6 votes |
private static InspectTemplate createTemplate() throws IOException { try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { List<InfoType> infoTypes = Stream.of("PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER") .map(it -> InfoType.newBuilder().setName(it).build()) .collect(Collectors.toList()); InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).build(); InspectTemplate inspectTemplate = InspectTemplate.newBuilder().setInspectConfig(inspectConfig).build(); CreateInspectTemplateRequest createInspectTemplateRequest = CreateInspectTemplateRequest.newBuilder() .setParent(LocationName.of(PROJECT_ID, "global").toString()) .setInspectTemplate(inspectTemplate) .build(); return dlpServiceClient.createInspectTemplate(createInspectTemplateRequest); } }
Example 3
Source File: DLPReidentifyText.java From beam with Apache License 2.0 | 6 votes |
@Setup public void setup() throws IOException { requestBuilder = ReidentifyContentRequest.newBuilder().setParent(ProjectName.of(projectId).toString()); if (inspectTemplateName != null) { requestBuilder.setInspectTemplateName(inspectTemplateName); } if (inspectConfig != null) { requestBuilder.setInspectConfig(inspectConfig); } if (reidentifyConfig != null) { requestBuilder.setReidentifyConfig(reidentifyConfig); } if (reidentifyTemplateName != null) { requestBuilder.setReidentifyTemplateName(reidentifyTemplateName); } dlpServiceClient = DlpServiceClient.create(); }
Example 4
Source File: RedactImageFileAllText.java From java-docs-samples with Apache License 2.0 | 5 votes |
static void redactImageFileAllText(String projectId, String inputPath, String outputPath) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { // Specify the content to be redacted. ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath)); ByteContentItem byteItem = ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build(); // Enable redaction of all text. ImageRedactionConfig imageRedactionConfig = ImageRedactionConfig.newBuilder().setRedactAllText(true).build(); // Construct the Redact request to be sent by the client. // Do not specify the type of info to redact. RedactImageRequest request = RedactImageRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setByteItem(byteItem) .addImageRedactionConfigs(imageRedactionConfig) .build(); // Use the client to send the API request. RedactImageResponse response = dlp.redactImage(request); // Parse the response and process results. FileOutputStream redacted = new FileOutputStream(outputPath); redacted.write(response.getRedactedImage().toByteArray()); redacted.close(); System.out.println("Redacted image written to " + outputPath); } }
Example 5
Source File: TemplatesCreate.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void createInspectTemplate(String projectId) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { // Specify the type of info the inspection will look for. // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types List<InfoType> infoTypes = Stream.of("PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER") .map(it -> InfoType.newBuilder().setName(it).build()) .collect(Collectors.toList()); // Construct the inspection configuration for the template InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).build(); // Optionally set a display name and a description for the template String displayName = "Inspection Config Template"; String description = "Save configuration for future inspection jobs"; // Build the template InspectTemplate inspectTemplate = InspectTemplate.newBuilder() .setInspectConfig(inspectConfig) .setDisplayName(displayName) .setDescription(description) .build(); // Create the request to be sent by the client CreateInspectTemplateRequest createInspectTemplateRequest = CreateInspectTemplateRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setInspectTemplate(inspectTemplate) .build(); // Send the request to the API and process the response InspectTemplate response = dlpServiceClient.createInspectTemplate(createInspectTemplateRequest); System.out.printf("Template created: %s", response.getName()); } }
Example 6
Source File: DLPClient.java From android-docs-samples with Apache License 2.0 | 5 votes |
/** * Create a new client. * <p> * Ensure that you have enabled the DLP API for the given project. * * @param credentials authentication credentials * @param projectId project ID */ public DLPClient(@NonNull GoogleCredentials credentials, @NonNull String projectId) { mProjectId = projectId; // create the client try { mClient = DlpServiceClient.create(DlpServiceSettings.newBuilder() .setCredentialsProvider(() -> credentials) .build()); } catch (IOException e) { throw new IllegalStateException("Unable to create client", e); } }
Example 7
Source File: TemplatesList.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void listInspectTemplates(String projectId) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { // Create the request to be sent by the client ListInspectTemplatesRequest request = ListInspectTemplatesRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setPageSize(1) .build(); // Send the request ListInspectTemplatesPagedResponse response = dlpServiceClient.listInspectTemplates(request); // Parse through and process the response System.out.println("Templates found:"); for (InspectTemplate template : response.getPage().getResponse().getInspectTemplatesList()) { System.out.printf("Template name: %s\n", template.getName()); if (template.getDisplayName() != null) { System.out.printf("\tDisplay name: %s \n", template.getDisplayName()); System.out.printf("\tCreate time: %s \n", template.getCreateTime()); System.out.printf("\tUpdate time: %s \n", template.getUpdateTime()); // print inspection config InspectConfig inspectConfig = template.getInspectConfig(); for (InfoType infoType : inspectConfig.getInfoTypesList()) { System.out.printf("\tInfoType: %s\n", infoType.getName()); } System.out.printf("\tMin likelihood: %s\n", inspectConfig.getMinLikelihood()); System.out.printf("\tLimits: %s\n", inspectConfig.getLimits().getMaxFindingsPerRequest()); } } } }
Example 8
Source File: InfoTypesList.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void listInfoTypes() throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlpClient = DlpServiceClient.create()) { // Construct the request to be sent by the client ListInfoTypesRequest listInfoTypesRequest = ListInfoTypesRequest.newBuilder() // Only return infoTypes supported by certain parts of the API. // Supported filters are "supported_by=INSPECT" and "supported_by=RISK_ANALYSIS" // Defaults to "supported_by=INSPECT" .setFilter("supported_by=INSPECT") // BCP-47 language code for localized infoType friendly names. // Defaults to "en_US" .setLanguageCode("en-US") .build(); // Use the client to send the API request. ListInfoTypesResponse response = dlpClient.listInfoTypes(listInfoTypesRequest); // Parse the response and process the results System.out.println("Infotypes found:"); for (InfoTypeDescription infoTypeDescription : response.getInfoTypesList()) { System.out.println("Name : " + infoTypeDescription.getName()); System.out.println("Display name : " + infoTypeDescription.getDisplayName()); } } }
Example 9
Source File: InspectImageFile.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectImageFile(String projectId, String filePath) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { // Specify the type and content to be inspected. ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath)); ByteContentItem byteItem = ByteContentItem.newBuilder().setType(BytesType.IMAGE).setData(fileBytes).build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); // Specify the type of info the inspection will look for. List<InfoType> infoTypes = new ArrayList<>(); // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types for (String typeName : new String[]{"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } // Construct the configuration for the Inspect request. InspectConfig config = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .setIncludeQuote(true) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .build(); // Use the client to send the API request. InspectContentResponse response = dlp.inspectContent(request); // Parse the response and process results. System.out.println("Findings: " + response.getResult().getFindingsCount()); for (Finding f : response.getResult().getFindingsList()) { System.out.println("\tQuote: " + f.getQuote()); System.out.println("\tInfo type: " + f.getInfoType().getName()); System.out.println("\tLikelihood: " + f.getLikelihood()); } } }
Example 10
Source File: InspectTable.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectTable(String projectId, Table tableToInspect) { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { // Specify the table to be inspected. ContentItem item = ContentItem.newBuilder().setTable(tableToInspect).build(); // Specify the type of info the inspection will look for. // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types InfoType infoType = InfoType.newBuilder().setName("PHONE_NUMBER").build(); // Construct the configuration for the Inspect request. InspectConfig config = InspectConfig.newBuilder() .addInfoTypes(infoType) .setIncludeQuote(true) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .build(); // Use the client to send the API request. InspectContentResponse response = dlp.inspectContent(request); // Parse the response and process results System.out.println("Findings: " + response.getResult().getFindingsCount()); for (Finding f : response.getResult().getFindingsList()) { System.out.println("\tQuote: " + f.getQuote()); System.out.println("\tInfo type: " + f.getInfoType().getName()); System.out.println("\tLikelihood: " + f.getLikelihood()); } } catch (Exception e) { System.out.println("Error during inspectString: \n" + e.toString()); } }
Example 11
Source File: DeIdentifyTableConditionInfoTypes.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static Table deIdentifyTableConditionInfoTypes(String projectId, Table tableToDeIdentify) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { // Specify what content you want the service to de-identify. ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); // Specify how the content should be de-identified. // Select type of info to be replaced. InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build(); // Specify that findings should be replaced with corresponding info type name. ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig = ReplaceWithInfoTypeConfig.getDefaultInstance(); PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig).build(); // Associate info type with the replacement strategy InfoTypeTransformation infoTypeTransformation = InfoTypeTransformation.newBuilder() .addInfoTypes(infoType) .setPrimitiveTransformation(primitiveTransformation) .build(); InfoTypeTransformations infoTypeTransformations = InfoTypeTransformations.newBuilder() .addTransformations(infoTypeTransformation) .build(); // Specify fields to be de-identified. List<FieldId> fieldIds = Stream.of("PATIENT", "FACTOID") .map(id -> FieldId.newBuilder().setName(id).build()) .collect(Collectors.toList()); // Specify when the above fields should be de-identified. Condition condition = Condition.newBuilder() .setField(FieldId.newBuilder().setName("AGE").build()) .setOperator(RelationalOperator.GREATER_THAN) .setValue(Value.newBuilder().setIntegerValue(89).build()) .build(); // Apply the condition to records RecordCondition recordCondition = RecordCondition.newBuilder() .setExpressions(Expressions.newBuilder() .setConditions(Conditions.newBuilder() .addConditions(condition) .build()) .build()) .build(); // Associate the de-identification and conditions with the specified fields. FieldTransformation fieldTransformation = FieldTransformation.newBuilder() .setInfoTypeTransformations(infoTypeTransformations) .addAllFields(fieldIds) .setCondition(recordCondition) .build(); RecordTransformations transformations = RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); DeidentifyConfig deidentifyConfig = DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); // Combine configurations into a request for the service. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(contentItem) .setDeidentifyConfig(deidentifyConfig) .build(); // Send the request and receive response from the service. DeidentifyContentResponse response = dlp.deidentifyContent(request); // Print the results. System.out.println( "Table after de-identification: " + response.getItem().getTable()); return response.getItem().getTable(); } }
Example 12
Source File: DeIdentifyTableInfoTypes.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static Table deIdentifyTableInfoTypes(String projectId, Table tableToDeIdentify) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { // Specify what content you want the service to de-identify. ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); // Specify how the content should be de-identified. // Select type of info to be replaced. InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build(); // Specify that findings should be replaced with corresponding info type name. ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig = ReplaceWithInfoTypeConfig.getDefaultInstance(); PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig).build(); // Associate info type with the replacement strategy InfoTypeTransformation infoTypeTransformation = InfoTypeTransformation.newBuilder() .addInfoTypes(infoType) .setPrimitiveTransformation(primitiveTransformation) .build(); InfoTypeTransformations infoTypeTransformations = InfoTypeTransformations.newBuilder() .addTransformations(infoTypeTransformation) .build(); // Specify fields to be de-identified. List<FieldId> fieldIds = Stream.of("PATIENT", "FACTOID") .map(id -> FieldId.newBuilder().setName(id).build()) .collect(Collectors.toList()); // Associate the de-identification and conditions with the specified field. FieldTransformation fieldTransformation = FieldTransformation.newBuilder() .setInfoTypeTransformations(infoTypeTransformations) .addAllFields(fieldIds) .build(); RecordTransformations transformations = RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); DeidentifyConfig deidentifyConfig = DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); // Combine configurations into a request for the service. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(contentItem) .setDeidentifyConfig(deidentifyConfig) .build(); // Send the request and receive response from the service. DeidentifyContentResponse response = dlp.deidentifyContent(request); // Print the results. System.out.println( "Table after de-identification: " + response.getItem().getTable()); return response.getItem().getTable(); } }
Example 13
Source File: InspectStringOmitOverlap.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectStringOmitOverlap(String projectId, String textToInspect) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { // Specify the type and content to be inspected. ByteContentItem byteItem = ByteContentItem.newBuilder() .setType(BytesType.TEXT_UTF8) .setData(ByteString.copyFromUtf8(textToInspect)) .build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); // Specify the type of info the inspection will look for. // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types. List<InfoType> infoTypes = new ArrayList<>(); for (String typeName : new String[]{"PERSON_NAME", "EMAIL_ADDRESS"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } // Exclude EMAIL_ADDRESS matches ExclusionRule exclusionRule = ExclusionRule.newBuilder() .setExcludeInfoTypes(ExcludeInfoTypes.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS"))) .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH) .build(); // Construct a ruleset that applies the exclusion rule to the PERSON_NAME infotype. // If a PERSON_NAME match overlaps with an EMAIL_ADDRESS match, the PERSON_NAME match will // be excluded. InspectionRuleSet ruleSet = InspectionRuleSet.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME")) .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule)) .build(); // Construct the configuration for the Inspect request, including the ruleset. InspectConfig config = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .setIncludeQuote(true) .addRuleSet(ruleSet) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .build(); // Use the client to send the API request. InspectContentResponse response = dlp.inspectContent(request); // Parse the response and process results System.out.println("Findings: " + response.getResult().getFindingsCount()); for (Finding f : response.getResult().getFindingsList()) { System.out.println("\tQuote: " + f.getQuote()); System.out.println("\tInfo type: " + f.getInfoType().getName()); System.out.println("\tLikelihood: " + f.getLikelihood()); } } }
Example 14
Source File: InspectStringWithExclusionDict.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectStringWithExclusionDict(String projectId, String textToInspect, List<String> excludedMatchList) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { // Specify the type and content to be inspected. ByteContentItem byteItem = ByteContentItem.newBuilder() .setType(BytesType.TEXT_UTF8) .setData(ByteString.copyFromUtf8(textToInspect)) .build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); // Specify the type of info the inspection will look for. // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types. List<InfoType> infoTypes = new ArrayList<>(); for (String typeName : new String[]{"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } // Exclude matches from the specified excludedMatchList. ExclusionRule exclusionRule = ExclusionRule.newBuilder() .setMatchingType(MatchingType.MATCHING_TYPE_FULL_MATCH) .setDictionary(Dictionary.newBuilder() .setWordList(WordList.newBuilder().addAllWords(excludedMatchList))) .build(); // Construct a ruleset that applies the exclusion rule to the EMAIL_ADDRESSES infotype. InspectionRuleSet ruleSet = InspectionRuleSet.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS")) .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule)) .build(); // Construct the configuration for the Inspect request, including the ruleset. InspectConfig config = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .setIncludeQuote(true) .addRuleSet(ruleSet) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .build(); // Use the client to send the API request. InspectContentResponse response = dlp.inspectContent(request); // Parse the response and process results System.out.println("Findings: " + response.getResult().getFindingsCount()); for (Finding f : response.getResult().getFindingsList()) { System.out.println("\tQuote: " + f.getQuote()); System.out.println("\tInfo type: " + f.getInfoType().getName()); System.out.println("\tLikelihood: " + f.getLikelihood()); } } }
Example 15
Source File: DeIdentifyWithSimpleWordList.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void deidentifyWithSimpleWordList(String projectId, String textToDeIdentify) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { // Specify what content you want the service to DeIdentify. ContentItem contentItem = ContentItem.newBuilder().setValue(textToDeIdentify).build(); // Construct the word list to be detected Dictionary wordList = Dictionary.newBuilder() .setWordList( WordList.newBuilder() .addWords("RM-GREEN") .addWords("RM-YELLOW") .addWords("RM-ORANGE") .build()) .build(); // Specify the word list custom info type the inspection will look for. InfoType infoType = InfoType.newBuilder().setName("CUSTOM_ROOM_ID").build(); CustomInfoType customInfoType = CustomInfoType.newBuilder().setInfoType(infoType).setDictionary(wordList).build(); InspectConfig inspectConfig = InspectConfig.newBuilder().addCustomInfoTypes(customInfoType).build(); // Define type of deidentification as replacement. PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() .setReplaceWithInfoTypeConfig(ReplaceWithInfoTypeConfig.getDefaultInstance()) .build(); // Associate deidentification type with info type. InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder() .addInfoTypes(infoType) .setPrimitiveTransformation(primitiveTransformation) .build(); // Construct the configuration for the Redact request and list all desired transformations. DeidentifyConfig deidentifyConfig = DeidentifyConfig.newBuilder() .setInfoTypeTransformations( InfoTypeTransformations.newBuilder().addTransformations(transformation)) .build(); // Combine configurations into a request for the service. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(contentItem) .setInspectConfig(inspectConfig) .setDeidentifyConfig(deidentifyConfig) .build(); // Send the request and receive response from the service DeidentifyContentResponse response = dlp.deidentifyContent(request); // Print the results System.out.println( "Text after replace with infotype config: " + response.getItem().getValue()); } }
Example 16
Source File: InspectStringCustomHotword.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectStringCustomHotword(String projectId, String textToInspect, String customHotword) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { // Specify the type and content to be inspected. ByteContentItem byteItem = ByteContentItem.newBuilder() .setType(BytesType.TEXT_UTF8) .setData(ByteString.copyFromUtf8(textToInspect)) .build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); // Increase likelihood of matches that have customHotword nearby HotwordRule hotwordRule = HotwordRule.newBuilder() .setHotwordRegex(Regex.newBuilder().setPattern(customHotword)) .setProximity(Proximity.newBuilder().setWindowBefore(50)) .setLikelihoodAdjustment( LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.VERY_LIKELY)) .build(); // Construct a ruleset that applies the hotword rule to the PERSON_NAME infotype. InspectionRuleSet ruleSet = InspectionRuleSet.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME").build()) .addRules(InspectionRule.newBuilder().setHotwordRule(hotwordRule)) .build(); // Construct the configuration for the Inspect request. InspectConfig config = InspectConfig.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME").build()) .setIncludeQuote(true) .addRuleSet(ruleSet) .setMinLikelihood(Likelihood.VERY_LIKELY) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .build(); // Use the client to send the API request. InspectContentResponse response = dlp.inspectContent(request); // Parse the response and process results System.out.println("Findings: " + response.getResult().getFindingsCount()); for (Finding f : response.getResult().getFindingsList()) { System.out.println("\tQuote: " + f.getQuote()); System.out.println("\tInfo type: " + f.getInfoType().getName()); System.out.println("\tLikelihood: " + f.getLikelihood()); } } }
Example 17
Source File: InspectTextFile.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectTextFile(String projectId, String filePath) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { // Specify the type and content to be inspected. ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath)); ByteContentItem byteItem = ByteContentItem.newBuilder().setType(BytesType.TEXT_UTF8).setData(fileBytes).build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); // Specify the type of info the inspection will look for. List<InfoType> infoTypes = new ArrayList<>(); // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types for (String typeName : new String[]{"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } // Construct the configuration for the Inspect request. InspectConfig config = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .setIncludeQuote(true) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .build(); // Use the client to send the API request. InspectContentResponse response = dlp.inspectContent(request); // Parse the response and process results System.out.println("Findings: " + response.getResult().getFindingsCount()); for (Finding f : response.getResult().getFindingsList()) { System.out.println("\tQuote: " + f.getQuote()); System.out.println("\tInfo type: " + f.getInfoType().getName()); System.out.println("\tLikelihood: " + f.getLikelihood()); } } }
Example 18
Source File: DeIdentifyWithRedaction.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void deIdentifyWithRedaction(String projectId, String textToRedact) { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { // Specify the content to be inspected. ContentItem item = ContentItem.newBuilder() .setValue(textToRedact).build(); // Specify the type of info the inspection will look for. // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types InfoType infoType = InfoType.newBuilder().setName("EMAIL_ADDRESS").build(); InspectConfig inspectConfig = InspectConfig.newBuilder().addInfoTypes(infoType).build(); // Define type of deidentification. PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() .setRedactConfig(RedactConfig.getDefaultInstance()) .build(); // Associate deidentification type with info type. InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder() .addInfoTypes(infoType) .setPrimitiveTransformation(primitiveTransformation) .build(); // Construct the configuration for the Redact request and list all desired transformations. DeidentifyConfig redactConfig = DeidentifyConfig.newBuilder() .setInfoTypeTransformations(InfoTypeTransformations.newBuilder() .addTransformations(transformation)) .build(); // Construct the Redact request to be sent by the client. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setDeidentifyConfig(redactConfig) .setInspectConfig(inspectConfig) .build(); // Use the client to send the API request. DeidentifyContentResponse response = dlp.deidentifyContent(request); // Parse the response and process results System.out.println("Text after redaction: " + response.getItem().getValue()); } catch (Exception e) { System.out.println("Error during inspectString: \n" + e.toString()); } }
Example 19
Source File: DeIdentifyTableRowSuppress.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static Table deIdentifyTableRowSuppress(String projectId, Table tableToDeIdentify) throws IOException { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. After completing all of your requests, call // the "close" method on the client to safely clean up any remaining background resources. try (DlpServiceClient dlp = DlpServiceClient.create()) { // Specify what content you want the service to de-identify. ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); // Specify when the content should be de-identified. Condition condition = Condition.newBuilder() .setField(FieldId.newBuilder().setName("AGE").build()) .setOperator(RelationalOperator.GREATER_THAN) .setValue(Value.newBuilder().setIntegerValue(89).build()).build(); // Apply the condition to record suppression. RecordSuppression recordSuppressions = RecordSuppression.newBuilder() .setCondition(RecordCondition.newBuilder() .setExpressions(Expressions.newBuilder() .setConditions(Conditions.newBuilder().addConditions(condition).build()) .build()) .build()) .build(); // Use record suppression as the only transformation RecordTransformations transformations = RecordTransformations.newBuilder() .addRecordSuppressions(recordSuppressions) .build(); DeidentifyConfig deidentifyConfig = DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); // Combine configurations into a request for the service. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(contentItem) .setDeidentifyConfig(deidentifyConfig) .build(); // Send the request and receive response from the service. DeidentifyContentResponse response = dlp.deidentifyContent(request); // Print the results. System.out.println( "Table after de-identification: " + response.getItem().getTable()); return response.getItem().getTable(); } }
Example 20
Source File: S3Import.java From dlp-dataflow-deidentification with Apache License 2.0 | 4 votes |
@ProcessElement public void processElement(ProcessContext c) throws IOException { try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) { if (!c.element().getValue().isEmpty()) { ContentItem contentItem = ContentItem.newBuilder().setValue(c.element().getValue()).build(); this.requestBuilder.setItem(contentItem); if (this.requestBuilder.build().getSerializedSize() > DLP_PAYLOAD_LIMIT) { String errorMessage = String.format( "Payload Size %s Exceeded Batch Size %s", this.requestBuilder.build().getSerializedSize(), DLP_PAYLOAD_LIMIT); c.output(apiResponseFailedElements, errorMessage); } else { InspectContentResponse response = dlpServiceClient.inspectContent(this.requestBuilder.build()); String timestamp = TIMESTAMP_FORMATTER.print(Instant.now().toDateTime(DateTimeZone.UTC)); response .getResult() .getFindingsList() .forEach( finding -> { List<TableCell> cells = new ArrayList<>(); TableRow row = new TableRow(); cells.add(new TableCell().set("file_name", c.element().getKey())); row.set("file_name", c.element().getKey()); cells.add(new TableCell().set("inspection_timestamp", timestamp)); row.set("inspection_timestamp", timestamp); cells.add(new TableCell().set("infoType", finding.getInfoType().getName())); row.set("infoType", finding.getInfoType().getName()); cells.add(new TableCell().set("likelihood", finding.getLikelihood().name())); row.set("likelihood", finding.getLikelihood().name()); row.setF(cells); c.output(apiResponseSuccessElements, KV.of(BQ_TABLE_NAME, row)); }); numberOfBytesInspected.inc(contentItem.getSerializedSize()); response .findInitializationErrors() .forEach( error -> { c.output(apiResponseFailedElements, error.toString()); }); } } } catch (Exception e) { c.output(apiResponseFailedElements, e.toString()); } }