use of com.google.cloud.dlp.v2.DlpServiceClient in project java-docs-samples by GoogleCloudPlatform.
the class RiskAnalysis method numericalStatsAnalysis.
// [START dlp_numerical_stats]
/**
* Calculate numerical statistics for a column in a BigQuery table using the DLP API.
*
* @param projectId The Google Cloud Platform project ID to run the API call under.
* @param datasetId The BigQuery dataset to analyze.
* @param tableId The BigQuery table to analyze.
* @param columnName The name of the column to analyze, which must contain only numerical data.
* @param topicId The name of the Pub/Sub topic to notify once the job completes
* @param subscriptionId The name of the Pub/Sub subscription to use when listening for job
* completion status.
*/
private static void numericalStatsAnalysis(String projectId, String datasetId, String tableId, String columnName, String topicId, String subscriptionId) throws Exception {
// Instantiates a client
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
BigQueryTable bigQueryTable = BigQueryTable.newBuilder().setTableId(tableId).setDatasetId(datasetId).setProjectId(projectId).build();
FieldId fieldId = FieldId.newBuilder().setName(columnName).build();
NumericalStatsConfig numericalStatsConfig = NumericalStatsConfig.newBuilder().setField(fieldId).build();
PrivacyMetric privacyMetric = PrivacyMetric.newBuilder().setNumericalStatsConfig(numericalStatsConfig).build();
String topicName = String.format("projects/%s/topics/%s", projectId, topicId);
PublishToPubSub publishToPubSub = PublishToPubSub.newBuilder().setTopic(topicName).build();
// Create action to publish job status notifications over Google Cloud Pub/Sub
Action action = Action.newBuilder().setPubSub(publishToPubSub).build();
RiskAnalysisJobConfig riskAnalysisJobConfig = RiskAnalysisJobConfig.newBuilder().setSourceTable(bigQueryTable).setPrivacyMetric(privacyMetric).addActions(action).build();
CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setRiskJob(riskAnalysisJobConfig).build();
DlpJob dlpJob = dlpServiceClient.createDlpJob(createDlpJobRequest);
String dlpJobName = dlpJob.getName();
final SettableApiFuture<Boolean> done = SettableApiFuture.create();
// Set up a Pub/Sub subscriber to listen on the job completion status
Subscriber subscriber = Subscriber.newBuilder(ProjectSubscriptionName.newBuilder().setProject(projectId).setSubscription(subscriptionId).build(), (pubsubMessage, ackReplyConsumer) -> {
if (pubsubMessage.getAttributesCount() > 0 && pubsubMessage.getAttributesMap().get("DlpJobName").equals(dlpJobName)) {
// notify job completion
done.set(true);
ackReplyConsumer.ack();
}
}).build();
subscriber.startAsync();
// For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
try {
done.get(1, TimeUnit.MINUTES);
// Wait for the job to become available
Thread.sleep(500);
} catch (TimeoutException e) {
System.out.println("Unable to verify job completion.");
}
// Retrieve completed job status
DlpJob completedJob = dlpServiceClient.getDlpJob(GetDlpJobRequest.newBuilder().setName(dlpJobName).build());
System.out.println("Job status: " + completedJob.getState());
AnalyzeDataSourceRiskDetails riskDetails = completedJob.getRiskDetails();
AnalyzeDataSourceRiskDetails.NumericalStatsResult result = riskDetails.getNumericalStatsResult();
System.out.printf("Value range : [%.3f, %.3f]\n", result.getMinValue().getFloatValue(), result.getMaxValue().getFloatValue());
int percent = 1;
Double lastValue = null;
for (Value quantileValue : result.getQuantileValuesList()) {
Double currentValue = quantileValue.getFloatValue();
if (lastValue == null || !lastValue.equals(currentValue)) {
System.out.printf("Value at %s %% quantile : %.3f", percent, currentValue);
}
lastValue = currentValue;
}
} catch (Exception e) {
System.out.println("Error in categoricalStatsAnalysis: " + e.getMessage());
}
}
use of com.google.cloud.dlp.v2.DlpServiceClient in project java-docs-samples by GoogleCloudPlatform.
the class Templates method deleteInspectTemplate.
// [END dlp_list_inspect_templates]
// [START dlp_delete_inspect_template]
/**
* Delete the DLP inspection configuration template with the specified name.
*
* @param projectId Google Cloud Project ID
* @param templateId Template ID to be deleted
*/
private static void deleteInspectTemplate(String projectId, String templateId) {
// construct the template name to be deleted
String templateName = String.format("projects/%s/inspectTemplates/%s", projectId, templateId);
// instantiate the client
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
// create delete template request
DeleteInspectTemplateRequest request = DeleteInspectTemplateRequest.newBuilder().setName(templateName).build();
dlpServiceClient.deleteInspectTemplate(request);
System.out.printf("Deleted template: %s\n", templateName);
} catch (Exception e) {
System.err.printf("Error deleting template: %s\n", templateName);
}
}
use of com.google.cloud.dlp.v2.DlpServiceClient in project java-docs-samples by GoogleCloudPlatform.
the class Triggers method createTrigger.
// [START dlp_create_trigger]
/**
* Schedule a DLP inspection trigger for a GCS location.
*
* @param triggerId (Optional) name of the trigger to be created
* @param displayName (Optional) display name for the trigger to be created
* @param description (Optional) description for the trigger to be created
* @param scanPeriod How often to wait between scans, in days (minimum = 1 day)
* @param infoTypes infoTypes of information to match eg. InfoType.PHONE_NUMBER,
* InfoType.EMAIL_ADDRESS
* @param minLikelihood minimum likelihood required before returning a match
* @param maxFindings maximum number of findings to report per request (0 = server maximum)
* @param projectId The project ID to run the API call under
*/
private static void createTrigger(String triggerId, String displayName, String description, String bucketName, String fileName, int scanPeriod, List<InfoType> infoTypes, Likelihood minLikelihood, int maxFindings, String projectId) throws Exception {
// instantiate a client
DlpServiceClient dlpServiceClient = DlpServiceClient.create();
try {
CloudStorageOptions cloudStorageOptions = CloudStorageOptions.newBuilder().setFileSet(CloudStorageOptions.FileSet.newBuilder().setUrl("gs://" + bucketName + "/" + fileName)).build();
StorageConfig storageConfig = StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).build();
InspectConfig.FindingLimits findingLimits = InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).build();
InspectJobConfig inspectJobConfig = InspectJobConfig.newBuilder().setInspectConfig(inspectConfig).setStorageConfig(storageConfig).build();
// Schedule scan of GCS bucket every scanPeriod number of days (minimum = 1 day)
Duration duration = Duration.newBuilder().setSeconds(scanPeriod * 24 * 3600).build();
Schedule schedule = Schedule.newBuilder().setRecurrencePeriodDuration(duration).build();
JobTrigger.Trigger trigger = JobTrigger.Trigger.newBuilder().setSchedule(schedule).build();
JobTrigger jobTrigger = JobTrigger.newBuilder().setInspectJob(inspectJobConfig).setName(triggerId).setDisplayName(displayName).setDescription(description).setStatus(JobTrigger.Status.HEALTHY).addTriggers(trigger).build();
// Create scan request
CreateJobTriggerRequest createJobTriggerRequest = CreateJobTriggerRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setJobTrigger(jobTrigger).build();
JobTrigger createdJobTrigger = dlpServiceClient.createJobTrigger(createJobTriggerRequest);
System.out.println("Created Trigger: " + createdJobTrigger.getName());
} catch (Exception e) {
System.out.println("Error creating trigger: " + e.getMessage());
}
}
use of com.google.cloud.dlp.v2.DlpServiceClient in project java-docs-samples by GoogleCloudPlatform.
the class Triggers method deleteTrigger.
// [END dlp_list_triggers]
// [START dlp_delete_trigger]
/**
* Delete a DLP trigger in a project.
*
* @param projectId The project ID to run the API call under.
* @param triggerId Trigger ID
*/
private static void deleteTrigger(String projectId, String triggerId) {
ProjectJobTriggerName triggerName = ProjectJobTriggerName.of(projectId, triggerId);
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
DeleteJobTriggerRequest deleteJobTriggerRequest = DeleteJobTriggerRequest.newBuilder().setName(triggerName.toString()).build();
dlpServiceClient.deleteJobTrigger(deleteJobTriggerRequest);
System.out.println("Trigger deleted: " + triggerName.toString());
} catch (Exception e) {
System.out.println("Error deleting trigger :" + e.getMessage());
}
}
use of com.google.cloud.dlp.v2.DlpServiceClient in project java-docs-samples by GoogleCloudPlatform.
the class Triggers method listTriggers.
// [END dlp_create_trigger]
// [START dlp_list_triggers]
/**
* List all DLP triggers for a given project.
*
* @param projectId The project ID to run the API call under.
*/
private static void listTriggers(String projectId) {
// Instantiates a client
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
ListJobTriggersRequest listJobTriggersRequest = ListJobTriggersRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).build();
DlpServiceClient.ListJobTriggersPagedResponse response = dlpServiceClient.listJobTriggers(listJobTriggersRequest);
response.getPage().getValues().forEach(trigger -> {
System.out.println("Trigger: " + trigger.getName());
System.out.println("\tCreated: " + trigger.getCreateTime());
System.out.println("\tUpdated: " + trigger.getUpdateTime());
if (trigger.getDisplayName() != null) {
System.out.println("\tDisplay name: " + trigger.getDisplayName());
}
if (trigger.getDescription() != null) {
System.out.println("\tDescription: " + trigger.getDescription());
}
System.out.println("\tStatus: " + trigger.getStatus());
System.out.println("\tError count: " + trigger.getErrorsCount());
});
} catch (Exception e) {
System.out.println("Error listing triggers :" + e.getMessage());
}
}
Aggregations