How to solve Duplicate values exception when I create PCollectionView>

后端 未结 1 1362
暗喜
暗喜 2021-01-06 06:57

I\'m setting up a slow-changing lookup Map in my Apache-Beam pipeline. It continuously updates the lookup map. For each key in lookup map, I retrieve the latest value in t

相关标签:
1条回答
  • 2021-01-06 07:23

    Specifically with regards to view.asMap and accumulating panes discussion in the comments:

    If you would like to make use of the View.asMap side input (for example, when the source of the map elements is itself distributed – often because you are creating a side input from the output of a previous transform), there are some other factors that will need to be taken into consideration: View.asMap is itself an aggregation, it will inherit triggering and accumulate its input. In this specific pattern, setting the pipeline to accumulatingPanes mode before this transform will result in duplicate key errors even if a transform such as Latest.perKey is used before the View.asMap transform.

    Given the read updates the whole map, then the use of View.asSingleton would I think be a better approach for this use case.

    Some general notes around this pattern, which will hopefully be useful for others as well:

    For this pattern we can use the GenerateSequence source transform to emit a value periodically for example once a day. Pass this value into a global window via a data-driven trigger that activates on each element. In a DoFn, use this process as a trigger to pull data from your bounded source Create your SideInput for use in downstream transforms.

    It's important to note that because this pattern uses a global-window side input triggering on processing time, matching to elements being processed in event time will be nondeterministic. For example if we have a main pipeline which is windowed on event time, the version of the SideInput View that those windows will see will depend on the latest trigger that has fired in processing time rather than the event time.

    Also important to note that in general the side input should be something that fits into memory.

    Java (SDK 2.9.0):

    In the sample below the side input is updated at very short intervals, this is so that effects can be easily seen. The expectation is that the side input is updating slowly, for example every few hours or once a day.

    In the example code below we make use of a Map that we create in a DoFn which becomes the View.asSingleton, this is the recommended approach for this pattern.

    The sample below illustrates the pattern, please note the View.asSingleton is rebuilt on every counter update.

    public static void main(String[] args) {
    
     // Create pipeline
     PipelineOptions options = PipelineOptionsFactory.fromArgs(args).withValidation()
         .as(PipelineOptions.class);
    
     // Using View.asSingleton, this pipeline uses a dummy external service as illustration.
     // Run in debug mode to see the output
     Pipeline p = Pipeline.create(options);
    
     // Create slowly updating sideinput
    
     PCollectionView<Map<String, String>> map = p
         .apply(GenerateSequence.from(0).withRate(1, Duration.standardSeconds(5L)))
    
         .apply(Window.<Long>into(new GlobalWindows())
             .triggering(Repeatedly.forever(AfterProcessingTime.pastFirstElementInPane()))
             .discardingFiredPanes())
    
         .apply(ParDo.of(new DoFn<Long, Map<String, String>>() {
           @ProcessElement public void process(@Element Long input,
               OutputReceiver<Map<String, String>> o) {
             // Do any external reads needed here...
             // We will make use of our dummy external service.
             // Every time this triggers, the complete map will be replaced with that read from 
             // the service.
             o.output(DummyExternalService.readDummyData());
           }
    
         })).apply(View.asSingleton());
    
     // ---- Consume slowly updating sideinput
    
     // GenerateSequence is only used here to generate dummy data for this illustration.
     // You would use your real source for example PubSubIO, KafkaIO etc...
     p.apply(GenerateSequence.from(0).withRate(1, Duration.standardSeconds(1L)))
         .apply(Window.into(FixedWindows.of(Duration.standardSeconds(1))))
         .apply(Sum.longsGlobally().withoutDefaults())
         .apply(ParDo.of(new DoFn<Long, KV<Long, Long>>() {
    
           @ProcessElement public void process(ProcessContext c) {
             Map<String, String> keyMap = c.sideInput(map);
             c.outputWithTimestamp(KV.of(1L, c.element()), Instant.now());
    
      LOG.debug("Value is {} key A is {} and key B is {}"
    , c.element(), keyMap.get("Key_A"),keyMap.get("Key_B"));
    
           }
         }).withSideInputs(map));
    
     p.run();
    }
    
    public static class DummyExternalService {
    
     public static Map<String, String> readDummyData() {
    
       Map<String, String> map = new HashMap<>();
       Instant now = Instant.now();
    
       DateTimeFormatter dtf = DateTimeFormat.forPattern("HH:MM:SS");
    
       map.put("Key_A", now.minus(Duration.standardSeconds(30)).toString(dtf));
       map.put("Key_B", now.minus(Duration.standardSeconds(30)).toString());
    
       return map;
    
     }
    }
    
    0 讨论(0)
提交回复
热议问题