spark reduce and map issue

BufBills

I am doing a small experiment in Spark and I am having troubles.

wordCounts is : [('rat', 2), ('elephant', 1), ('cat', 2)]


# TODO: Replace <FILL IN> with appropriate code
from operator import add
totalCount = (wordCounts
              .map(lambda x: (x,1))   <==== something wrong with this line maybe
              .reduce(sum))            <====omething wrong with this line maybe
average = totalCount / float(wordsRDD.map(lambda x: (x,1)).reduceByKey(add).count())
print totalCount
print round(average, 2)

# TEST Mean using reduce (3b)
Test.assertEquals(round(average, 2), 1.67, 'incorrect value of average')
BufBills

I figured out my solution:

from operator import add
totalCount = (wordCounts
              .map(lambda x: x[1])
              .reduce(add))
average = totalCount / float(wordsRDD.map(lambda x: (x,1)).reduceByKey(add).count())
print totalCount
print round(average, 2)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Spark:在 SparkSql 中使用 map 和 reduce

来自分类Dev

缓存是Spark优于map-reduce的唯一优势吗?

来自分类Dev

一起使用Spark和Map-Reduce

来自分类Dev

使用Spark进行连续的逐行处理而不使用map和reduce

来自分类Dev

使用map.reduce给出SPARK-5063错误,但在Interactive Shell中运行良好

来自分类Dev

$(...).. map(...)。reduce不是函数

来自分类Dev

RethinkDB Map / Reduce示例

来自分类Dev

Map-reduce 概念

来自分类Dev

RavenDB map reduce,reduce中的重复条目

来自分类Dev

是否可以在没有运行任何Map / Reduce(/ Yarn)的情况下针对Hive表执行Spark SQL?

来自分类Dev

Apache Spark中的reduce()与fold()

来自分类Dev

使用map reduce实施小组

来自分类Dev

节点中的Map-Reduce

来自分类Dev

Hadoop Map Reduce哈希程序

来自分类Dev

使用UnderscoreJS进行Map Reduce

来自分类Dev

mongo db的map reduce功能

来自分类Dev

使用reduce fnt代替map

来自分类Dev

Hadoop Map-Reduce编程

来自分类Dev

使用UnderscoreJS进行Map Reduce

来自分类Dev

Map Reduce程序中的问题

来自分类Dev

MongoDB Map Reduce对象字段

来自分类Dev

Map Reduce中的关键值

来自分类Dev

使用map / reduce计算总数

来自分类Dev

在 Erlang 中用 Reduce 实现 Map

来自分类Dev

Apache Spark:map与mapPartitions?

来自分类Dev

对map reduce中的reduce函数的行为感到困惑

来自分类Dev

Spark:reduce和reduceByKey之间的语义差异

来自分类Dev

spark 1.0.0 DStream.reduce行为异常

来自分类Dev

在数组数组的map()中使用reduce()