当前位置:首页 - Spark

Spark sortByKey算子学习使用详解

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

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: 18.191.120.103 X-Real-Ip: 18.191.120.103 X-Domain: r4.com.cn X-Request: GET /art153.aspx HTTP/1.1 X-Request-Uri: /art153.aspx Connection: close Accept: */* User-Agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com) Cookie: aoji_uuid=97c4c024af0b464ab1398c0a45e2cead_20241222; aoji_session_uuid=5d0434738be944dfacca2e7d60a34df7_20241222; PHPSESSID=t2oqlpns1aalk12n4bau9b3ufi Accept-Encoding: gzip, br, zstd, deflate