【问题标题】:Java Spark UDAF to return Struct Field \ multiple columnsJava Spark UDAF 返回结构字段\多列
【发布时间】:2017-07-07 18:20:51
【问题描述】:

我正在编写一个 UDAF 聚合函数,我想返回一个数据类型,它可以是具有列名的结构(例如 start 和 end 都是 long 类型)或 2 列。

在评估函数中试图返回一个映射类型和一个数组,但这不是我所期望的。

很想知道这件事的线索。 谢谢

【问题讨论】:

标签: java apache-spark user-defined-functions


【解决方案1】:

最简单的方法是在一个字段中返回一个包含您的值的列表,然后将其展开为几列。

您可以在此处阅读 UDAF 尝试返回两个整数列的示例:


UDAF(重要代码部分)


public YourUDAFName(someParams) {
    [...]
    _returnDataType = DataTypes.createArrayType(DataTypes.IntegerType);
}
[...]
@Override
public Object evaluate(Row buffer) {
    List<Integer> output = new ArrayList<>();
    output.add(1); //Here put your logical...
    output.add(5); // "
    return output;    
}

使用示例...


Dataset<Row> ds = getYourDatasetHere();
YourUDAFName udaf = new YourUDAFName(someParams);
ds.groupBy("yourGroupByKey")
.agg(udaf .apply(
    col("someColumnFromDs"),
    col("someOtherColumn")).as("columnWithList"));

// Here we expand the "columnWithList"...
List<Column> newColumns = new ArrayList<>();
for (int i = 0; i < numElementInTheList; i++) {
    ds = ds.withColumn("nameOfYourExpandedColumn", ds.col("outputByIntervals").getItem(i));
}
ds.show();

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-02
    • 2017-03-27
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    相关资源
    最近更新 更多