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