0

I have a scala dataframe like this

-----------------------------------
| Code | Name | Age | ID          |
-----------------------------------
| ABC  | Alan    | 22  | 111111   |
| ABC  | Bob     | 25  | 222222   |
| DEF  | Charlie | 29  | 333333   |
| GHI  | David   | 11  | 555555   |
-----------------------------------

I want to have an output HashMap like this:

{
 'ABC': [{'Name': 'Alan',    'Age': 22', 'ID': 111111} , {'Name': 'Bob', 'Age': 25', 'ID': 22222}], 
 'DEF': [{'Name': 'Charlie', 'Age': 29', 'ID': 333333}],
 'GHI': [{'Name': 'David',   'Age': 11', 'ID': 555555}] 
}

How can I efficiently do this?

1
  • that's probably a bad idea that defeat the spark design, work with rather than try to get out of Commented Aug 13, 2021 at 13:52

1 Answer 1

1

Assuming your DataFrame is named ds, this should work:

ds.select('code, to_json(struct('name, 'age, 'id)) as "json")
  .groupBy('code).agg(collect_list('json))
  .as[(String, Array[String])]
  .collect.toMap

This will give you a Map[String, Array[String]]. If what you wanted was to turn the whole DataFrame into a single JSON, I wouldn't recommend that, but it would be doable as well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.