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 =
.map(col_interested -> avg(col_interested).as(col_interested + "_avg"))

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

Leave a Comment