Class PubsubClient
java.lang.Object
org.apache.beam.sdk.io.gcp.pubsub.PubsubClient
- All Implemented Interfaces:
Closeable,AutoCloseable
- Direct Known Subclasses:
PubsubGrpcClient,PubsubJsonClient,PubsubTestClient
An (abstract) helper class for talking to Pubsub via an underlying transport.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA message received from Pubsub.static classA message to be sent to Pubsub.static classPath representing a cloud project id.static interfaceFactory for creating clients.static classPath representing a Pubsub schema.static classPath representing a Pubsub subscription.static classPath representing a Pubsub topic. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract intackDeadlineSeconds(PubsubClient.SubscriptionPath subscription) Return the ack deadline, in seconds, forsubscription.abstract voidacknowledge(PubsubClient.SubscriptionPath subscription, List<String> ackIds) Acknowldege messages fromsubscriptionwithackIds.createRandomSubscription(PubsubClient.ProjectPath project, PubsubClient.TopicPath topic, int ackDeadlineSeconds) Create a random subscription fortopic.abstract voidcreateSchema(PubsubClient.SchemaPath schemaPath, String schemaContent, com.google.pubsub.v1.Schema.Type type) CreateSchemafrom Schema definition content.abstract voidcreateSubscription(PubsubClient.TopicPath topic, PubsubClient.SubscriptionPath subscription, int ackDeadlineSeconds) Createsubscriptiontotopic.abstract voidCreatetopic.abstract voidcreateTopic(PubsubClient.TopicPath topic, PubsubClient.SchemaPath schema) Create {link TopicPath} withPubsubClient.SchemaPath.abstract voiddeleteSchema(PubsubClient.SchemaPath schemaPath) DeletePubsubClient.SchemaPath.abstract voiddeleteSubscription(PubsubClient.SubscriptionPath subscription) Deletesubscription.abstract voidprotected static longReturn the timestamp (in ms since unix epoch) to use for a Pubsub message withtimestampAttributeandattriutes.abstract SchemagetSchema(PubsubClient.SchemaPath schemaPath) Return a BeamSchemafrom the Pub/Sub schema resource, if exists.abstract PubsubClient.SchemaPathgetSchemaPath(PubsubClient.TopicPath topicPath) ReturnPubsubClient.SchemaPathfromPubsubClient.TopicPathif exists.abstract booleanisEOF()Return true ifpull(long, org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath, int, boolean)will always return empty list.abstract booleanReturn true iftopicexists.abstract List<PubsubClient.SubscriptionPath> listSubscriptions(PubsubClient.ProjectPath project, PubsubClient.TopicPath topic) Return a list of subscriptions fortopicinproject.abstract List<PubsubClient.TopicPath> listTopics(PubsubClient.ProjectPath project) Return a list of topics forproject.abstract voidmodifyAckDeadline(PubsubClient.SubscriptionPath subscription, List<String> ackIds, int deadlineSeconds) Modify the ack deadline for messages fromsubscriptionwithackIdsto bedeadlineSecondsfrom now.protected static LongparseTimestampAsMsSinceEpoch(String timestamp) Return timestamp as ms-since-unix-epoch corresponding totimestamp.static PubsubClient.ProjectPathprojectPathFromId(String projectId) static PubsubClient.ProjectPathprojectPathFromPath(String path) abstract intpublish(PubsubClient.TopicPath topic, List<PubsubClient.OutgoingMessage> outgoingMessages) PublishoutgoingMessagesto Pubsubtopic.abstract List<PubsubClient.IncomingMessage> pull(long requestTimeMsSinceEpoch, PubsubClient.SubscriptionPath subscription, int batchSize, boolean returnImmediately) Request the next batch of up tobatchSizemessages fromsubscription.static PubsubClient.SchemaPathschemaPathFromId(String projectId, String schemaId) static PubsubClient.SchemaPathschemaPathFromPath(String path) subscriptionPathFromName(String projectId, String subscriptionName) static PubsubClient.TopicPathtopicPathFromName(String projectId, String topicName) static PubsubClient.TopicPathtopicPathFromPath(String path)
-
Constructor Details
-
PubsubClient
public PubsubClient()
-
-
Method Details
-
parseTimestampAsMsSinceEpoch
Return timestamp as ms-since-unix-epoch corresponding totimestamp. ThrowIllegalArgumentExceptionif timestamp cannot be recognized. -
extractTimestampAttribute
protected static long extractTimestampAttribute(String timestampAttribute, @Nullable Map<String, String> attributes) Return the timestamp (in ms since unix epoch) to use for a Pubsub message withtimestampAttributeandattriutes.The message attributes must contain
timestampAttribute, and the value of that attribute will be taken as the timestamp.- Throws:
IllegalArgumentException- if the timestamp cannot be recognized as a ms-since-unix-epoch or RFC3339 time.
-
projectPathFromPath
-
projectPathFromId
-
schemaPathFromPath
-
schemaPathFromId
-
subscriptionPathFromPath
-
subscriptionPathFromName
public static PubsubClient.SubscriptionPath subscriptionPathFromName(String projectId, String subscriptionName) -
topicPathFromPath
-
topicPathFromName
-
publish
public abstract int publish(PubsubClient.TopicPath topic, List<PubsubClient.OutgoingMessage> outgoingMessages) throws IOException PublishoutgoingMessagesto Pubsubtopic. Return number of messages published.- Throws:
IOException
-
pull
public abstract List<PubsubClient.IncomingMessage> pull(long requestTimeMsSinceEpoch, PubsubClient.SubscriptionPath subscription, int batchSize, boolean returnImmediately) throws IOException Request the next batch of up tobatchSizemessages fromsubscription. Return the received messages, or empty collection if none were available. Does not wait for messages to arrive ifreturnImmediatelyis true. Returned messages will record their request time asrequestTimeMsSinceEpoch.- Throws:
IOException
-
acknowledge
public abstract void acknowledge(PubsubClient.SubscriptionPath subscription, List<String> ackIds) throws IOException Acknowldege messages fromsubscriptionwithackIds.- Throws:
IOException
-
modifyAckDeadline
public abstract void modifyAckDeadline(PubsubClient.SubscriptionPath subscription, List<String> ackIds, int deadlineSeconds) throws IOException Modify the ack deadline for messages fromsubscriptionwithackIdsto bedeadlineSecondsfrom now.- Throws:
IOException
-
createTopic
Createtopic.- Throws:
IOException
-
createTopic
public abstract void createTopic(PubsubClient.TopicPath topic, PubsubClient.SchemaPath schema) throws IOException Create {link TopicPath} withPubsubClient.SchemaPath.- Throws:
IOException
-
deleteTopic
- Throws:
IOException
-
listTopics
public abstract List<PubsubClient.TopicPath> listTopics(PubsubClient.ProjectPath project) throws IOException Return a list of topics forproject.- Throws:
IOException
-
isTopicExists
Return true iftopicexists.- Throws:
IOException
-
createSubscription
public abstract void createSubscription(PubsubClient.TopicPath topic, PubsubClient.SubscriptionPath subscription, int ackDeadlineSeconds) throws IOException Createsubscriptiontotopic.- Throws:
IOException
-
createRandomSubscription
public PubsubClient.SubscriptionPath createRandomSubscription(PubsubClient.ProjectPath project, PubsubClient.TopicPath topic, int ackDeadlineSeconds) throws IOException Create a random subscription fortopic. Return thePubsubClient.SubscriptionPath. It is the responsibility of the caller to later delete the subscription.- Throws:
IOException
-
deleteSubscription
public abstract void deleteSubscription(PubsubClient.SubscriptionPath subscription) throws IOException Deletesubscription.- Throws:
IOException
-
listSubscriptions
public abstract List<PubsubClient.SubscriptionPath> listSubscriptions(PubsubClient.ProjectPath project, PubsubClient.TopicPath topic) throws IOException Return a list of subscriptions fortopicinproject.- Throws:
IOException
-
ackDeadlineSeconds
public abstract int ackDeadlineSeconds(PubsubClient.SubscriptionPath subscription) throws IOException Return the ack deadline, in seconds, forsubscription.- Throws:
IOException
-
isEOF
public abstract boolean isEOF()Return true ifpull(long, org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath, int, boolean)will always return empty list. Actual clients will return false. Test clients may return true to signal that all expected messages have been pulled and the test may complete. -
createSchema
public abstract void createSchema(PubsubClient.SchemaPath schemaPath, String schemaContent, com.google.pubsub.v1.Schema.Type type) throws IOException CreateSchemafrom Schema definition content.- Throws:
IOException
-
deleteSchema
DeletePubsubClient.SchemaPath.- Throws:
IOException
-
getSchemaPath
public abstract PubsubClient.SchemaPath getSchemaPath(PubsubClient.TopicPath topicPath) throws IOException ReturnPubsubClient.SchemaPathfromPubsubClient.TopicPathif exists.- Throws:
IOException
-
getSchema
Return a BeamSchemafrom the Pub/Sub schema resource, if exists.- Throws:
IOException
-