You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by da...@apache.org on 2024/05/13 18:51:35 UTC
(beam) branch master updated: Avoid side input issue where Latest.globally produces 2 elements in a window (#31263)
This is an automated email from the ASF dual-hosted git repository.
damccorm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git
The following commit(s) were added to refs/heads/master by this push:
new 296c13a291d Avoid side input issue where Latest.globally produces 2 elements in a window (#31263)
296c13a291d is described below
commit 296c13a291dd9ce9638c69d0e7d4874968c438fd
Author: Danny McCormick <da...@google.com>
AuthorDate: Mon May 13 13:51:28 2024 -0500
Avoid side input issue where Latest.globally produces 2 elements in a window (#31263)
* Avoid side input issue where Latest.globally produces 2 elements in a window
* Correct var
* Fix compile
---
.../java/org/apache/beam/examples/snippets/Snippets.java | 14 +++++++++-----
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/examples/java/src/main/java/org/apache/beam/examples/snippets/Snippets.java b/examples/java/src/main/java/org/apache/beam/examples/snippets/Snippets.java
index 3ca431455a0..4f24c69f74b 100644
--- a/examples/java/src/main/java/org/apache/beam/examples/snippets/Snippets.java
+++ b/examples/java/src/main/java/org/apache/beam/examples/snippets/Snippets.java
@@ -606,8 +606,10 @@ public class Snippets {
// Run in debug mode to see the output.
Pipeline p = Pipeline.create();
- // Create a side input that updates each second.
- PCollectionView<Map<String, String>> map =
+ // Create a side input that updates every 5 seconds.
+ // View as an iterable, not singleton, so that if we happen to trigger more
+ // than once before Latest.globally is computed we can handle both elements.
+ PCollectionView<Iterable<Map<String, String>>> mapIterable =
p.apply(GenerateSequence.from(0).withRate(1, Duration.standardSeconds(5L)))
.apply(
ParDo.of(
@@ -628,7 +630,7 @@ public class Snippets {
.triggering(Repeatedly.forever(AfterProcessingTime.pastFirstElementInPane()))
.discardingFiredPanes())
.apply(Latest.globally())
- .apply(View.asSingleton());
+ .apply(View.asIterable());
// Consume side input. GenerateSequence generates test data.
// Use a real source (like PubSubIO or KafkaIO) in production.
@@ -641,7 +643,9 @@ public class Snippets {
@ProcessElement
public void process(ProcessContext c, @Timestamp Instant timestamp) {
- Map<String, String> keyMap = c.sideInput(map);
+ Iterable<Map<String, String>> si = c.sideInput(mapIterable);
+ // Take an element from the side input iterable (likely length 1)
+ Map<String, String> keyMap = si.iterator().next();
c.outputWithTimestamp(KV.of(1L, c.element()), Instant.now());
LOG.info(
@@ -651,7 +655,7 @@ public class Snippets {
keyMap.get("Key_A"));
}
})
- .withSideInputs(map));
+ .withSideInputs(mapIterable));
p.run();
}