Search in sources :

Example 6 with TopicPath

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath in project beam by apache.

the class TestPubsubSignal method initializePubsub.

private void initializePubsub(Description description) throws IOException {
    topicAdmin = TopicAdminClient.create(TopicAdminSettings.newBuilder().setCredentialsProvider(pipelineOptions::getGcpCredential).setEndpoint(pubsubEndpoint).build());
    subscriptionAdmin = SubscriptionAdminClient.create(SubscriptionAdminSettings.newBuilder().setCredentialsProvider(pipelineOptions::getGcpCredential).setEndpoint(pubsubEndpoint).build());
    // Example topic name:
    // integ-test-TestClassName-testMethodName-2018-12-11-23-32-333-<random-long>-result
    TopicPath resultTopicPathTmp = PubsubClient.topicPathFromName(pipelineOptions.getProject(), createTopicName(description, RESULT_TOPIC_NAME));
    TopicPath startTopicPathTmp = PubsubClient.topicPathFromName(pipelineOptions.getProject(), createTopicName(description, START_TOPIC_NAME));
    topicAdmin.createTopic(resultTopicPathTmp.getPath());
    topicAdmin.createTopic(startTopicPathTmp.getPath());
    // Set these after successful creation; this signals that they need teardown
    resultTopicPath = resultTopicPathTmp;
    startTopicPath = startTopicPathTmp;
}
Also used : TopicPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath)

Example 7 with TopicPath

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath in project beam by apache.

the class PubsubUnboundedSource method createRandomSubscription.

private SubscriptionPath createRandomSubscription(PipelineOptions options) {
    TopicPath topicPath = topic.get();
    ProjectPath projectPath;
    if (project != null) {
        projectPath = project.get();
    } else {
        String projectId = options.as(GcpOptions.class).getProject();
        checkState(projectId != null, "Cannot create subscription to topic %s because pipeline option 'project' not specified", topicPath);
        projectPath = PubsubClient.projectPathFromId(options.as(GcpOptions.class).getProject());
    }
    try {
        try (PubsubClient pubsubClient = pubsubFactory.newClient(timestampAttribute, idAttribute, options.as(PubsubOptions.class))) {
            SubscriptionPath subscriptionPath = pubsubClient.createRandomSubscription(projectPath, topicPath, DEAULT_ACK_TIMEOUT_SEC);
            LOG.warn("Created subscription {} to topic {}." + " Note this subscription WILL NOT be deleted when the pipeline terminates", subscriptionPath, topic);
            return subscriptionPath;
        }
    } catch (Exception e) {
        throw new RuntimeException(String.format("Failed to create subscription to topic %s on project %s: %s", topicPath, projectPath, e.getMessage()), e);
    }
}
Also used : TopicPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath) GcpOptions(org.apache.beam.sdk.extensions.gcp.options.GcpOptions) SubscriptionPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath) ProjectPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.ProjectPath) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Example 8 with TopicPath

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath in project beam by apache.

the class TestPubsub method initializePubsub.

private void initializePubsub(Description description) throws IOException {
    if (isLocalhost) {
        channel = ManagedChannelBuilder.forTarget(pubsubEndpoint).usePlaintext().build();
    } else {
        channel = ManagedChannelBuilder.forTarget(pubsubEndpoint).useTransportSecurity().build();
    }
    channelProvider = FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel));
    topicAdmin = TopicAdminClient.create(TopicAdminSettings.newBuilder().setCredentialsProvider(pipelineOptions::getGcpCredential).setTransportChannelProvider(channelProvider).setEndpoint(pubsubEndpoint).build());
    subscriptionAdmin = SubscriptionAdminClient.create(SubscriptionAdminSettings.newBuilder().setCredentialsProvider(pipelineOptions::getGcpCredential).setTransportChannelProvider(channelProvider).setEndpoint(pubsubEndpoint).build());
    TopicPath eventsTopicPathTmp = PubsubClient.topicPathFromName(pipelineOptions.getProject(), createTopicName(description, EVENTS_TOPIC_NAME));
    topicAdmin.createTopic(eventsTopicPathTmp.getPath());
    // Set this after successful creation; it signals that the topic needs teardown
    eventsTopicPath = eventsTopicPathTmp;
    String subscriptionName = topicPath().getName() + "_beam_" + ThreadLocalRandom.current().nextLong();
    SubscriptionPath subscriptionPathTmp = new SubscriptionPath(String.format("projects/%s/subscriptions/%s", pipelineOptions.getProject(), subscriptionName));
    subscriptionAdmin.createSubscription(subscriptionPathTmp.getPath(), topicPath().getPath(), PushConfig.getDefaultInstance(), DEFAULT_ACK_DEADLINE_SECONDS);
    subscriptionPath = subscriptionPathTmp;
}
Also used : TopicPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath) SubscriptionPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath) ByteString(com.google.protobuf.ByteString)

Example 9 with TopicPath

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath in project beam by apache.

the class TestPubsub method publish.

/**
 * Publish messages to {@link #topicPath()}.
 */
public void publish(List<PubsubMessage> messages) {
    Preconditions.checkNotNull(eventsTopicPath);
    Publisher eventPublisher;
    try {
        eventPublisher = Publisher.newBuilder(eventsTopicPath.getPath()).setCredentialsProvider(pipelineOptions::getGcpCredential).setChannelProvider(channelProvider).setEndpoint(pubsubEndpoint).build();
    } catch (IOException e) {
        throw new RuntimeException("Error creating event publisher", e);
    }
    List<ApiFuture<String>> futures = messages.stream().map((message) -> {
        com.google.pubsub.v1.PubsubMessage.Builder builder = com.google.pubsub.v1.PubsubMessage.newBuilder().setData(ByteString.copyFrom(message.getPayload())).putAllAttributes(message.getAttributeMap());
        return eventPublisher.publish(builder.build());
    }).collect(Collectors.toList());
    try {
        ApiFutures.allAsList(futures).get();
    } catch (ExecutionException e) {
        throw new RuntimeException("Error publishing a test message", e);
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while waiting for messages to publish", e);
    }
    eventPublisher.shutdown();
}
Also used : Statement(org.junit.runners.model.Statement) TopicPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath) TestPipelineOptions(org.apache.beam.sdk.testing.TestPipelineOptions) ManagedChannel(io.grpc.ManagedChannel) TestRule(org.junit.rules.TestRule) Duration(org.joda.time.Duration) TimeoutException(java.util.concurrent.TimeoutException) Subscriber(com.google.cloud.pubsub.v1.Subscriber) Streams(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Streams) AckReplyConsumer(com.google.cloud.pubsub.v1.AckReplyConsumer) Publisher(com.google.cloud.pubsub.v1.Publisher) Seconds(org.joda.time.Seconds) FixedTransportChannelProvider(com.google.api.gax.rpc.FixedTransportChannelProvider) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) Iterables(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables) SubscriptionAdminClient(com.google.cloud.pubsub.v1.SubscriptionAdminClient) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) Nullable(org.checkerframework.checker.nullness.qual.Nullable) DateTimeFormat(org.joda.time.format.DateTimeFormat) MessageReceiver(com.google.cloud.pubsub.v1.MessageReceiver) ApiFutures(com.google.api.core.ApiFutures) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) SubscriptionPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) Description(org.junit.runner.Description) TopicAdminSettings(com.google.cloud.pubsub.v1.TopicAdminSettings) Collectors(java.util.stream.Collectors) ApiFuture(com.google.api.core.ApiFuture) ByteString(com.google.protobuf.ByteString) ExecutionException(java.util.concurrent.ExecutionException) GrpcTransportChannel(com.google.api.gax.grpc.GrpcTransportChannel) ManagedChannelBuilder(io.grpc.ManagedChannelBuilder) List(java.util.List) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) TransportChannelProvider(com.google.api.gax.rpc.TransportChannelProvider) TopicAdminClient(com.google.cloud.pubsub.v1.TopicAdminClient) Preconditions(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions) Matcher(org.hamcrest.Matcher) Instant(org.joda.time.Instant) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) SubscriptionAdminSettings(com.google.cloud.pubsub.v1.SubscriptionAdminSettings) PushConfig(com.google.pubsub.v1.PushConfig) ApiFuture(com.google.api.core.ApiFuture) ManagedChannelBuilder(io.grpc.ManagedChannelBuilder) Publisher(com.google.cloud.pubsub.v1.Publisher) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 10 with TopicPath

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath in project beam by apache.

the class PubsubUnboundedSourceTest method noSubscriptionSplitGeneratesSubscription.

@Test
public void noSubscriptionSplitGeneratesSubscription() throws Exception {
    TopicPath topicPath = PubsubClient.topicPathFromName("my_project", "my_topic");
    factory = PubsubTestClient.createFactoryForCreateSubscription();
    PubsubUnboundedSource source = new PubsubUnboundedSource(factory, StaticValueProvider.of(PubsubClient.projectPathFromId("my_project")), StaticValueProvider.of(topicPath), null, /* subscription */
    null, /* timestampLabel */
    null, /* idLabel */
    false);
    assertThat(source.getSubscription(), nullValue());
    assertThat(source.getSubscription(), nullValue());
    PipelineOptions options = PipelineOptionsFactory.create();
    List<PubsubSource> splits = new PubsubSource(source).split(3, options);
    // We have at least one returned split
    assertThat(splits, hasSize(greaterThan(0)));
    for (PubsubSource split : splits) {
        // Each split is equal
        assertThat(split, equalTo(splits.get(0)));
    }
    assertThat(splits.get(0).subscriptionPath, not(nullValue()));
}
Also used : TopicPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) PubsubSource(org.apache.beam.sdk.io.gcp.pubsub.PubsubUnboundedSource.PubsubSource) Test(org.junit.Test)

Aggregations

TopicPath (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath)17 SubscriptionPath (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath)10 IOException (java.io.IOException)6 Test (org.junit.Test)6 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)3 PipelineOptions (org.apache.beam.sdk.options.PipelineOptions)3 ByteString (com.google.protobuf.ByteString)2 PubsubSource (org.apache.beam.sdk.io.gcp.pubsub.PubsubUnboundedSource.PubsubSource)2 ApiFuture (com.google.api.core.ApiFuture)1 ApiFutures (com.google.api.core.ApiFutures)1 GrpcTransportChannel (com.google.api.gax.grpc.GrpcTransportChannel)1 FixedTransportChannelProvider (com.google.api.gax.rpc.FixedTransportChannelProvider)1 TransportChannelProvider (com.google.api.gax.rpc.TransportChannelProvider)1 Subscriptions (com.google.api.services.pubsub.Pubsub.Projects.Subscriptions)1 Topics (com.google.api.services.pubsub.Pubsub.Projects.Topics)1 ListSubscriptionsResponse (com.google.api.services.pubsub.model.ListSubscriptionsResponse)1 ListTopicsResponse (com.google.api.services.pubsub.model.ListTopicsResponse)1 AckReplyConsumer (com.google.cloud.pubsub.v1.AckReplyConsumer)1 MessageReceiver (com.google.cloud.pubsub.v1.MessageReceiver)1 Publisher (com.google.cloud.pubsub.v1.Publisher)1