Class PubSubSourceBuilder<T>

java.lang.Object
io.github.flink.gcp.connector.pubsub.source.PubSubSourceBuilder<T>
Type Parameters:
T - type of the records produced by the source

@Public public class PubSubSourceBuilder<T> extends Object
Builder for Pub/Sub sources, obtained from PubSubSource.builder().

Required settings: at least one subscription and a deserialization schema.

  • Method Details

    • subscription

      public PubSubSourceBuilder<T> subscription(SubscriptionDestination subscription)
      Adds a subscription to consume. Calling this several times, or combining it with subscriptions(SubscriptionDestination...), consumes every added subscription in one source.

      The subscription must already exist. Use subscription(SubscriptionDestination, SubscriptionCreateOptions) to have the source create it when it does not.

      Parameters:
      subscription - the subscription
      Returns:
      this builder
    • subscription

      public PubSubSourceBuilder<T> subscription(SubscriptionDestination subscription, SubscriptionCreateOptions createOptions)
      Adds a subscription to consume, creating it with the given settings if it does not exist.

      Passing options is what authorises creating this subscription; a subscription added without them must already exist. The options are per subscription because they carry the topic binding, and two subscriptions of one topic each receive a complete copy of its stream — so sharing one options object would silently duplicate every message.

      An existing subscription is left exactly as it is: these settings are not applied to it.

      Parameters:
      subscription - the subscription
      createOptions - the settings to create it with if it is absent
      Returns:
      this builder
    • subscriptions

      public PubSubSourceBuilder<T> subscriptions(SubscriptionDestination... subscriptions)
      Adds subscriptions to consume.
      Parameters:
      subscriptions - the subscriptions
      Returns:
      this builder
    • subscriptions

      public PubSubSourceBuilder<T> subscriptions(Collection<SubscriptionDestination> subscriptions)
      Adds subscriptions to consume.
      Parameters:
      subscriptions - the subscriptions
      Returns:
      this builder
    • deserializer

      public PubSubSourceBuilder<T> deserializer(PubSubDeserializationSchema<T> deserializer)
      Sets the record deserializer. Required; one message may produce any number of records, including none, which drops the message while still acknowledging it with the checkpoint that covers it.
      Parameters:
      deserializer - the deserialization schema
      Returns:
      this builder
    • orderingMode

      public PubSubSourceBuilder<T> orderingMode(OrderingMode orderingMode)
      Sets whether the source preserves ordering-key delivery order. Defaults to OrderingMode.NONE.

      OrderingMode.PER_KEY assigns each subscription to exactly one reader subtask, so source parallelism beyond the subscription count leaves subtasks idle. See the enum for the full guarantee and its cost.

      Parameters:
      orderingMode - the ordering mode
      Returns:
      this builder
    • subscriberOptions

      public PubSubSourceBuilder<T> subscriberOptions(PubSubSubscriberOptions subscriberOptions)
      Sets the subscriber tuning options: SDK flow control, the streaming-pull connection count and the acknowledgement-deadline extension settings, plus the source's drain size, subscriber shutdown budget and first-checkpoint watchdog. Optional; every knob left unset keeps the SDK's (or the source's) default.
      Parameters:
      subscriberOptions - the subscriber options
      Returns:
      this builder
    • deserializationFailurePolicy

      public PubSubSourceBuilder<T> deserializationFailurePolicy(DeserializationFailurePolicy deserializationFailurePolicy)
      Sets what the source does with a message the deserialization schema cannot convert. Defaults to DeserializationFailurePolicy.FAIL.
      Parameters:
      deserializationFailurePolicy - the failure policy
      Returns:
      this builder
    • startPosition

      public PubSubSourceBuilder<T> startPosition(PubSubStartPosition startPosition)
      Sets where the source starts consuming. Defaults to PubSubStartPosition.continueFromSubscription(), which starts wherever the subscriptions already are.

      Every other position seeks, which rewrites state shared by every consumer of the subscription — including other jobs. The seek runs once, at the first start of a job, and never on a restore. See PubSubStartPosition for the full semantics.

      Parameters:
      startPosition - where to start consuming
      Returns:
      this builder
    • serviceAccountKeyFile

      public PubSubSourceBuilder<T> serviceAccountKeyFile(String serviceAccountKeyFile)
      Authenticates the source with the service-account JSON key at the given path instead of application-default credentials. The file is read on the JobManager for subscription administration and on each TaskManager that creates a reader, so the same path must be readable by every JobManager and TaskManager that can run this source. Optional; when unset the source uses application-default credentials.

      Service-account keys are long-lived secrets. Prefer an attached service account or Workload Identity where the deployment supports one. This setting cannot be combined with emulatorEndpoint(String), whose plaintext channel deliberately carries no credentials.

      Parameters:
      serviceAccountKeyFile - the service-account JSON key-file path
      Returns:
      this builder
    • emulatorEndpoint

      public PubSubSourceBuilder<T> emulatorEndpoint(String emulatorEndpoint)
      Points the source at a Pub/Sub emulator instead of the production service. Subscribers connect to the given host:port over a plaintext channel with no credentials, so this must only ever be used against an emulator (for example a testcontainers PubSubEmulatorContainer). Optional; when unset the source connects to Pub/Sub with application-default credentials.

      The value is parsed here, so a malformed host:port is rejected on the client instead of surfacing as a connection failure once the job has been deployed.

      Parameters:
      emulatorEndpoint - the emulator endpoint as host:port
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the endpoint is not host:port with a port in 1..65535
    • restoreDeduplication

      public PubSubSourceBuilder<T> restoreDeduplication(boolean restoreDeduplication)
      Keeps a completed checkpoint's acknowledgements across a restart. Optional; off by default.

      The source acknowledges a checkpoint's messages when the checkpoint's completion notification reaches the reader. Flink delivers that notification on a best-effort basis: a job that is cancelled or fails between completing a checkpoint and notifying its readers restores that checkpoint, while Pub/Sub, never having heard the acknowledgements, redelivers its messages, which are then processed a second time. The AdaptiveScheduler, including reactive mode, usually triggers a rescale toward added resources or changed resource requirements when a checkpoint completes; every rescale it triggers that way races the notification, and one lost race duplicates a whole checkpoint interval.

      With this on, each checkpoint also records the ids of the messages it covers that are not acknowledged yet. After a restore, a redelivery of one of those messages is acknowledged instead of emitted, for restoreDeduplicationTtl(Duration). This removes the duplicates of a whole-job restore — which every AdaptiveScheduler restart is, whether it rescales up or down — and loses nothing: every suppressed message was covered by the restored checkpoint, and a redelivery that arrives after the ledger expires is emitted as it would be without this setting. A region failover is covered for the subtasks it restores; a redelivery that Pub/Sub routes to a source subtask the failover left running is emitted again.

      The cost is checkpoint state proportional to the messages emitted per checkpoint interval, and a restore that waits for the ledger to be redistributed before consuming. See the Pub/Sub connector documentation for the details and caveats, including a seek during the ledger's lifetime.

      Parameters:
      restoreDeduplication - whether to suppress redeliveries of restored messages
      Returns:
      this builder
    • restoreDeduplicationTtl

      public PubSubSourceBuilder<T> restoreDeduplicationTtl(Duration restoreDeduplicationTtl)
      Sets how long a restored deduplication ledger is kept once the reader starts consuming. Optional; requires restoreDeduplication(boolean). When unset, the ledger is kept for the subscriber's PubSubSubscriberOptions.Builder.maxAckExtensionPeriod(Duration), the longest the previous execution could have kept a message leased, capped at Duration.ofNanos(Long.MAX_VALUE), or for ten minutes, the longest acknowledgement deadline, when that extension is disabled.

      A redelivery that arrives after the ledger expires is emitted, so a longer lifetime suppresses late redeliveries, for example behind a backlog, at the cost of keeping the ids in memory for longer.

      Parameters:
      restoreDeduplicationTtl - how long to keep the restored ledger; must be positive
      Returns:
      this builder
    • build

      public org.apache.flink.api.connector.source.Source<T,SubscriptionSplit,PubSubEnumeratorState> build()
      Builds the source.

      The returned source implements LineageVertexProvider. It reports each configured subscription as pubsub / subscription:project:subscription, with the source's unboundedness and a gcp physical-resource facet. It does not look up backing topics or infer them from subscription-creation settings. Inspection performs no authentication, client creation, RPC, or deserialization.

      Flink 2.2 and 2.3 extract this metadata natively. Flink 1.20 supports direct inspection through the interface, without automatic FLIP-314 listener delivery.

      Returns:
      the source