How to get all columns after groupby on Dataset in spark sql 2.1.0

前端 未结 5 737
孤独总比滥情好
孤独总比滥情好 2020-12-29 11:13

First, I am very new to SPARK

I have millions of records in my Dataset and i wanted to groupby with name column and finding names which having maximum age. I am gett

5条回答
  •  星月不相逢
    2020-12-29 11:21

    For your solution you have to try different approach. You was almost there for solution but let me help you understand.

    Dataset resultset = studentDataSet.groupBy("name").max("age");
    

    now what you can do is you can join the resultset with studentDataSet

    Dataset joinedDS = studentDataset.join(resultset, "name");
    

    The problem with groupBy this that after applying groupBy you get RelationalGroupedDataset so it depends on what next operation you perform like sum, min, mean, max etc then the result of these operation joined with groupBy

    As in you case name column is joined with the max of age so it will return only two columns but if use apply groupBy on age and then apply max on 'age' column you will get two column one is age and second is max(age).

    Note :- code is not tested please make changes if needed Hope this clears you query

提交回复
热议问题