簡體   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