简体   繁体   English

如何在 spark scala 中获取 groupby 的 output 如下

[英]How to get the output of groupby as below in spark scala

After running groupby i got the result as运行 groupby 后,我得到的结果为

+--------------------+-----+
|                City|count|
+--------------------+-----+
|               Tyler|    2|
|           Fairbanks|    1|
|         Springfield|   12|
|          Charleston|    7|
|              Corona|    2|

I want the same result as below我想要与下面相同的结果

Tyler : 2
Fairbanks : 1
Springfield : 12

I have to do it in scala spark.. not pysaprk我必须在 scala spark.. 不是 pysapk

This question is a bit unclear.这个问题有点不清楚。 Please add more data like how your are printing the first output (putting the code will help).请添加更多数据,例如您如何打印第一个 output(输入代码会有所帮助)。 Rest, based on output I can think of you are just taking some values from RDD and printing on console with default println. Rest,基于 output 我可以认为您只是从 RDD 中获取一些值并使用默认 println 在控制台上打印。 If thats the case, try mapping the data based on your output requirement and then use.mkstring("\n") over it.如果是这种情况,请尝试根据您的 output 要求映射数据,然后对其使用.mkstring("\n")。 So pseudo code will look like所以伪代码看起来像

collectedResult.map(x=>s"${x._1}: ${x._2}").mkString("\n")

Note: this is just an example may not run just like that in editor.注意:这只是一个示例,可能不会像在编辑器中那样运行。 Also if the size of result is too big, this approach is not at all recommended.此外,如果结果的大小太大,则根本不推荐这种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM