Java Code Examples for com.google.pubsub.v1.ProjectSubscriptionName#of()

The following examples show how to use com.google.pubsub.v1.ProjectSubscriptionName#of() . 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: PubsubIntegrationTest.java    From gcp-ingestion with Mozilla Public License 2.0 7 votes vote down vote up
private List<String> receiveLines(int expectedMessageCount) throws Exception {
  List<String> received = new CopyOnWriteArrayList<>();
  ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId,
      subscriptionId);

  MessageReceiver receiver = ((PubsubMessage message, AckReplyConsumer consumer) -> {
    try {
      String encoded = Json.asString(new org.apache.beam.sdk.io.gcp.pubsub.PubsubMessage(
          message.getData().toByteArray(), message.getAttributesMap()));
      received.add(encoded);
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
    consumer.ack();
  });
  Subscriber subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
  subscriber.startAsync();
  while (received.size() < expectedMessageCount) {
    Thread.sleep(100);
  }
  subscriber.stopAsync();

  return received;
}
 
Example 2
Source File: PubSubConnector.java    From smallrye-reactive-messaging with Apache License 2.0 6 votes vote down vote up
private void createSubscription(final PubSubConfig config) {
    final SubscriptionAdminClient subscriptionAdminClient = pubSubManager.subscriptionAdminClient(config);

    final ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(config.getProjectId(),
            config.getSubscription());

    try {
        subscriptionAdminClient.getSubscription(subscriptionName);
    } catch (final NotFoundException e) {
        final PushConfig pushConfig = PushConfig.newBuilder()
                .build();

        final ProjectTopicName topicName = ProjectTopicName.of(config.getProjectId(), config.getTopic());

        subscriptionAdminClient.createSubscription(subscriptionName, topicName, pushConfig, 0);
    }
}
 
Example 3
Source File: PubSubSubscriptionUtils.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
/**
 * Create a {@link ProjectSubscriptionName} based on a subscription name within a project or the
 * fully-qualified subscription name. If the specified subscription is in the
 * {@code projects/<project_name>/subscriptions/<subscription_name>} format, then the {@code projectId} is
 * ignored}
 * @param subscription the subscription name in the project or the fully-qualified project name
 * @param projectId the project ID to use if the subscription is not a fully-qualified name
 * @return the Pub/Sub object representing the subscription name
 */
public static ProjectSubscriptionName toProjectSubscriptionName(String subscription, @Nullable String projectId) {
	Assert.notNull(subscription, "The subscription can't be null.");

	ProjectSubscriptionName projectSubscriptionName = null;

	if (ProjectSubscriptionName.isParsableFrom(subscription)) {
		// Fully-qualified subscription name in the "projects/<project_name>/subscriptions/<subscription_name>" format
		projectSubscriptionName = ProjectSubscriptionName.parse(subscription);
	}
	else {
		Assert.notNull(projectId, "The project ID can't be null when using canonical subscription name.");
		projectSubscriptionName = ProjectSubscriptionName.of(projectId, subscription);
	}

	return projectSubscriptionName;
}
 
Example 4
Source File: CPSSubscriberTask.java    From pubsub with Apache License 2.0 6 votes vote down vote up
private CPSSubscriberTask(StartRequest request, MetricsHandler metricsHandler, int workerCount) {
  this.metricsHandler = metricsHandler;
  ProjectSubscriptionName subscription =
      ProjectSubscriptionName.of(
          request.getProject(), request.getPubsubOptions().getSubscription());
  try {
    this.subscriber =
        Subscriber.newBuilder(subscription, this)
            .setParallelPullCount(workerCount)
            .setFlowControlSettings(
                FlowControlSettings.newBuilder()
                    .setMaxOutstandingElementCount(Long.MAX_VALUE)
                    .setMaxOutstandingRequestBytes(BYTES_PER_WORKER * workerCount)
                    .build())
            .build();
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example 5
Source File: Subscriptions.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public static int pubSub(String subId, long timeoutSeconds, String projectId)
    throws InterruptedException {
  // String subId = "my-occurrence-subscription";
  // long timeoutSeconds = 20;
  // String projectId = "my-project-id";
  Subscriber subscriber = null;
  MessageReceiverExample receiver = new MessageReceiverExample();

  try {
    // Subscribe to the requested Pub/Sub channel
    ProjectSubscriptionName subName = ProjectSubscriptionName.of(projectId, subId);
    subscriber = Subscriber.newBuilder(subName, receiver).build();
    subscriber.startAsync().awaitRunning();
    // Sleep to listen for messages
    TimeUnit.SECONDS.sleep(timeoutSeconds);
  } finally {
    // Stop listening to the channel
    if (subscriber != null) {
      subscriber.stopAsync();
    }
  }
  // Print and return the number of Pub/Sub messages received
  System.out.println(receiver.messageCount);
  return receiver.messageCount;
}
 
Example 6
Source File: PubSubManager.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private static Subscriber buildSubscriber(final PubSubConfig config, final PubSubMessageReceiver messageReceiver) {
    final ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(config.getProjectId(),
            config.getSubscription());

    final Subscriber.Builder subscriberBuilder = Subscriber.newBuilder(subscriptionName, messageReceiver);

    buildCredentialsProvider(config).ifPresent(subscriberBuilder::setCredentialsProvider);
    buildTransportChannelProvider(config).ifPresent(subscriberBuilder::setChannelProvider);

    return subscriberBuilder.build();
}
 
Example 7
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public Subscriber subscribeToSubscription(String project, String subscription, MessageReceiver messageReceiver) {
	ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(project, subscription);
	Subscriber subscriber =
		Subscriber
			.newBuilder(subscriptionName, messageReceiver)
			.setChannelProvider(channelProvider)
			.setCredentialsProvider(NoCredentialsProvider.create())
			.build();
	subscriber.startAsync();
	return subscriber;
}
 
Example 8
Source File: PubsubBenchWrapperImpl.java    From google-cloud-java with Apache License 2.0 5 votes vote down vote up
public void recv(PubsubRecv request, StreamObserver<EmptyResponse> responseObserver) {
  System.out.println("recv has been called");

  ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(
      "some-project", request.getSubName());
  
  Subscriber subscriber = null;
  try {
    InstantiatingExecutorProvider executorProvider =
      InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(1).build();

    subscriber =
        Subscriber.newBuilder(subscriptionName, new SimpleReceiver())
            .setExecutorProvider(executorProvider)
            .build();
    subscriber.startAsync().awaitRunning();

    // Allow the subscriber to run indefinitely unless an unrecoverable error occurs.
    subscriber.awaitTerminated();
  } catch (IllegalStateException e) {
    System.out.println("Subscriber unexpectedly stopped: " + e);
  }

  EmptyResponse reply = EmptyResponse.newBuilder().build();
  responseObserver.onNext(reply);
  responseObserver.onCompleted();
}
 
Example 9
Source File: SamplesTest.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void tearDownClass() {
  try {
    SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create();
    ProjectSubscriptionName subName = ProjectSubscriptionName.of(PROJECT_ID, subId);
    subscriptionAdminClient.deleteSubscription(subName);
    subscriptionAdminClient.shutdownNow();
  } catch (Exception e) {
    // these exceptions aren't relevant to the tests
    System.out.println("TearDownClass Error: " + e.toString());
  }
}
 
Example 10
Source File: Subscriptions.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static Subscription createOccurrenceSubscription(String subId, String projectId) 
    throws IOException, StatusRuntimeException, InterruptedException {
  // This topic id will automatically receive messages when Occurrences are added or modified
  String topicId = "container-analysis-occurrences-v1";
  ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
  ProjectSubscriptionName subName = ProjectSubscriptionName.of(projectId, subId);

  SubscriptionAdminClient client = SubscriptionAdminClient.create();
  PushConfig config = PushConfig.getDefaultInstance();
  Subscription sub = client.createSubscription(subName, topicName, config, 0);
  return sub;
}
 
Example 11
Source File: Connection.java    From heroic with Apache License 2.0 5 votes vote down vote up
Connection(
    ConsumerSchema.Consumer consumer,
    ConsumerReporter reporter,
    AtomicLong errors,
    LongAdder consumed,
    String projectId,
    String topicId,
    String subscriptionId,
    int threads,
    long maxOutstandingElementCount,
    long maxOutstandingRequestBytes,
    int maxInboundMessageSize,
    long keepAlive
) {
    this.consumer = consumer;
    this.reporter = reporter;
    this.errors = errors;
    this.consumed = consumed;
    this.projectId = projectId;
    this.subscriptionId = subscriptionId;
    this.threads = threads;
    this.topicName = ProjectTopicName.of(projectId, topicId);
    this.subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
    this.credentialsProvider = SubscriptionAdminSettings
        .defaultCredentialsProviderBuilder()
        .build();
    this.channelProvider = SubscriptionAdminSettings
        .defaultGrpcTransportProviderBuilder()
        .setMaxInboundMessageSize(maxInboundMessageSize)
        .setKeepAliveTime(Duration.ofSeconds(keepAlive))
        .build();
    this.maxOutstandingElementCount = maxOutstandingElementCount;
    this.maxOutstandingRequestBytes = maxOutstandingRequestBytes;
}
 
Example 12
Source File: EmulatorHelper.java    From heroic with Apache License 2.0 5 votes vote down vote up
public static void deleteSubscription(
    String projectId, String subscriptionId, String endpoint
) throws IOException {
    SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create(
        SubscriptionAdminSettings.newBuilder()
            .setTransportChannelProvider(channelProvider(endpoint))
            .setCredentialsProvider(credentialsProvider)
            .build()
    );
    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
    subscriptionAdminClient.deleteSubscription(subscriptionName);
}
 
Example 13
Source File: PubsubUtils.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
public static ProjectTopicName getPubSubTopicFromSubscription(
    String gcpProject, String subscription) throws IOException {
  setupSubscriptionClient();

  ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(gcpProject, subscription);

  LOG.info("Retrieving information about subscription {}", subscriptionName);
  Subscription subscriptionEntity = subscriptionAdminClient.getSubscription(subscriptionName);

  ProjectTopicName result = ProjectTopicName.parse(subscriptionEntity.getTopic());
  LOG.info("ProjectTopicName is {} with topic {}", result, result.getTopic());
  return result;
}
 
Example 14
Source File: PubsubIntegrationTest.java    From gcp-ingestion with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Create a Pub/Sub topic and subscription.
 *
 * @throws IOException if Pub/Sub is unavailable
 */
@Before
public void initializePubsubResources() throws IOException {
  projectId = ServiceOptions.getDefaultProjectId();
  topicId = "test-topic-" + UUID.randomUUID().toString();
  subscriptionId = "test-subscription-" + UUID.randomUUID().toString();
  topicName = ProjectTopicName.of(projectId, topicId);
  subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);

  TopicAdminClient.create().createTopic(topicName);
  SubscriptionAdminClient.create().createSubscription(subscriptionName, topicName,
      PushConfig.getDefaultInstance(), 0);
}
 
Example 15
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public Subscriber subscribeToSubscription(String project, String subscription, MessageReceiver messageReceiver) {
	ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(project, subscription);
	Subscriber subscriber =
		Subscriber
			.newBuilder(subscriptionName, messageReceiver)
			.setChannelProvider(channelProvider)
			.setCredentialsProvider(NoCredentialsProvider.create())
			.build();
	subscriber.startAsync();
	return subscriber;
}
 
Example 16
Source File: Connection.java    From heroic with Apache License 2.0 4 votes vote down vote up
Connection start() {
    log.info("Starting PubSub connection");

    ProjectSubscriptionName subscriptionName = ProjectSubscriptionName
        .of(projectId, subscriptionId);

    FlowControlSettings flowControlSettings =
        FlowControlSettings.newBuilder()
            .setMaxOutstandingElementCount(maxOutstandingElementCount)
            .setMaxOutstandingRequestBytes(maxOutstandingRequestBytes)
            .build();

    ExecutorProvider executorProvider =
        InstantiatingExecutorProvider.newBuilder().setExecutorThreadCount(threads).build();


    log.info("Subscribing to {}", subscriptionName);
    final Receiver receiver = new Receiver(consumer, reporter, errors, consumed);
    subscriber = Subscriber
        .newBuilder(subscriptionName, receiver)
        .setFlowControlSettings(flowControlSettings)
        .setParallelPullCount(threads)
        .setExecutorProvider(executorProvider)
        .setChannelProvider(channelProvider)
        .setCredentialsProvider(credentialsProvider)
        .build();

    subscriber.addListener(
      new Subscriber.Listener() {
          @Override
          public void failed(Subscriber.State from, Throwable failure) {
              // Called when the Subscriber encountered a fatal error and is shutting down
              log.error(
                "An error on subscriber happened (from state: " + from.name() + ")", failure);
              System.exit(1);
          } }, MoreExecutors.directExecutor());


    subscriber.startAsync().awaitRunning();

    log.info("PubSub connection started");
    return this;
}
 
Example 17
Source File: InspectGcsFile.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectGcsFile(
    String projectId, String gcsUri, String topicId, String subscriptionId)
    throws ExecutionException, InterruptedException, 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 GCS file to be inspected.
    CloudStorageOptions cloudStorageOptions =
        CloudStorageOptions.newBuilder()
            .setFileSet(FileSet.newBuilder().setUrl(gcsUri))
            .build();

    StorageConfig storageConfig =
        StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).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 =
        Stream.of("PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER")
            .map(it -> InfoType.newBuilder().setName(it).build())
            .collect(Collectors.toList());

    // Specify how the content should be inspected.
    InspectConfig inspectConfig =
        InspectConfig.newBuilder()
            .addAllInfoTypes(infoTypes)
            .setIncludeQuote(true)
            .build();

    // Specify the action that is triggered when the job completes.
    String pubSubTopic = String.format("projects/%s/topics/%s", projectId, topicId);
    Action.PublishToPubSub publishToPubSub =
        Action.PublishToPubSub.newBuilder().setTopic(pubSubTopic).build();
    Action action = Action.newBuilder().setPubSub(publishToPubSub).build();

    // Configure the long running job we want the service to perform.
    InspectJobConfig inspectJobConfig =
        InspectJobConfig.newBuilder()
            .setStorageConfig(storageConfig)
            .setInspectConfig(inspectConfig)
            .addActions(action)
            .build();

    // Create the request for the job configured above.
    CreateDlpJobRequest createDlpJobRequest =
        CreateDlpJobRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setInspectJob(inspectJobConfig)
            .build();

    // Use the client to send the request.
    final DlpJob dlpJob = dlp.createDlpJob(createDlpJobRequest);
    System.out.println("Job created: " + dlpJob.getName());

    // Set up a Pub/Sub subscriber to listen on the job completion status
    final SettableApiFuture<Boolean> done = SettableApiFuture.create();

    ProjectSubscriptionName subscriptionName =
        ProjectSubscriptionName.of(projectId, subscriptionId);

    MessageReceiver messageHandler =
        (PubsubMessage pubsubMessage, AckReplyConsumer ackReplyConsumer) -> {
          handleMessage(dlpJob, done, pubsubMessage, ackReplyConsumer);
        };
    Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageHandler).build();
    subscriber.startAsync();

    // Wait for job completion semi-synchronously
    // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
    try {
      done.get(15, TimeUnit.MINUTES);
    } catch (TimeoutException e) {
      System.out.println("Job was not completed after 15 minutes.");
      return;
    } finally {
      subscriber.stopAsync();
      subscriber.awaitTerminated();
    }

    // Get the latest state of the job from the service
    GetDlpJobRequest request = GetDlpJobRequest.newBuilder().setName(dlpJob.getName()).build();
    DlpJob completedJob = dlp.getDlpJob(request);

    // Parse the response and process results.
    System.out.println("Job status: " + completedJob.getState());
    InspectDataSourceDetails.Result result = completedJob.getInspectDetails().getResult();
    System.out.println("Findings: ");
    for (InfoTypeStats infoTypeStat : result.getInfoTypeStatsList()) {
      System.out.print("\tInfo type: " + infoTypeStat.getInfoType().getName());
      System.out.println("\tCount: " + infoTypeStat.getCount());
    }
  }
}
 
Example 18
Source File: InspectBigQueryTableWithSampling.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectBigQueryTableWithSampling(
    String projectId, String topicId, String subscriptionId)
    throws ExecutionException, InterruptedException, 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 BigQuery table to be inspected.
    BigQueryTable tableReference =
        BigQueryTable.newBuilder()
            .setProjectId("bigquery-public-data")
            .setDatasetId("usa_names")
            .setTableId("usa_1910_current")
            .build();

    BigQueryOptions bigQueryOptions =
        BigQueryOptions.newBuilder()
            .setTableReference(tableReference)
            .setRowsLimit(1000)
            .setSampleMethod(SampleMethod.RANDOM_START)
            .addIdentifyingFields(FieldId.newBuilder().setName("name"))
            .build();

    StorageConfig storageConfig =
        StorageConfig.newBuilder().setBigQueryOptions(bigQueryOptions).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("PERSON_NAME").build();

    // Specify how the content should be inspected.
    InspectConfig inspectConfig =
        InspectConfig.newBuilder()
            .addInfoTypes(infoType)
            .setIncludeQuote(true)
            .build();

    // Specify the action that is triggered when the job completes.
    String pubSubTopic = String.format("projects/%s/topics/%s", projectId, topicId);
    Action.PublishToPubSub publishToPubSub =
        Action.PublishToPubSub.newBuilder().setTopic(pubSubTopic).build();
    Action action = Action.newBuilder().setPubSub(publishToPubSub).build();

    // Configure the long running job we want the service to perform.
    InspectJobConfig inspectJobConfig =
        InspectJobConfig.newBuilder()
            .setStorageConfig(storageConfig)
            .setInspectConfig(inspectConfig)
            .addActions(action)
            .build();

    // Create the request for the job configured above.
    CreateDlpJobRequest createDlpJobRequest =
        CreateDlpJobRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setInspectJob(inspectJobConfig)
            .build();

    // Use the client to send the request.
    final DlpJob dlpJob = dlp.createDlpJob(createDlpJobRequest);
    System.out.println("Job created: " + dlpJob.getName());

    // Set up a Pub/Sub subscriber to listen on the job completion status
    final SettableApiFuture<Boolean> done = SettableApiFuture.create();

    ProjectSubscriptionName subscriptionName =
        ProjectSubscriptionName.of(projectId, subscriptionId);

    MessageReceiver messageHandler =
        (PubsubMessage pubsubMessage, AckReplyConsumer ackReplyConsumer) -> {
          handleMessage(dlpJob, done, pubsubMessage, ackReplyConsumer);
        };
    Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageHandler).build();
    subscriber.startAsync();

    // Wait for job completion semi-synchronously
    // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
    try {
      done.get(15, TimeUnit.MINUTES);
    } catch (TimeoutException e) {
      System.out.println("Job was not completed after 15 minutes.");
      return;
    } finally {
      subscriber.stopAsync();
      subscriber.awaitTerminated();
    }

    // Get the latest state of the job from the service
    GetDlpJobRequest request = GetDlpJobRequest.newBuilder().setName(dlpJob.getName()).build();
    DlpJob completedJob = dlp.getDlpJob(request);

    // Parse the response and process results.
    System.out.println("Job status: " + completedJob.getState());
    InspectDataSourceDetails.Result result = completedJob.getInspectDetails().getResult();
    System.out.println("Findings: ");
    for (InfoTypeStats infoTypeStat : result.getInfoTypeStatsList()) {
      System.out.print("\tInfo type: " + infoTypeStat.getInfoType().getName());
      System.out.println("\tCount: " + infoTypeStat.getCount());
    }
  }
}
 
Example 19
Source File: InspectGcsFileWithSampling.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void inspectGcsFileWithSampling(
    String projectId, String gcsUri, String topicId, String subscriptionId)
    throws ExecutionException, InterruptedException, 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 GCS file to be inspected and sampling configuration
    CloudStorageOptions cloudStorageOptions =
        CloudStorageOptions.newBuilder()
            .setFileSet(FileSet.newBuilder().setUrl(gcsUri))
            .setBytesLimitPerFile(200)
            .addFileTypes(FileType.TEXT_FILE)
            .setFilesLimitPercent(90)
            .setSampleMethod(SampleMethod.RANDOM_START)
            .build();

    StorageConfig storageConfig =
        StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).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("PERSON_NAME").build();

    // Specify how the content should be inspected.
    InspectConfig inspectConfig =
        InspectConfig.newBuilder()
            .addInfoTypes(infoType)
            .setExcludeInfoTypes(true)
            .setIncludeQuote(true)
            .setMinLikelihood(Likelihood.POSSIBLE)
            .build();

    // Specify the action that is triggered when the job completes.
    String pubSubTopic = String.format("projects/%s/topics/%s", projectId, topicId);
    Action.PublishToPubSub publishToPubSub =
        Action.PublishToPubSub.newBuilder().setTopic(pubSubTopic).build();
    Action action = Action.newBuilder().setPubSub(publishToPubSub).build();

    // Configure the long running job we want the service to perform.
    InspectJobConfig inspectJobConfig =
        InspectJobConfig.newBuilder()
            .setStorageConfig(storageConfig)
            .setInspectConfig(inspectConfig)
            .addActions(action)
            .build();

    // Create the request for the job configured above.
    CreateDlpJobRequest createDlpJobRequest =
        CreateDlpJobRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setInspectJob(inspectJobConfig)
            .build();

    // Use the client to send the request.
    final DlpJob dlpJob = dlp.createDlpJob(createDlpJobRequest);
    System.out.println("Job created: " + dlpJob.getName());

    // Set up a Pub/Sub subscriber to listen on the job completion status
    final SettableApiFuture<Boolean> done = SettableApiFuture.create();

    ProjectSubscriptionName subscriptionName =
        ProjectSubscriptionName.of(projectId, subscriptionId);

    MessageReceiver messageHandler =
        (PubsubMessage pubsubMessage, AckReplyConsumer ackReplyConsumer) -> {
          handleMessage(dlpJob, done, pubsubMessage, ackReplyConsumer);
        };
    Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageHandler).build();
    subscriber.startAsync();

    // Wait for job completion semi-synchronously
    // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
    try {
      done.get(15, TimeUnit.MINUTES);
    } catch (TimeoutException e) {
      System.out.println("Job was not completed after 15 minutes.");
      return;
    } finally {
      subscriber.stopAsync();
      subscriber.awaitTerminated();
    }

    // Get the latest state of the job from the service
    GetDlpJobRequest request = GetDlpJobRequest.newBuilder().setName(dlpJob.getName()).build();
    DlpJob completedJob = dlp.getDlpJob(request);

    // Parse the response and process results.
    System.out.println("Job status: " + completedJob.getState());
    InspectDataSourceDetails.Result result = completedJob.getInspectDetails().getResult();
    System.out.println("Findings: ");
    for (InfoTypeStats infoTypeStat : result.getInfoTypeStatsList()) {
      System.out.print("\tInfo type: " + infoTypeStat.getInfoType().getName());
      System.out.println("\tCount: " + infoTypeStat.getCount());
    }
  }
}
 
Example 20
Source File: RiskAnalysisNumericalStats.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void numericalStatsAnalysis(
    String projectId, String datasetId, String tableId, String topicId, String subscriptionId)
    throws ExecutionException, InterruptedException, 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 BigQuery table to analyze
    BigQueryTable bigQueryTable =
        BigQueryTable.newBuilder()
            .setTableId(tableId)
            .setDatasetId(datasetId)
            .setProjectId(projectId)
            .build();

    // This represents the name of the column to analyze, which must contain numerical data
    String columnName = "Age";

    // Configure the privacy metric for the job
    FieldId fieldId = FieldId.newBuilder().setName(columnName).build();
    NumericalStatsConfig numericalStatsConfig =
        NumericalStatsConfig.newBuilder().setField(fieldId).build();
    PrivacyMetric privacyMetric =
        PrivacyMetric.newBuilder().setNumericalStatsConfig(numericalStatsConfig).build();

    // Create action to publish job status notifications over Google Cloud Pub/Sub
    ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
    PublishToPubSub publishToPubSub =
        PublishToPubSub.newBuilder().setTopic(topicName.toString()).build();
    Action action = Action.newBuilder().setPubSub(publishToPubSub).build();

    // Configure the risk analysis job to perform
    RiskAnalysisJobConfig riskAnalysisJobConfig =
        RiskAnalysisJobConfig.newBuilder()
            .setSourceTable(bigQueryTable)
            .setPrivacyMetric(privacyMetric)
            .addActions(action)
            .build();

    CreateDlpJobRequest createDlpJobRequest =
        CreateDlpJobRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setRiskJob(riskAnalysisJobConfig)
            .build();

    // Send the request to the API using the client
    DlpJob dlpJob = dlpServiceClient.createDlpJob(createDlpJobRequest);

    // Set up a Pub/Sub subscriber to listen on the job completion status
    final SettableApiFuture<Boolean> done = SettableApiFuture.create();

    ProjectSubscriptionName subscriptionName =
        ProjectSubscriptionName.of(projectId, subscriptionId);

    MessageReceiver messageHandler =
        (PubsubMessage pubsubMessage, AckReplyConsumer ackReplyConsumer) -> {
          handleMessage(dlpJob, done, pubsubMessage, ackReplyConsumer);
        };
    Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageHandler).build();
    subscriber.startAsync();

    // Wait for job completion semi-synchronously
    // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
    try {
      done.get(15, TimeUnit.MINUTES);
    } catch (TimeoutException e) {
      System.out.println("Job was not completed after 15 minutes.");
      return;
    } finally {
      subscriber.stopAsync();
      subscriber.awaitTerminated();
    }

    // Build a request to get the completed job
    GetDlpJobRequest getDlpJobRequest =
        GetDlpJobRequest.newBuilder().setName(dlpJob.getName()).build();

    // Retrieve completed job status
    DlpJob completedJob = dlpServiceClient.getDlpJob(getDlpJobRequest);
    System.out.println("Job status: " + completedJob.getState());

    // Get the result and parse through and process the information
    NumericalStatsResult result = completedJob.getRiskDetails().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;
    }
  }
}