当前位置:首页 - Spark

Spark sortByKey算子学习使用详解

作者:高景洋 日期:2020-10-25 11:32:54 浏览次数:1303

sortByKey:

     按Key进行排序


示例代码:


def my_sortByKey():
    data = ['hello spark', 'hello world', 'hello world']
    rdd = sc.parallelize(data)
    map_rdd = rdd.flatMap(lambda line: line.split(' ')).map(lambda x: (x, 1))
    reduce_by_key = map_rdd.reduceByKey(lambda a,b:a+b)
    sort_by_rdd = reduce_by_key.map(lambda x:(x[1],x[0])) 
 print(sort_by_rdd.sortByKey().map(lambda x:(x[1],x[0])).collect())

输出结果:
[('spark', 1), ('world', 2), ('hello', 3)]

本文永久性链接:
<a href="http://r4.com.cn/art153.aspx">Spark sortByKey算子学习使用详解</a>
当前header:Host: r4.com.cn X-Host1: r4.com.cn X-Host2: r4.com.cn X-Host3: 127.0.0.1:8080 X-Forwarded-For: 44.210.83.132 X-Real-Ip: 44.210.83.132 X-Domain: r4.com.cn X-Request: GET /art153.aspx HTTP/1.1 X-Request-Uri: /art153.aspx Connection: close User-Agent: CCBot/2.0 (https://commoncrawl.org/faq/) Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 If-Modified-Since: Thu, 01 Jun 2023 09:32:29 GMT Accept-Encoding: br,gzip