use of com.google.cloud.pubsub.v1.AckReplyConsumer in project java-docs-samples by GoogleCloudPlatform.
the class RiskAnalysis method calculateLDiversity.
// [END dlp_k_anonymity]
// [START dlp_l_diversity]
/**
* Calculate l-diversity for an attribute relative to quasi-identifiers in a BigQuery table.
*
* @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 sensitiveAttribute The name of the attribute to compare the quasi-ID against
* @param quasiIds A set of column names that form a composite key ('quasi-identifiers').
* @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 calculateLDiversity(String projectId, String datasetId, String tableId, String sensitiveAttribute, List<String> quasiIds, String topicId, String subscriptionId) throws Exception {
// Instantiates a client
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
FieldId sensitiveAttributeField = FieldId.newBuilder().setName(sensitiveAttribute).build();
List<FieldId> quasiIdFields = quasiIds.stream().map(columnName -> FieldId.newBuilder().setName(columnName).build()).collect(Collectors.toList());
LDiversityConfig ldiversityConfig = LDiversityConfig.newBuilder().addAllQuasiIds(quasiIdFields).setSensitiveAttribute(sensitiveAttributeField).build();
BigQueryTable bigQueryTable = BigQueryTable.newBuilder().setProjectId(projectId).setDatasetId(datasetId).setTableId(tableId).build();
PrivacyMetric privacyMetric = PrivacyMetric.newBuilder().setLDiversityConfig(ldiversityConfig).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();
LDiversityResult ldiversityResult = riskDetails.getLDiversityResult();
for (LDiversityHistogramBucket result : ldiversityResult.getSensitiveValueFrequencyHistogramBucketsList()) {
for (LDiversityEquivalenceClass bucket : result.getBucketValuesList()) {
List<String> quasiIdValues = bucket.getQuasiIdsValuesList().stream().map(Value::toString).collect(Collectors.toList());
System.out.println("\tQuasi-ID values: " + String.join(", ", quasiIdValues));
System.out.println("\tClass size: " + bucket.getEquivalenceClassSize());
for (ValueFrequency valueFrequency : bucket.getTopSensitiveValuesList()) {
System.out.printf("\t\tSensitive value %s occurs %d time(s).\n", valueFrequency.getValue().toString(), valueFrequency.getCount());
}
}
}
} catch (Exception e) {
System.out.println("Error in calculateLDiversity: " + e.getMessage());
}
}
use of com.google.cloud.pubsub.v1.AckReplyConsumer in project java-docs-samples by GoogleCloudPlatform.
the class RiskAnalysis method calculateKMap.
// [END dlp_l_diversity]
// [START dlp_k_map]
/**
* Calculate k-map risk estimation for an attribute relative to quasi-identifiers in a BigQuery
* table.
*
* @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 quasiIds A set of column names that form a composite key ('quasi-identifiers').
* @param infoTypes The infoTypes corresponding to each quasi-id column
* @param regionCode An ISO-3166-1 region code specifying the k-map distribution region
* @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 calculateKMap(String projectId, String datasetId, String tableId, List<String> quasiIds, List<InfoType> infoTypes, String regionCode, String topicId, String subscriptionId) throws Exception {
// Instantiates a client
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
Iterator<String> quasiIdsIterator = quasiIds.iterator();
Iterator<InfoType> infoTypesIterator = infoTypes.iterator();
if (quasiIds.size() != infoTypes.size()) {
throw new IllegalArgumentException("The numbers of quasi-IDs and infoTypes must be equal!");
}
ArrayList<TaggedField> taggedFields = new ArrayList();
while (quasiIdsIterator.hasNext() || infoTypesIterator.hasNext()) {
taggedFields.add(TaggedField.newBuilder().setField(FieldId.newBuilder().setName(quasiIdsIterator.next()).build()).setInfoType(infoTypesIterator.next()).build());
}
KMapEstimationConfig kmapConfig = KMapEstimationConfig.newBuilder().addAllQuasiIds(taggedFields).setRegionCode(regionCode).build();
BigQueryTable bigQueryTable = BigQueryTable.newBuilder().setProjectId(projectId).setDatasetId(datasetId).setTableId(tableId).build();
PrivacyMetric privacyMetric = PrivacyMetric.newBuilder().setKMapEstimationConfig(kmapConfig).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();
KMapEstimationResult kmapResult = riskDetails.getKMapEstimationResult();
for (KMapEstimationHistogramBucket result : kmapResult.getKMapEstimationHistogramList()) {
System.out.printf("\tAnonymity range: [%d, %d]\n", result.getMinAnonymity(), result.getMaxAnonymity());
System.out.printf("\tSize: %d\n", result.getBucketSize());
for (KMapEstimationQuasiIdValues valueBucket : result.getBucketValuesList()) {
String quasiIdValues = valueBucket.getQuasiIdsValuesList().stream().map(v -> {
String s = v.toString();
return s.substring(s.indexOf(':') + 1).trim();
}).collect(Collectors.joining(", "));
System.out.printf("\tValues: {%s}\n", quasiIdValues);
System.out.printf("\tEstimated k-map anonymity: %d\n", valueBucket.getEstimatedAnonymity());
}
}
} catch (Exception e) {
System.out.println("Error in calculateKMap: " + e.getMessage());
}
}
use of com.google.cloud.pubsub.v1.AckReplyConsumer in project beam by apache.
the class TestPubsubSignal method pollForResultForDuration.
private String pollForResultForDuration(SubscriptionPath signalSubscriptionPath, Duration timeoutDuration) throws IOException {
AtomicReference<String> result = new AtomicReference<>(null);
MessageReceiver receiver = (com.google.pubsub.v1.PubsubMessage message, AckReplyConsumer replyConsumer) -> {
LOG.info("Received message: {}", message.getData().toStringUtf8());
// Ignore empty messages
if (message.getData().isEmpty()) {
replyConsumer.ack();
}
if (result.compareAndSet(null, message.getData().toStringUtf8())) {
replyConsumer.ack();
} else {
replyConsumer.nack();
}
};
Subscriber subscriber = Subscriber.newBuilder(signalSubscriptionPath.getPath(), receiver).setCredentialsProvider(pipelineOptions::getGcpCredential).setEndpoint(pubsubEndpoint).build();
subscriber.startAsync();
DateTime startTime = new DateTime();
int timeoutSeconds = timeoutDuration.toStandardSeconds().getSeconds();
while (result.get() == null && Seconds.secondsBetween(startTime, new DateTime()).getSeconds() < timeoutSeconds) {
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
}
subscriber.stopAsync();
subscriber.awaitTerminated();
if (result.get() == null) {
throw new AssertionError(String.format("Did not receive signal on %s in %ss", signalSubscriptionPath, timeoutDuration.getStandardSeconds()));
}
return result.get();
}
use of com.google.cloud.pubsub.v1.AckReplyConsumer in project spring-cloud-gcp by spring-cloud.
the class PubSubSubscriberTemplate method subscribe.
@Override
public Subscriber subscribe(String subscription, Consumer<BasicAcknowledgeablePubsubMessage> messageConsumer) {
Assert.notNull(messageConsumer, "The messageConsumer can't be null.");
Subscriber subscriber = this.subscriberFactory.createSubscriber(subscription, (message, ackReplyConsumer) -> messageConsumer.accept(new PushedAcknowledgeablePubsubMessage(PubSubSubscriptionUtils.toProjectSubscriptionName(subscription, this.subscriberFactory.getProjectId()), message, ackReplyConsumer)));
subscriber.startAsync();
return subscriber;
}
use of com.google.cloud.pubsub.v1.AckReplyConsumer in project spring-cloud-gcp by spring-cloud.
the class PubSubSubscriberTemplate method subscribeAndConvert.
@Override
public <T> Subscriber subscribeAndConvert(String subscription, Consumer<ConvertedBasicAcknowledgeablePubsubMessage<T>> messageConsumer, Class<T> payloadType) {
Assert.notNull(messageConsumer, "The messageConsumer can't be null.");
Subscriber subscriber = this.subscriberFactory.createSubscriber(subscription, (message, ackReplyConsumer) -> messageConsumer.accept(new ConvertedPushedAcknowledgeablePubsubMessage<>(PubSubSubscriptionUtils.toProjectSubscriptionName(subscription, this.subscriberFactory.getProjectId()), message, this.getMessageConverter().fromPubSubMessage(message, payloadType), ackReplyConsumer)));
subscriber.startAsync();
return subscriber;
}
Aggregations