繁体   English   中英

如何在 Apache Beam Java SDK 中的多列上使用aggregateField()?

[英]How to use aggregateField() over multiple columns in Apache Beam Java SDK?

在 Apache 光束 Python SDK 中,可以执行以下操作:

input
| GroupBy(account=lambda s: s["account"])
.aggregate_field(lambda x: x["wordsAddup"] - x["wordsSubtract"], sum, 'wordsRead')

我们如何在 Java SDK 中执行类似的操作? 奇怪的是,编程指南中只有 Python中用于此转换的示例。

这是我在 Java 中生成等效项的尝试:

input.apply(
Group.byFieldNames("account")
.aggregateField(<INSERT EQUIVALENT HERE>, Sum.ofIntegers(), "wordsRead"));

https://beam.apache.org/documentation/programming-guide/#using-schemas中有一些 Java 示例。 (Note you may have to select the java tab on a selector that has both Java and Python to see them.)

在 Java 中,我认为 aggregateField 的第一个参数不能采用任意表达式; 它必须是字段名称。 您可以使用为所需表达式添加新字段的投影来继续分组操作。 例如

input
    .apply(SqlTransform.query(
        "SELECT *, wordsAddup - wordsSubtract AS wordsDiff from PCOLLECTION")
    .apply(Group.byFieldNames("account")
        .aggregateField("wordsDiff", Sum.ofIntegers(), "wordsRead"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM