Spark: Sort an RDD by multiple values in a tuple / columns

不羁的心 提交于 2019-12-11 01:04:20

问题


So I have an RDD as follows

RDD[(String, Int, String)]

And as an example

    ('b', 1, 'a')
    ('a', 1, 'b')
    ('a', 0, 'b')
    ('a', 0, 'a')

The final result should look something like

('a', 0, 'a')
('a', 0, 'b')
('a', 1, 'b')
('b', 1, 'a')

How would I do something like this?


回答1:


Try this:

rdd.sortBy(r => r)

If you wanted to switch the sort order around, you could do this:

rdd.sortBy(r => (r._3, r._1, r._2))

For reverse order:

rdd.sortBy(r => r, false)


来源:https://stackoverflow.com/questions/36393224/spark-sort-an-rdd-by-multiple-values-in-a-tuple-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!