我的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楼 票数:0

在您的代码中有两个值,但是您只想聚合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)

  ask by Shane translate from so

未解决问题?本站智能推荐:

2回复

Pyspark:对keyd的值应用reduce by key

经过一些转换后,我得到了具有以下格式的rdd: [(0, [('a', 1), ('b', 1), ('b', 1), ('b', 1)]) (1, [('c', 1), ('d', 1), ('h', 1), ('h', 1)])] 我无法弄清楚如何在此rdd的值部分上基本“ red
1回复

PySpark - (tuple,int) 值上的 reduceByKey

我有 RDD: 我想将其减少到元组中的每个键及其每个点的平均值。 例如,如果我有(小例子): 我会得到: 然后(或直接) 我试过: 和其他没有帮助或给出 RDD 错误的东西。 我该如何解决这个问题?
1回复

派斯帕克; 对列表值使用 ReduceByKey

我试图更好地理解 reduceByKey 函数,并一直在探索使用它来完成不同任务的方法。 我想应用下面显示的 RDD 数据。 一行数据的格式是一个带有名称的元组,然后是与该名称关联的所有日期的列表(下面是数据外观的副本) 为了获得与元组中每个名称关联的所有日期的计数,我应用了下面的代码,使用 r
1回复

使用reduceByKey查找RDD的最大值,然后查找其他变量的关联值

我有3个值的RDD 我想找到并返回value1最大化的整个RDD,我知道我可以做 然后将其重新加入,但我只想执行一个干净的操作,该操作将查找由密钥分组的最大值2,然后返回与这些值关联的整个RDD。 我也不想在任何情况下将数据放在数据框中 谢谢
1回复

reduceByKey和mapValues如何同时工作?

在大数据领域,我是一个崭新的火花。 我有一个代码,实际上创建了一个函数,该函数拆分CSV文件并返回两个字段。 然后是我知道它如何工作的map函数,但是我对代码的下一部分感到困惑(正在对totalsByAge变量进行操作),正在应用mapValues和reduceByKey。 请帮我了解
1回复

pyspark中的reduceByKey

我有一个类似以下的rdd: 我想编写一个reduceByKey操作,我期望类似于以下内容: 我想做的是,对于每种产品,我都会检查分数,并只返回分数最高的三个产品。 计划通过reduceByKey做到这一点
1回复

PySpark带有字典的reducebykey

在进行reducebykey转换的情况下,为什么Spark不得不从元组列表中构建RDD? 错误: 如果reduceByKey()用于键值对的集合,对我来说似乎很明显,每对都应该驻留在用于键值对的Python对象的类型中,而不是字典而不是元组。
1回复

列表作为PySpark的reduceByKey的键

我试图在格式数据(([a,b,c], 1), ([a,b,c], 1), ([a,d,b,e], 1), ...上调用pyspark的reduceByKey函数(([a,b,c], 1), ([a,b,c], 1), ([a,d,b,e], 1), ... 似乎pyspark不接受数组作为