![](/img/trans.png)
[英]How do I set the coder for a PCollection<List<String>> in Apache Beam?
[英]Apache Beam: Flattening PCollection<List<Foo>> to PCollection<Foo>
假設我們有一些嵌套列表:
["a", "b"]
["c", "d"]
我們可以像這樣在 Stream API 中輕松地做襟翼圖:
Stream
.of(List.of("a", "b"), List.of("c", "d"))
.flatMap(List::stream)
.forEach(System.out::println);
但是用“FlatMapElements”做這件事,真是一團糟:
Pipeline pipeline = Pipeline.create();
pipeline.apply(Create.of(List.of(List.of("a", "b"), List.of("c", "d"))))
.apply(FlatMapElements.into(TypeDescriptor.of(String.class)).via(list -> list))
.apply(ParDo.of(new SomeOutputFunction()));
我們可以用平面地圖功能做其他更好的事情嗎?
一個簡單的平面地圖工作不應該那么復雜,所以我想我錯過了一些東西。
由於類型推斷問題,我什至無法將.via(list -> list)
替換為.via(Function.identity())
。
“.apply(Flatten.iterables())” 它將“PCollection<List>”轉換為“PCollection”
請參考 Apache Beam 編程指南https://beam.apache.org/documentation/programming-guide/#flatten :
PCollectionList<String> collections = PCollectionList.of(pc1).and(pc2).and(pc3);
PCollection<String> merged = collections.apply(Flatten.<String>pCollections());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.