Search in sources :

Example 1 with ProjectPath

use of org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.ProjectPath 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 2 with ProjectPath

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

the class PubsubClientTest method projectPathFromIdWellFormed.

// 
// Paths
// 
@Test
public void projectPathFromIdWellFormed() {
    ProjectPath path = PubsubClient.projectPathFromId("test");
    assertEquals("projects/test", path.getPath());
}
Also used : ProjectPath(org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.ProjectPath) Test(org.junit.Test)

Aggregations

ProjectPath (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.ProjectPath)2 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 NoSuchElementException (java.util.NoSuchElementException)1 GcpOptions (org.apache.beam.sdk.extensions.gcp.options.GcpOptions)1 SubscriptionPath (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath)1 TopicPath (org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.TopicPath)1 Test (org.junit.Test)1