gpt4 book ai didi

java - 将转换应用于一个输出标签

转载 作者:行者123 更新时间:2023-11-30 05:52:07 25 4
gpt4 key购买 nike

我认为我有一个产生两个输出的函数(如果我错了,请纠正我):

PCollection<String> words = ...;

final TupleTag<String> shortWordsTag = new TupleTag<String>(){};

PCollectionTuple results =
words.apply(
ParDo
.of(new DoFn<String, String>() {
@ProcessElement
public void processElement(ProcessContext context) {
String word = context.element();
if (word.length() < 5) {
context.output(shortWordsTag, word);
} else {
context.output(word);
}

现在我想调用另一个函数,但只应用其中一个输出。像这样的事情:

results.apply(
ParDo
.of(new DoFn<String, String>() {
@ProcessElement
public void processElement(ProcessContext context) {
String word = context.element();
// do stuff, but should only have words with length < 5 here
}
)

我可以看到一些使用 withOutputTags 的示例,但此方法似乎需要多个标签(一个标签和一个标签列表),而且我不确定如何使用它我的场景。

如何指定仅针对输出到 shortWordsTag 标记的数据调用我的 results.apply

最佳答案

正如您所提到的,在 Apache Beam 中的单个转换中处理多个输出的正确方法确实是使用 PCollectionTuplewithOutputTags

在 Apache Beam 文档中,您可以找到一些非常好的示例,了解如何设置具有多个输出的转换,并为每个输出使用不同的标签:

此外,如果您访问上面第二个链接中的 4.5.2 节,您将找到有关如何在 DoFn 中发送到多个输出的示例。简而言之,使用您共享的核心代码,您将需要执行以下操作:

PCollectionTuple results = [...].withOutputTags(MAIN_TAG, LIST_OF_ADDITIONAL_TAGS);

results.get(YOUR_DESIRED_TAG).apply(...);

调用get( ) method on a PCollectionTuple将返回与您将在方法内传递的 TupleTag 关联的 PCollection。

关于java - 将转换应用于一个输出标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53724173/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com