How to do aggregate functions, may columns and extract back

If you are going to make all the groupBy with the same columns, and you want to create all the aggregation column references as avg(col_interested ).as(col_interested_avg) with all the elements of columns_interestedList you can create all the references with the stream, and pass them to the gag method.

List<Column> avgCols = columns_interestedList.stream()
.map(col_interested -> avg(col_interested).as(col_interested + "_avg"))
.collect(Collectors.toList());

dataDs.groupBy("grp_col_1","grp_col_1")
.agg(avgCols.add(count((*).as("col_interested_count").toArray(new Column[0]));

Leave a Comment