繁体   English   中英

具有多个值的reduceByKey

reduceByKey with multiple values

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文繁体   英文版本   中英对照 版本,有任何建议请联系yoyou2525@163.com。

我的rdd中的每个值都是一个元组:

temp = clustering.map(lambda x: (x[0][0], (1,1)))
temp.take(10)

[(0, (1, 1)),
 (0, (1, 1)),
 (6, (1, 1)),
 (0, (1, 1)),
 (0, (1, 1)),
 (0, (1, 1)),
 (0, (1, 1)),
 (0, (1, 1)),
 (7, (1, 1)),
 (0, (1, 1))]

然后尝试通过密钥减少它:

temp.reduceByKey(lambda a,b: (a[1]+b[1])).collect()

并得到此错误:

    ---------------------------------------------------------------------------
Py4JJavaError                             Traceback (most recent call last)
<ipython-input-33-237802845981> in <module>
----> 1 temp.reduceByKey(lambda a,b: (a[1]+b[1])).collect()

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\rdd.py in collect(self)
    814         """
    815         with SCCallSiteSync(self.context) as css:
--> 816             sock_info = self.ctx._jvm.PythonRDD.collectAndServe(self._jrdd.rdd())
    817         return list(_load_from_socket(sock_info, self._jrdd_deserializer))
    818 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\py4j\java_gateway.py in __call__(self, *args)
   1255         answer = self.gateway_client.send_command(command)
   1256         return_value = get_return_value(
-> 1257             answer, self.gateway_client, self.target_id, self.name)
   1258 
   1259         for temp_arg in temp_args:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\py4j\protocol.py in get_return_value(answer, gateway_client, target_id, name)
    326                 raise Py4JJavaError(
    327                     "An error occurred while calling {0}{1}{2}.\n".
--> 328                     format(target_id, ".", name), value)
    329             else:
    330                 raise Py4JError(

Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 35.0 failed 1 times, most recent failure: Lost task 0.0 in stage 35.0 (TID 28, localhost, executor driver): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\Lib\site-packages\pyspark\python\lib\pyspark.zip\pyspark\worker.py", line 377, in main
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\Lib\site-packages\pyspark\python\lib\pyspark.zip\pyspark\worker.py", line 372, in process
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\rdd.py", line 2499, in pipeline_func
    return func(split, prev_func(split, iterator))
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\rdd.py", line 2499, in pipeline_func
    return func(split, prev_func(split, iterator))
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\rdd.py", line 352, in func
    return f(iterator)
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\rdd.py", line 1861, in combineLocally
    merger.mergeValues(iterator)
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\Lib\site-packages\pyspark\python\lib\pyspark.zip\pyspark\shuffle.py", line 240, in mergeValues
    d[k] = comb(d[k], v) if k in d else creator(v)
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\util.py", line 99, in wrapper
    return f(*args, **kwargs)
  File "<ipython-input-33-237802845981>", line 1, in <lambda>
TypeError: 'int' object is not subscriptable

    at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:452)
    at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRunner.scala:588)
    at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRunner.scala:571)
    at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.hasNext(PythonRunner.scala:406)
    at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
    at scala.collection.Iterator$GroupedIterator.fill(Iterator.scala:1124)
    at scala.collection.Iterator$GroupedIterator.hasNext(Iterator.scala:1130)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:409)
    at org.apache.spark.shuffle.sort.BypassMergeSortShuffleWriter.write(BypassMergeSortShuffleWriter.java:125)
    at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:99)
    at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:55)
    at org.apache.spark.scheduler.Task.run(Task.scala:121)
    at org.apache.spark.executor.Executor$TaskRunner$$anonfun$10.apply(Executor.scala:408)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1360)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:414)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

Driver stacktrace:
    at org.apache.spark.scheduler.DAGScheduler.org$apache$spark$scheduler$DAGScheduler$$failJobAndIndependentStages(DAGScheduler.scala:1889)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1877)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$abortStage$1.apply(DAGScheduler.scala:1876)
    at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
    at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
    at org.apache.spark.scheduler.DAGScheduler.abortStage(DAGScheduler.scala:1876)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:926)
    at org.apache.spark.scheduler.DAGScheduler$$anonfun$handleTaskSetFailed$1.apply(DAGScheduler.scala:926)
    at scala.Option.foreach(Option.scala:257)
    at org.apache.spark.scheduler.DAGScheduler.handleTaskSetFailed(DAGScheduler.scala:926)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.doOnReceive(DAGScheduler.scala:2110)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2059)
    at org.apache.spark.scheduler.DAGSchedulerEventProcessLoop.onReceive(DAGScheduler.scala:2048)
    at org.apache.spark.util.EventLoop$$anon$1.run(EventLoop.scala:49)
    at org.apache.spark.scheduler.DAGScheduler.runJob(DAGScheduler.scala:737)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2061)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2082)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2101)
    at org.apache.spark.SparkContext.runJob(SparkContext.scala:2126)
    at org.apache.spark.rdd.RDD$$anonfun$collect$1.apply(RDD.scala:945)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:151)
    at org.apache.spark.rdd.RDDOperationScope$.withScope(RDDOperationScope.scala:112)
    at org.apache.spark.rdd.RDD.withScope(RDD.scala:363)
    at org.apache.spark.rdd.RDD.collect(RDD.scala:944)
    at org.apache.spark.api.python.PythonRDD$.collectAndServe(PythonRDD.scala:166)
    at org.apache.spark.api.python.PythonRDD.collectAndServe(PythonRDD.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
    at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
    at py4j.Gateway.invoke(Gateway.java:282)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\Lib\site-packages\pyspark\python\lib\pyspark.zip\pyspark\worker.py", line 377, in main
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\Lib\site-packages\pyspark\python\lib\pyspark.zip\pyspark\worker.py", line 372, in process
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\rdd.py", line 2499, in pipeline_func
    return func(split, prev_func(split, iterator))
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\rdd.py", line 2499, in pipeline_func
    return func(split, prev_func(split, iterator))
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\rdd.py", line 352, in func
    return f(iterator)
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\rdd.py", line 1861, in combineLocally
    merger.mergeValues(iterator)
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\Lib\site-packages\pyspark\python\lib\pyspark.zip\pyspark\shuffle.py", line 240, in mergeValues
    d[k] = comb(d[k], v) if k in d else creator(v)
  File "C:\Users\helmis\AppData\Local\Continuum\anaconda3\lib\site-packages\pyspark\util.py", line 99, in wrapper
    return f(*args, **kwargs)
  File "<ipython-input-33-237802845981>", line 1, in <lambda>
TypeError: 'int' object is not subscriptable

    at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:452)
    at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRunner.scala:588)
    at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRunner.scala:571)
    at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.hasNext(PythonRunner.scala:406)
    at org.apache.spark.InterruptibleIterator.hasNext(InterruptibleIterator.scala:37)
    at scala.collection.Iterator$GroupedIterator.fill(Iterator.scala:1124)
    at scala.collection.Iterator$GroupedIterator.hasNext(Iterator.scala:1130)
    at scala.collection.Iterator$$anon$11.hasNext(Iterator.scala:409)
    at org.apache.spark.shuffle.sort.BypassMergeSortShuffleWriter.write(BypassMergeSortShuffleWriter.java:125)
    at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:99)
    at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:55)
    at org.apache.spark.scheduler.Task.run(Task.scala:121)
    at org.apache.spark.executor.Executor$TaskRunner$$anonfun$10.apply(Executor.scala:408)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1360)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:414)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    ... 1 more

试过这个:

temp.reduceByKey(lambda a,b: print(a)).collect()

并得到:

[(0, None),
 (6, None),
 (7, None),
 (8, None),
 (5, None),
 (3, None),
 (9, None),
 (1, (1, 1)),
 (2, (1, 1)),
 (4, None)]

所以,不知道怎么了! 感谢您的帮助。

我想要最终得到的是:temp.reduceByKey(lambda a,b:(a [0] + b [0],a [1] + b [1]))。collect()

1 个回复

在您的代码中有两个值,但是您只想聚合value-2。 因此,您编写的代码如下:

temp.reduceByKey(lambda a,b:(a [1] + b [1]))。collect()

错误原因:由于代码中缺少value-1的位置,因此其抛出错误。 您必须在代码中指定value-1(a [0]),即如下所示:

temp.reduceByKey(lambda a,b:(a [0],a [1] + b [1]))。take(20)

1 reduceByKey可以用于多个值吗?

我刚刚开始学习spark和scala,并测试了RDD的一些转换和操作。 我发现reduceByKey的用法如下: 其中,它采用两个相同值的元组,并使用加法运算将它们组合在一起。 现在我的问题是我们是否不能一次累加两个以上的值,或者一次只能将所有具有相同键的值相加? 像这样: ...

3 如何用3个值减少reduceByKey?

我试图遍历文本文件的RDD,对文件中的每个唯一单词求和,然后累加每个唯一单词后面的所有单词及其计数。 到目前为止,这就是我所拥有的: 如果到目前为止我还不清楚,我想做的就是累积文件中每个单词之后的单词集,以及所述单词跟随其前一个单词的次数,形式为: (PrecedingWo ...

4 指定值元素上的ReduceByKey

Spark的reduceByKey ,并试图了解reduceByKey ,它被指定为接受RDD [(K,V)]。 我不清楚的是当值是列表/元组时如何应用此功能... 经过各种映射和过滤操作后,我的RDD最终以(Cluster:String, (Unique_ID:String, Coun ...

5 每次reduceByKey返回不同的值

我有一个键值数据,我们称它为x 。 它由一个钥匙以及一对体积和重量组成。 看起来像这样 我想为每个t1和t2计算加权体积值。 那是我计算 t1的2 * 0.8 + 3 * 0.1 + 4 * 0.3 t2的3 * 0.8 + 10 * 0.3 我可以 我会 ...

6 ReduceByKey对元组的可迭代值

我正在尝试计算特定日期的特定物品的外观。 我输入的结构是Date\\tItem1:AppearencesOfItem1,...,ItemN:AppearencesOfItemN 例 为此,我创建以下PairRdd结构: 然后在导致以下结果的日期使用groupByKey ...

7 Spark RDD:多个reducebykey或仅一次

我有如下代码: rst1和rst2获得样本结果。 我以为rst1需要更多的内存(100次),但只有一个reduceByKey转换; 但是,rst2需要较少的内存,但需要更多的reduceByKey转换(99次)。 那么,这是时间与空间的权衡游戏吗? 我的问题是:我上面的分析是 ...

8 如何reduceByKey?

我正在Cloudera QuickStart VM中使用Spark控制台。 下面提供了一个输出文件。 它显示前20条记录。 每条记录都是电视频道名称及其相应观看者数量的组合。 有几百条记录。 目标是按电视频道名称对RDD(channel_views)进行分组,以便每个记录都是电 ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2022 STACKOOM.COM