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 class
A message received from Pubsub.static class
A message to be sent to Pubsub.static class
Path representing a cloud project id.static interface
Factory for creating clients.static class
Path representing a Pubsub schema.static class
Path representing a Pubsub subscription.static class
Path representing a Pubsub topic. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract int
ackDeadlineSeconds
(PubsubClient.SubscriptionPath subscription) Return the ack deadline, in seconds, forsubscription
.abstract void
acknowledge
(PubsubClient.SubscriptionPath subscription, List<String> ackIds) Acknowldege messages fromsubscription
withackIds
.createRandomSubscription
(PubsubClient.ProjectPath project, PubsubClient.TopicPath topic, int ackDeadlineSeconds) Create a random subscription fortopic
.abstract void
createSchema
(PubsubClient.SchemaPath schemaPath, String schemaContent, com.google.pubsub.v1.Schema.Type type) CreateSchema
from Schema definition content.abstract void
createSubscription
(PubsubClient.TopicPath topic, PubsubClient.SubscriptionPath subscription, int ackDeadlineSeconds) Createsubscription
totopic
.abstract void
Createtopic
.abstract void
createTopic
(PubsubClient.TopicPath topic, PubsubClient.SchemaPath schema) Create {link TopicPath} withPubsubClient.SchemaPath
.abstract void
deleteSchema
(PubsubClient.SchemaPath schemaPath) DeletePubsubClient.SchemaPath
.abstract void
deleteSubscription
(PubsubClient.SubscriptionPath subscription) Deletesubscription
.abstract void
protected static long
Return the timestamp (in ms since unix epoch) to use for a Pubsub message withtimestampAttribute
andattriutes
.abstract Schema
getSchema
(PubsubClient.SchemaPath schemaPath) Return a BeamSchema
from the Pub/Sub schema resource, if exists.abstract PubsubClient.SchemaPath
getSchemaPath
(PubsubClient.TopicPath topicPath) ReturnPubsubClient.SchemaPath
fromPubsubClient.TopicPath
if exists.abstract boolean
isEOF()
Return true ifpull(long, org.apache.beam.sdk.io.gcp.pubsub.PubsubClient.SubscriptionPath, int, boolean)
will always return empty list.abstract boolean
Return true iftopic
exists.abstract List
<PubsubClient.SubscriptionPath> listSubscriptions
(PubsubClient.ProjectPath project, PubsubClient.TopicPath topic) Return a list of subscriptions fortopic
inproject
.abstract List
<PubsubClient.TopicPath> listTopics
(PubsubClient.ProjectPath project) Return a list of topics forproject
.abstract void
modifyAckDeadline
(PubsubClient.SubscriptionPath subscription, List<String> ackIds, int deadlineSeconds) Modify the ack deadline for messages fromsubscription
withackIds
to bedeadlineSeconds
from now.protected static Long
parseTimestampAsMsSinceEpoch
(String timestamp) Return timestamp as ms-since-unix-epoch corresponding totimestamp
.static PubsubClient.ProjectPath
projectPathFromId
(String projectId) static PubsubClient.ProjectPath
projectPathFromPath
(String path) abstract int
publish
(PubsubClient.TopicPath topic, List<PubsubClient.OutgoingMessage> outgoingMessages) PublishoutgoingMessages
to Pubsubtopic
.abstract List
<PubsubClient.IncomingMessage> pull
(long requestTimeMsSinceEpoch, PubsubClient.SubscriptionPath subscription, int batchSize, boolean returnImmediately) Request the next batch of up tobatchSize
messages fromsubscription
.static PubsubClient.SchemaPath
schemaPathFromId
(String projectId, String schemaId) static PubsubClient.SchemaPath
schemaPathFromPath
(String path) subscriptionPathFromName
(String projectId, String subscriptionName) static PubsubClient.TopicPath
topicPathFromName
(String projectId, String topicName) static PubsubClient.TopicPath
topicPathFromPath
(String path)
-
Constructor Details
-
PubsubClient
public PubsubClient()
-
-
Method Details
-
parseTimestampAsMsSinceEpoch
Return timestamp as ms-since-unix-epoch corresponding totimestamp
. ThrowIllegalArgumentException
if 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 withtimestampAttribute
andattriutes
.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 PublishoutgoingMessages
to 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 tobatchSize
messages fromsubscription
. Return the received messages, or empty collection if none were available. Does not wait for messages to arrive ifreturnImmediately
is 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 fromsubscription
withackIds
.- Throws:
IOException
-
modifyAckDeadline
public abstract void modifyAckDeadline(PubsubClient.SubscriptionPath subscription, List<String> ackIds, int deadlineSeconds) throws IOException Modify the ack deadline for messages fromsubscription
withackIds
to bedeadlineSeconds
from 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 iftopic
exists.- Throws:
IOException
-
createSubscription
public abstract void createSubscription(PubsubClient.TopicPath topic, PubsubClient.SubscriptionPath subscription, int ackDeadlineSeconds) throws IOException Createsubscription
totopic
.- 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 fortopic
inproject
.- 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 CreateSchema
from Schema definition content.- Throws:
IOException
-
deleteSchema
DeletePubsubClient.SchemaPath
.- Throws:
IOException
-
getSchemaPath
public abstract PubsubClient.SchemaPath getSchemaPath(PubsubClient.TopicPath topicPath) throws IOException ReturnPubsubClient.SchemaPath
fromPubsubClient.TopicPath
if exists.- Throws:
IOException
-
getSchema
Return a BeamSchema
from the Pub/Sub schema resource, if exists.- Throws:
IOException
-