当前位置:首页 - Spark

Spark action常用算子学习使用详解

作者:高景洋 日期:2020-10-25 14:20:58 浏览次数:1359

action常用算子:

    collect

    count

    take

    reduce

    saveAsTextFile

    foeach


示例代码:

def my_action():
    data = [1,2,3,4,5,6,7,8,9,10]
    rdd1 = sc.parallelize(data) 
print(rdd1.collect()) 
print('最大值:{}'.format(rdd1.max())) 
print('最小值:{}'.format(rdd1.min())) 
print('求和:{}'.format(rdd1.sum())) 
print('相邻的两个相加:{}'.format(rdd1.reduce(lambda a,b:a+b))) 
print('元素总数:{}'.format(rdd1.count())) 
print('取前三个:{}'.format(rdd1.take(3)))
    rdd1.foreach(lambda x:print(x))

输出结果:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
最大值:10
最小值:1
求和:55
相邻的两个相加:55
元素总数:10
取前三个:[1, 2, 3]
7
8
5
6
9
10
3
4
1
2


本文永久性链接:
<a href="http://r4.com.cn/art157.aspx">Spark action常用算子学习使用详解</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: 3.144.9.152 X-Real-Ip: 3.144.9.152 X-Domain: r4.com.cn X-Request: GET /art157.aspx HTTP/1.1 X-Request-Uri: /art157.aspx Connection: close Accept: */* User-Agent: Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com) Accept-Encoding: gzip, br, zstd, deflate