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

The following examples show how to use com.google.pubsub.v1.ProjectTopicName#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: PubsubHelper.java    From flink with Apache License 2.0 6 votes vote down vote up
public void createSubscription(String subscriptionProject, String subscription, String topicProject, String topic) throws IOException {
	ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.newBuilder()
		.setProject(subscriptionProject)
		.setSubscription(subscription)
		.build();

	deleteSubscription(subscriptionName);

	SubscriptionAdminClient adminClient = getSubscriptionAdminClient();

	ProjectTopicName topicName = ProjectTopicName.of(topicProject, topic);

	PushConfig pushConfig = PushConfig.getDefaultInstance();

	LOG.info("CreateSubscription {}", subscriptionName);
	getSubscriptionAdminClient().createSubscription(subscriptionName, topicName, pushConfig, 1);
}
 
Example 2
Source File: GoogleCloudPubSubSinkConfiguration.java    From divolte-collector with Apache License 2.0 6 votes vote down vote up
private SinkFactory createFlushingPool(final RetrySettings retrySettings,
                                       final BatchingSettings batchingSettings) {
    return (vc, sinkName, registry) -> {
        final String projectId = vc.configuration().global.gcps.projectId.orElseThrow(IllegalStateException::new);
        final ProjectTopicName topicName = ProjectTopicName.of(projectId, topic);
        final Publisher.Builder builder =
            Publisher.newBuilder(topicName)
                     .setRetrySettings(retrySettings)
                     .setBatchingSettings(batchingSettings);
        final Publisher publisher = IOExceptions.wrap(builder::build).get();
        return new GoogleCloudPubSubFlushingPool(sinkName,
                                                 vc.configuration().global.gcps.threads,
                                                 vc.configuration().global.gcps.bufferSize,
                                                 publisher,
                                                 Optional.empty(),
                                                 registry.getSchemaBySinkName(sinkName));
    };
}
 
Example 3
Source File: DeviceRegistryExample.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
/** Creates a topic and grants the IoT service account access. */
protected static Topic createIotTopic(String projectId, String topicId) throws Exception {
  // Create a new topic
  final ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);

  try (TopicAdminClient topicAdminClient = TopicAdminClient.create()) {
    final Topic topic = topicAdminClient.createTopic(topicName);
    final String topicString = topicName.toString();
    // add role -> members binding
    // create updated policy
    topicAdminClient.setIamPolicy(
        topicString,
        com.google.iam.v1.Policy.newBuilder(topicAdminClient.getIamPolicy(topicString))
            .addBindings(
                Binding.newBuilder()
                    .addMembers("serviceAccount:[email protected]")
                    .setRole(Role.owner().toString())
                    .build())
            .build());

    System.out.println("Setup topic / policy for: " + topic.getName());
    return topic;
  }
}
 
Example 4
Source File: PubSubTopicUtils.java    From spring-cloud-gcp with Apache License 2.0 6 votes vote down vote up
/**
 * Create a {@link ProjectTopicName} based on a topic name within a project or the
 * fully-qualified topic name. If the specified topic is in the
 * {@code projects/<project_name>/topics/<topic_name>} format, then the {@code projectId} is
 * ignored}
 * @param topic the topic name in the project or the fully-qualified project name
 * @param projectId the project ID to use if the topic is not a fully-qualified name
 * @return the Pub/Sub object representing the topic name
 */
public static ProjectTopicName toProjectTopicName(String topic, @Nullable String projectId) {
	Assert.notNull(topic, "The topic can't be null.");

	ProjectTopicName projectTopicName = null;

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

	return projectTopicName;
}
 
Example 5
Source File: CdcPCollectionsFetchers.java    From DataflowTemplates with Apache License 2.0 6 votes vote down vote up
private ProjectTopicName getPubSubTopic() {
  if (options.getInputSubscriptions() != null && !options.getInputSubscriptions().isEmpty()) {
    try {
      return PubsubUtils.getPubSubTopicFromSubscription(
          options.as(GcpOptions.class).getProject(), options.getInputSubscriptions());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  } else {
    Preconditions.checkArgument(
        options.getInputTopics() != null && !options.getInputTopics().isEmpty(),
        "Must provide an inputSubscriptions or inputTopics parameter.");
    return ProjectTopicName.of(
        options.as(GcpOptions.class).getProject(), options.getInputTopics());
  }
}
 
Example 6
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 7
Source File: Poller.java    From spanner-event-exporter with Apache License 2.0 6 votes vote down vote up
private Publisher configurePubSub() {
  if (publishToPubSub) {
    ProjectTopicName topicName =
        ProjectTopicName.of(PROJECT_ID, tableName); // Topic name will always be the Table Name
    try {
      Publisher publisher = Publisher.newBuilder(topicName).build();
      return publisher;
    } catch (IOException e) {
      log.error("Was not able to create a publisher for topic: " + topicName, e);

      stop();
      System.exit(1);
    }
  }

  // If configured to publishToPubSub, this function will return a publisher or throw
  return null;
}
 
Example 8
Source File: PublishGCPubSub.java    From nifi with Apache License 2.0 5 votes vote down vote up
private ProjectTopicName getTopicName(ProcessContext context) {
    final String topic = context.getProperty(TOPIC_NAME).evaluateAttributeExpressions().getValue();
    final String projectId = context.getProperty(PROJECT_ID).getValue();

    if (topic.contains("/")) {
        return ProjectTopicName.parse(topic);
    } else {
        return ProjectTopicName.of(projectId, topic);
    }
}
 
Example 9
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 10
Source File: PubSubConnector.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private void createTopic(final PubSubConfig config) {
    final TopicAdminClient topicAdminClient = pubSubManager.topicAdminClient(config);

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

    try {
        topicAdminClient.getTopic(topicName);
    } catch (final NotFoundException nf) {
        try {
            topicAdminClient.createTopic(topicName);
        } catch (final AlreadyExistsException ae) {
            log.topicExistAlready(topicName, ae);
        }
    }
}
 
Example 11
Source File: PubSubManager.java    From smallrye-reactive-messaging with Apache License 2.0 5 votes vote down vote up
private static Publisher buildPublisher(final PubSubConfig config) {
    final ProjectTopicName topicName = ProjectTopicName.of(config.getProjectId(), config.getTopic());

    try {
        final Publisher.Builder publisherBuilder = Publisher.newBuilder(topicName);

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

        return publisherBuilder.build();
    } catch (final IOException e) {
        throw ex.illegalStateUnableToBuildPublisher(e);
    }
}
 
Example 12
Source File: CloudPubSubSinkTask.java    From pubsub with Apache License 2.0 5 votes vote down vote up
private void createPublisher() {
  ProjectTopicName fullTopic = ProjectTopicName.of(cpsProject, cpsTopic);
  com.google.cloud.pubsub.v1.Publisher.Builder builder =
      com.google.cloud.pubsub.v1.Publisher.newBuilder(fullTopic)
          .setCredentialsProvider(gcpCredentialsProvider)
          .setBatchingSettings(
              BatchingSettings.newBuilder()
                  .setDelayThreshold(Duration.ofMillis(maxDelayThresholdMs))
                  .setElementCountThreshold(maxBufferSize)
                  .setRequestByteThreshold(maxBufferBytes)
                  .build())
          .setRetrySettings(
              RetrySettings.newBuilder()
                  // All values that are not configurable come from the defaults for the publisher
                  // client library.
                  .setTotalTimeout(Duration.ofMillis(maxTotalTimeoutMs))
                  .setMaxRpcTimeout(Duration.ofMillis(maxRequestTimeoutMs))
                  .setInitialRetryDelay(Duration.ofMillis(5))
                  .setRetryDelayMultiplier(2)
                  .setMaxRetryDelay(Duration.ofMillis(Long.MAX_VALUE))
                  .setInitialRpcTimeout(Duration.ofSeconds(10))
                  .setRpcTimeoutMultiplier(2)
                  .build());
  try {
    publisher = builder.build();
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example 13
Source File: EmulatorHelper.java    From heroic with Apache License 2.0 5 votes vote down vote up
public static Publisher publisher(
    String projectId, String topicId, String endpoint
) throws IOException {
    ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);

    return Publisher.newBuilder(topicName)
        .setChannelProvider(channelProvider(endpoint))
        .setCredentialsProvider(credentialsProvider)
        .build();
}
 
Example 14
Source File: GoogleCloudPubSubSinkConfiguration.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
private SinkFactory createFlushingPool(final RetrySettings retrySettings,
                                       final BatchingSettings batchingSettings,
                                       final String hostPort) {
    // Based on Google's PubSub documentation:
    //   https://cloud.google.com/pubsub/docs/emulator#pubsub-emulator-java
    // What's going on here? Well…
    //  - Authentication is disabled; the emulator doesn't use or support it.
    //  - When Pub/Sub wants an I/O channel to talk to its Google Cloud endpoint, we're substituting our
    //    own endpoint instead. This channel also has TLS disabled, because the emulator doesn't need, use
    //    or support it.
    //
    return (vc, sinkName, registry) -> {
        logger.info("Configuring sink to use Google Cloud Pub/Sub emulator: {}", sinkName, hostPort);
        final String projectId = vc.configuration().global.gcps.projectId.orElseThrow(IllegalStateException::new);
        final ProjectTopicName topicName = ProjectTopicName.of(projectId, topic);
        final ManagedChannel channel = ManagedChannelBuilder.forTarget(hostPort).usePlaintext().build();
        final TransportChannelProvider channelProvider =
            FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
        // There's no easy way to create topics for the emulator, so we create the topic ourselves.
        createTopic(hostPort, channelProvider, topicName);
        final Publisher.Builder builder =
            Publisher.newBuilder(topicName)
                     .setRetrySettings(retrySettings)
                     .setBatchingSettings(batchingSettings)
                     .setChannelProvider(channelProvider)
                     .setCredentialsProvider(NoCredentialsProvider.create());
        final Publisher publisher = IOExceptions.wrap(builder::build).get();
        return new GoogleCloudPubSubFlushingPool(sinkName,
                                                 vc.configuration().global.gcps.threads,
                                                 vc.configuration().global.gcps.bufferSize,
                                                 publisher,
                                                 Optional.of(channel),
                                                 registry.getSchemaBySinkName(sinkName));
    };
}
 
Example 15
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 16
Source File: GooglePubsubPublisher.java    From echo with Apache License 2.0 5 votes vote down vote up
public static GooglePubsubPublisher buildPublisher(
    GooglePubsubPublisherConfig config, ObjectMapper mapper) {
  GooglePubsubPublisher publisher = new GooglePubsubPublisher();
  publisher.setName(config.getName());
  ProjectTopicName fullName = ProjectTopicName.of(config.getProject(), config.getTopicName());
  publisher.setTopicName(config.getTopicName());
  publisher.setFullTopicName(fullName.toString());
  publisher.setContent(config.getContent());
  publisher.setMapper(mapper);

  BatchingSettings batchingSettings =
      BatchingSettings.newBuilder()
          .setElementCountThreshold(config.getBatchCountThreshold())
          .setDelayThreshold(Duration.ofMillis(config.getDelayMillisecondsThreshold()))
          .build();

  try {
    Publisher p =
        Publisher.newBuilder(fullName)
            .setCredentialsProvider(new GooglePubsubCredentialsProvider(config.getJsonPath()))
            .setBatchingSettings(batchingSettings)
            .build();
    publisher.setPublisher(p);
  } catch (IOException ioe) {
    log.error("Could not create Google Pubsub Publishers", ioe);
  }

  return publisher;
}
 
Example 17
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public Topic createTopic(String project, String topic) throws IOException {
	deleteTopic(project, topic);
	ProjectTopicName topicName = ProjectTopicName.of(project, topic);
	TopicAdminClient adminClient = getTopicAdminClient();
	LOG.info("CreateTopic {}", topicName);
	return adminClient.createTopic(topicName);
}
 
Example 18
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public void createSubscription(String subscriptionProject, String subscription, String topicProject, String topic) throws IOException {
	ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.newBuilder()
		.setProject(subscriptionProject)
		.setSubscription(subscription)
		.build();

	deleteSubscription(subscriptionName);

	ProjectTopicName topicName = ProjectTopicName.of(topicProject, topic);

	PushConfig pushConfig = PushConfig.getDefaultInstance();

	LOG.info("CreateSubscription {}", subscriptionName);
	getSubscriptionAdminClient().createSubscription(subscriptionName, topicName, pushConfig, 1).isInitialized();
}
 
Example 19
Source File: PubsubHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
public Topic createTopic(String project, String topic) throws IOException {
	deleteTopic(project, topic);
	ProjectTopicName topicName = ProjectTopicName.of(project, topic);
	TopicAdminClient adminClient = getTopicAdminClient();
	LOG.info("CreateTopic {}", topicName);
	return adminClient.createTopic(topicName);
}
 
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;
    }
  }
}