Search in sources :

Example 11 with Subscriber

use of com.google.cloud.pubsub.v1.Subscriber 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());
    }
}
Also used : Arrays(java.util.Arrays) TimeoutException(java.util.concurrent.TimeoutException) Subscriber(com.google.cloud.pubsub.v1.Subscriber) DefaultParser(org.apache.commons.cli.DefaultParser) KMapEstimationHistogramBucket(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KMapEstimationResult.KMapEstimationHistogramBucket) LDiversityEquivalenceClass(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.LDiversityResult.LDiversityEquivalenceClass) ValueFrequency(com.google.privacy.dlp.v2.ValueFrequency) LDiversityConfig(com.google.privacy.dlp.v2.PrivacyMetric.LDiversityConfig) NumericalStatsConfig(com.google.privacy.dlp.v2.PrivacyMetric.NumericalStatsConfig) Action(com.google.privacy.dlp.v2.Action) KMapEstimationConfig(com.google.privacy.dlp.v2.PrivacyMetric.KMapEstimationConfig) CategoricalStatsHistogramBucket(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.CategoricalStatsResult.CategoricalStatsHistogramBucket) KAnonymityEquivalenceClass(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KAnonymityResult.KAnonymityEquivalenceClass) Value(com.google.privacy.dlp.v2.Value) TaggedField(com.google.privacy.dlp.v2.PrivacyMetric.KMapEstimationConfig.TaggedField) RiskAnalysisJobConfig(com.google.privacy.dlp.v2.RiskAnalysisJobConfig) Collectors(java.util.stream.Collectors) SettableApiFuture(com.google.api.core.SettableApiFuture) List(java.util.List) KAnonymityResult(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KAnonymityResult) ParseException(org.apache.commons.cli.ParseException) BigQueryTable(com.google.privacy.dlp.v2.BigQueryTable) ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) AnalyzeDataSourceRiskDetails(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails) Options(org.apache.commons.cli.Options) HelpFormatter(org.apache.commons.cli.HelpFormatter) CategoricalStatsConfig(com.google.privacy.dlp.v2.PrivacyMetric.CategoricalStatsConfig) ArrayList(java.util.ArrayList) ServiceOptions(com.google.cloud.ServiceOptions) CommandLine(org.apache.commons.cli.CommandLine) FieldId(com.google.privacy.dlp.v2.FieldId) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) Option(org.apache.commons.cli.Option) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) Iterator(java.util.Iterator) CreateDlpJobRequest(com.google.privacy.dlp.v2.CreateDlpJobRequest) CommandLineParser(org.apache.commons.cli.CommandLineParser) KMapEstimationResult(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KMapEstimationResult) KMapEstimationQuasiIdValues(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KMapEstimationResult.KMapEstimationQuasiIdValues) InfoType(com.google.privacy.dlp.v2.InfoType) LDiversityResult(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.LDiversityResult) KAnonymityConfig(com.google.privacy.dlp.v2.PrivacyMetric.KAnonymityConfig) TimeUnit(java.util.concurrent.TimeUnit) PublishToPubSub(com.google.privacy.dlp.v2.Action.PublishToPubSub) ProjectName(com.google.privacy.dlp.v2.ProjectName) GetDlpJobRequest(com.google.privacy.dlp.v2.GetDlpJobRequest) LDiversityHistogramBucket(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.LDiversityResult.LDiversityHistogramBucket) KAnonymityHistogramBucket(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KAnonymityResult.KAnonymityHistogramBucket) PrivacyMetric(com.google.privacy.dlp.v2.PrivacyMetric) OptionGroup(org.apache.commons.cli.OptionGroup) DlpJob(com.google.privacy.dlp.v2.DlpJob) Collections(java.util.Collections) Action(com.google.privacy.dlp.v2.Action) ArrayList(java.util.ArrayList) TaggedField(com.google.privacy.dlp.v2.PrivacyMetric.KMapEstimationConfig.TaggedField) PrivacyMetric(com.google.privacy.dlp.v2.PrivacyMetric) PublishToPubSub(com.google.privacy.dlp.v2.Action.PublishToPubSub) Subscriber(com.google.cloud.pubsub.v1.Subscriber) AnalyzeDataSourceRiskDetails(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails) InfoType(com.google.privacy.dlp.v2.InfoType) TimeoutException(java.util.concurrent.TimeoutException) KMapEstimationQuasiIdValues(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KMapEstimationResult.KMapEstimationQuasiIdValues) KMapEstimationConfig(com.google.privacy.dlp.v2.PrivacyMetric.KMapEstimationConfig) RiskAnalysisJobConfig(com.google.privacy.dlp.v2.RiskAnalysisJobConfig) CreateDlpJobRequest(com.google.privacy.dlp.v2.CreateDlpJobRequest) KMapEstimationHistogramBucket(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KMapEstimationResult.KMapEstimationHistogramBucket) TimeoutException(java.util.concurrent.TimeoutException) ParseException(org.apache.commons.cli.ParseException) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) BigQueryTable(com.google.privacy.dlp.v2.BigQueryTable) DlpJob(com.google.privacy.dlp.v2.DlpJob) KMapEstimationResult(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KMapEstimationResult)

Example 12 with Subscriber

use of com.google.cloud.pubsub.v1.Subscriber in project java-docs-samples by GoogleCloudPlatform.

the class Samples method pubSub.

// [END occurrences_for_image]
// [START pubsub]
/**
 * Handle incoming occurrences using a pubsub subscription
 * @param subscriptionId the user-specified identifier for the pubsub subscription
 * @param timeout the amount of time to listen for pubsub messages (in seconds)
 * @param projectId the project's unique identifier
 * @return number of occurrence pubsub messages received
 * @throws Exception on errors with the subscription client
 */
public static int pubSub(String subscriptionId, int timeout, String projectId) throws Exception {
    Subscriber subscriber = null;
    MessageReceiverExample receiver = new MessageReceiverExample();
    try {
        // subscribe to the requested pubsub channel
        ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
        subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
        subscriber.startAsync().awaitRunning();
        // listen to messages for 'listenTimeout' seconds
        for (int i = 0; i < timeout; i++) {
            sleep(1000);
        }
    } finally {
        // stop listening to the channel
        if (subscriber != null) {
            subscriber.stopAsync();
        }
    }
    // print and return the number of pubsub messages received
    System.out.println(receiver.messageCount);
    return receiver.messageCount;
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) Subscriber(com.google.cloud.pubsub.v1.Subscriber)

Example 13 with Subscriber

use of com.google.cloud.pubsub.v1.Subscriber in project java-docs-samples by GoogleCloudPlatform.

the class SamplesTests method testPubSub.

@Test
public void testPubSub() throws Exception {
    int newCount;
    int tries;
    String subscriptionId = "drydockOccurrences";
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(PROJECT_ID, subscriptionId);
    Samples.createOccurrenceSubscription(subscriptionId, PROJECT_ID);
    Subscriber subscriber = null;
    Samples.MessageReceiverExample receiver = new Samples.MessageReceiverExample();
    try {
        subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
        subscriber.startAsync().awaitRunning();
        // sleep so any messages in the queue can go through and be counted before we start the test
        sleep(SLEEP_TIME);
        // set the initial state of our counter
        int startVal = receiver.messageCount + 1;
        // now, we can test adding 3 more occurrences
        int endVal = startVal + 3;
        for (int i = startVal; i <= endVal; i++) {
            Occurrence o = Samples.createOccurrence(imageUrl, noteId, PROJECT_ID);
            System.out.println("CREATED: " + o.getName());
            tries = 0;
            do {
                newCount = receiver.messageCount;
                sleep(SLEEP_TIME);
                tries += 1;
            } while (newCount != i && tries < TRY_LIMIT);
            System.out.println(receiver.messageCount + " : " + i);
            assertEquals(i, receiver.messageCount);
            Samples.deleteOccurrence(o.getName());
        }
    } catch (Exception e) {
        fail("exception thrown");
    } finally {
        if (subscriber != null) {
            subscriber.stopAsync();
        }
        // delete subscription now that we're done with it
        try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
            subscriptionAdminClient.deleteSubscription(subscriptionName);
        }
    }
}
Also used : ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) Subscriber(com.google.cloud.pubsub.v1.Subscriber) SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) Occurrence(com.google.containeranalysis.v1alpha1.Occurrence) Test(org.junit.Test)

Example 14 with Subscriber

use of com.google.cloud.pubsub.v1.Subscriber in project moleculer-java by moleculer-java.

the class GoogleTransporter method subscribe.

// --- SUBSCRIBE ---
@Override
public Promise subscribe(String channel) {
    try {
        // Create publisher
        getOrCreatePublisher(channel);
        // Create subscriber
        synchronized (subscribers) {
            if (!subscribers.containsKey(channel)) {
                Subscriber.Builder builder = Subscriber.newBuilder(SubscriptionName.of(projectID, channel), (message, consumer) -> {
                    // Message received
                    byte[] bytes = message.getData().toByteArray();
                    received(channel, bytes);
                    consumer.ack();
                });
                builder.setChannelProvider(channelProvider);
                builder.setCredentialsProvider(credentialsProvider);
                if (executorProvider != null) {
                    builder.setExecutorProvider(executorProvider);
                } else {
                    builder.setExecutorProvider(new ExecutorProvider() {

                        @Override
                        public final boolean shouldAutoClose() {
                            return false;
                        }

                        @Override
                        public final ScheduledExecutorService getExecutor() {
                            return scheduler;
                        }
                    });
                }
                if (headerProvider != null) {
                    builder.setHeaderProvider(headerProvider);
                }
                if (maxAckExtensionPeriod != null) {
                    builder.setMaxAckExtensionPeriod(maxAckExtensionPeriod);
                }
                if (parallelPullCount > 0) {
                    builder.setParallelPullCount(parallelPullCount);
                }
                if (executorProvider != null) {
                    builder.setSystemExecutorProvider(executorProvider);
                } else {
                    builder.setSystemExecutorProvider(new ExecutorProvider() {

                        @Override
                        public final boolean shouldAutoClose() {
                            return false;
                        }

                        @Override
                        public final ScheduledExecutorService getExecutor() {
                            return scheduler;
                        }
                    });
                }
                Subscriber subscriber = builder.build();
                subscriber.startAsync();
                subscribers.put(channel, subscriber);
            }
        }
    } catch (Exception cause) {
        return Promise.reject(cause);
    }
    return Promise.resolve();
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Subscriber(com.google.cloud.pubsub.v1.Subscriber) ExecutorProvider(com.google.api.gax.core.ExecutorProvider)

Example 15 with Subscriber

use of com.google.cloud.pubsub.v1.Subscriber in project google-cloud-java by GoogleCloudPlatform.

the class SubscriberSnippets method createSubscriber.

private void createSubscriber() throws Exception {
    // [START pubsub_pull]
    String projectId = "my-project-id";
    String subscriptionId = "my-subscription-id";
    SubscriptionName subscriptionName = SubscriptionName.create(projectId, subscriptionId);
    // Instantiate an asynchronous message receiver
    MessageReceiver receiver = new MessageReceiver() {

        @Override
        public void receiveMessage(PubsubMessage message, AckReplyConsumer consumer) {
            // handle incoming message, then ack/nack the received message
            System.out.println("Id : " + message.getMessageId());
            System.out.println("Data : " + message.getData().toStringUtf8());
            consumer.ack();
        }
    };
    Subscriber subscriber = null;
    try {
        // Create a subscriber for "my-subscription-id" bound to the message receiver
        subscriber = Subscriber.defaultBuilder(subscriptionName, receiver).build();
        subscriber.startAsync();
    // ...
    } finally {
        // stop receiving messages
        if (subscriber != null) {
            subscriber.stopAsync();
        }
    }
// [END pubsub_pull]
}
Also used : MessageReceiver(com.google.cloud.pubsub.spi.v1.MessageReceiver) Subscriber(com.google.cloud.pubsub.spi.v1.Subscriber) SubscriptionName(com.google.pubsub.v1.SubscriptionName) AckReplyConsumer(com.google.cloud.pubsub.spi.v1.AckReplyConsumer) PubsubMessage(com.google.pubsub.v1.PubsubMessage)

Aggregations

Subscriber (com.google.cloud.pubsub.v1.Subscriber)15 ProjectSubscriptionName (com.google.pubsub.v1.ProjectSubscriptionName)11 SettableApiFuture (com.google.api.core.SettableApiFuture)8 ServiceOptions (com.google.cloud.ServiceOptions)8 DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)8 Action (com.google.privacy.dlp.v2.Action)8 BigQueryTable (com.google.privacy.dlp.v2.BigQueryTable)8 CreateDlpJobRequest (com.google.privacy.dlp.v2.CreateDlpJobRequest)8 DlpJob (com.google.privacy.dlp.v2.DlpJob)8 GetDlpJobRequest (com.google.privacy.dlp.v2.GetDlpJobRequest)8 InfoType (com.google.privacy.dlp.v2.InfoType)8 ProjectName (com.google.privacy.dlp.v2.ProjectName)8 ProjectTopicName (com.google.pubsub.v1.ProjectTopicName)8 ArrayList (java.util.ArrayList)8 Collections (java.util.Collections)8 List (java.util.List)8 TimeUnit (java.util.concurrent.TimeUnit)8 CommandLine (org.apache.commons.cli.CommandLine)8 CommandLineParser (org.apache.commons.cli.CommandLineParser)8 DefaultParser (org.apache.commons.cli.DefaultParser)8