Skip to content

Commit 34d536d

Browse files
author
Nikita Grover
committed
Apply yapf formatting
1 parent 75816e7 commit 34d536d

3 files changed

Lines changed: 22 additions & 3 deletions

File tree

sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/ExternalWrite.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static class Configuration {
5353
private String topic;
5454
private @Nullable String idAttribute;
5555
private @Nullable String timestampAttribute;
56+
private boolean publishWithOrderingKey = false;
5657

5758
public void setTopic(String topic) {
5859
this.topic = topic;
@@ -65,6 +66,10 @@ public void setIdLabel(@Nullable String idAttribute) {
6566
public void setTimestampAttribute(@Nullable String timestampAttribute) {
6667
this.timestampAttribute = timestampAttribute;
6768
}
69+
70+
public void setPublishWithOrderingKey(Boolean publishWithOrderingKey) {
71+
this.publishWithOrderingKey = publishWithOrderingKey != null && publishWithOrderingKey;
72+
}
6873
}
6974

7075
public static class WriteBuilder
@@ -85,6 +90,9 @@ public PTransform<PCollection<byte[]>, PDone> buildExternal(Configuration config
8590
if (config.timestampAttribute != null) {
8691
writeBuilder.setTimestampAttribute(config.timestampAttribute);
8792
}
93+
if (config.publishWithOrderingKey) {
94+
writeBuilder.setPublishWithOrderingKey(true);
95+
}
8896
writeBuilder.setDynamicDestinations(false);
8997
return writeBuilder.build();
9098
}

sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubIO.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,10 @@ public void startBundle(StartBundleContext c) throws IOException {
17271727
this.pubsubClient =
17281728
getPubsubClientFactory()
17291729
.newClient(
1730-
getTimestampAttribute(), null, c.getPipelineOptions().as(PubsubOptions.class));
1730+
getTimestampAttribute(),
1731+
null,
1732+
c.getPipelineOptions().as(PubsubOptions.class),
1733+
Write.this.getPubsubRootUrl());
17311734
}
17321735

17331736
@ProcessElement

sdks/python/apache_beam/io/external/gcp/pubsub.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ def expand(self, pbegin):
117117
# this is not implemented yet on the Java side:
118118
# ('with_attributes', bool),
119119
('timestamp_attribute', typing.Optional[str]),
120+
('publish_with_ordering_key', bool),
120121
])
121122

122123

@@ -135,6 +136,7 @@ def __init__(
135136
with_attributes=False,
136137
id_label=None,
137138
timestamp_attribute=None,
139+
publish_with_ordering_key=False,
138140
expansion_service=None):
139141
"""Initializes ``WriteToPubSub``.
140142
@@ -150,18 +152,24 @@ def __init__(
150152
in a ReadFromPubSub PTransform to deduplicate messages.
151153
timestamp_attribute: If set, will set an attribute for each Cloud Pub/Sub
152154
message with the given name and the message's publish time as the value.
155+
publish_with_ordering_key: If True, enables ordering key support when
156+
publishing messages. The ordering key must be set on each
157+
PubsubMessage via the ``ordering_key`` attribute. Requires
158+
messages to be routed to the same region.
153159
"""
154160
self.params = WriteToPubsubSchema(
155161
topic=topic,
156162
id_label=id_label,
157163
# with_attributes=with_attributes,
158-
timestamp_attribute=timestamp_attribute)
164+
timestamp_attribute=timestamp_attribute,
165+
publish_with_ordering_key=publish_with_ordering_key)
159166
self.expansion_service = expansion_service
160167
self.with_attributes = with_attributes
161168

162169
def expand(self, pvalue):
163170
if self.with_attributes:
164-
pcoll = pvalue | 'ToProto' >> Map(pubsub.WriteToPubSub.to_proto_str)
171+
pcoll = pvalue | 'ToProto' >> Map(
172+
lambda m: m._to_proto_str(for_publish=True))
165173
else:
166174
pcoll = pvalue | 'ToProto' >> Map(
167175
lambda x: pubsub.PubsubMessage(x, {})._to_proto_str())

0 commit comments

Comments
 (0)