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 13:13:12 UTC

(beam) 01/01: Avoid side input issue where Latest.globally produces 2 elements in a window

This is an automated email from the ASF dual-hosted git repository.

damccorm pushed a commit to branch users/damccorm/siFix
in repository https://gitbox.apache.org/repos/asf/beam.git

commit a811d3c5207a33d906ae482d13667c9e76779177
Author: Danny McCormick <da...@google.com>
AuthorDate: Mon May 13 09:12:59 2024 -0400

    Avoid side input issue where Latest.globally produces 2 elements in a window
---
 .../main/java/org/apache/beam/examples/snippets/Snippets.java  | 10 ++++++----
 1 file changed, 6 insertions(+), 4 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..cf9bb1b23f4 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,7 @@ public class Snippets {
 
                       @ProcessElement
                       public void process(ProcessContext c, @Timestamp Instant timestamp) {
-                        Map<String, String> keyMap = c.sideInput(map);
+                        Map<String, String> keyMap = c.sideInput(mapIterable[0]);
                         c.outputWithTimestamp(KV.of(1L, c.element()), Instant.now());
 
                         LOG.info(