簡體   English   中英

訪問 PCollectionView 的元素<list<foo> &gt;: 谷歌雲數據流/Apache Beam </list<foo>

[英]Access elements of PCollectionView<List<Foo>> : Google Cloud Dataflow/Apache Beam

我有一個 PCollection,我想將其作為輔助輸入傳遞並在 ParDo 中訪問它的元素。

所以我創建了一個 PCollectionView 作為:

final PCollectionView<List<Foo>> view =
    myPCollection.apply(View.asList());

但是,當通過側面輸入時,如何在 ParDo 中訪問它的元素呢?

一個例子真的很有幫助。

謝謝你

這段代碼主要來自Beam programming guide

final PCollectionView<List<Foo>> view =
               myPCollection.apply(View.asList());


PCollection<String> resultingPCollection =
someOtherPCollection.apply(ParDo
    .of(new DoFn<String, String>() {
        @ProcessElement
        public void processElement(ProcessContext c) {
          List<Foo> mySideInput = c.sideInput(view);
          // Do something with side input
        }
    }).withSideInputs(view)
);

如果不想使用匿名 DoFn,也可以將 PCollectionView 作為其構造函數的一部分傳遞,並在 processElement 函數中訪問它。 像這樣:

final PCollectionView<List<Foo>> view =
           myPCollection.apply(View.asList());


PCollection<String> resultingPCollection =
          someOtherPCollection.apply(ParDo
              .of(new MyDoFn(view)).withSideInputs(view));

class MyDoFn extends DoFn<String, String> {
  final PCollectionView<List<Foo>> view;

  MyDoFn(PCollectionView<List<Foo>> view) {
    this.view = view;
  }

  @ProcessElement
  public void processElement(ProcessContext c) {
    List<Foo> mySideInput = c.sideInput(this.view);
   // Do something with side input
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM