apache_beam.io.gcp.pubsub module

Google Cloud PubSub sources and sinks.

Cloud Pub/Sub sources and sinks are currently supported only in streaming pipelines, during remote execution.

This API is currently under development and is subject to change.

class apache_beam.io.gcp.pubsub.PubsubMessage(data, attributes)[source]

Bases: future.types.newobject.newobject

Represents a Cloud Pub/Sub message.

Message payload includes the data and attributes fields. For the payload to be valid, at least one of its fields must be non-empty.

This interface is experimental. No backwards compatibility guarantees.

data

(bytes) Message data. May be None.

attributes

(dict) Key-value map of str to str, containing both user-defined and service generated attributes (such as id_label and timestamp_attribute). May be None.

class apache_beam.io.gcp.pubsub.ReadFromPubSub(topic=None, subscription=None, id_label=None, with_attributes=False, timestamp_attribute=None)[source]

Bases: apache_beam.transforms.ptransform.PTransform

A PTransform for reading from Cloud Pub/Sub.

Initializes ReadFromPubSub.

Parameters:
  • topic – Cloud Pub/Sub topic in the form “projects/<project>/topics/<topic>”. If provided, subscription must be None.
  • subscription – Existing Cloud Pub/Sub subscription to use in the form “projects/<project>/subscriptions/<subscription>”. If not specified, a temporary subscription will be created from the specified topic. If provided, topic must be None.
  • id_label – The attribute on incoming Pub/Sub messages to use as a unique record identifier. When specified, the value of this attribute (which can be any string that uniquely identifies the record) will be used for deduplication of messages. If not provided, we cannot guarantee that no duplicate data will be delivered on the Pub/Sub stream. In this case, deduplication of the stream will be strictly best effort.
  • with_attributes – True - output elements will be PubsubMessage objects. False - output elements will be of type bytes (message data only).
  • timestamp_attribute

    Message value to use as element timestamp. If None, uses message publishing time as the timestamp.

    Timestamp values should be in one of two formats:

    • A numerical value representing the number of milliseconds since the Unix epoch.
    • A string in RFC 3339 format, UTC timezone. Example: 2015-10-29T23:41:41.123Z. The sub-second component of the timestamp is optional, and digits beyond the first three (i.e., time units smaller than milliseconds) may be ignored.
expand(pvalue)[source]
to_runner_api_parameter(context)[source]
apache_beam.io.gcp.pubsub.ReadStringsFromPubSub(*args, **kwargs)[source]
apache_beam.io.gcp.pubsub.WriteStringsToPubSub(*args, **kwargs)[source]
class apache_beam.io.gcp.pubsub.WriteToPubSub(topic, with_attributes=False, id_label=None, timestamp_attribute=None)[source]

Bases: apache_beam.transforms.ptransform.PTransform

A PTransform for writing messages to Cloud Pub/Sub.

Initializes WriteToPubSub.

Parameters:
  • topic – Cloud Pub/Sub topic in the form “/topics/<project>/<topic>”.
  • with_attributes – True - input elements will be PubsubMessage objects. False - input elements will be of type bytes (message data only).
  • id_label – If set, will set an attribute for each Cloud Pub/Sub message with the given name and a unique value. This attribute can then be used in a ReadFromPubSub PTransform to deduplicate messages.
  • timestamp_attribute – If set, will set an attribute for each Cloud Pub/Sub message with the given name and the message’s publish time as the value.
static to_proto_str(element)[source]
expand(pcoll)[source]
to_runner_api_parameter(context)[source]