I'm trying to execute below code using Spark JAVA api.
sampleDS=sampleDS
.select(col("column1"),col("column2"),col("price1")col("price2"))
.groupBy(col("column1"),col("column2"))
.agg(expr("sum(price1)").as("MainPrice"),expr("sum(price2)").as("ExtPrice"))
.sort(col("column1"),col("column2"));
But this part in the code I want that to be Dynamic,
.agg(expr("sum(price1)").as("MainPrice"),expr("sum(price2)").as("ExtPrice"))
which means only if a request has ExtPrice I need ExtPrice to be there , viceversa for MainPrice
i.e.
if only MainPrice is selected the code should be .agg(expr("sum(price1)"))
or when only ExtPrice is selected .agg(expr("sum(price2)"))
I tried adding the expressions in a List. Something but I got stuck there.
Can anyone help me here sort this out?