CombineByKey失败

时间:2019-10-07 15:51:34

标签: python-3.x apache-spark pyspark aggregation

我正在从O'Reilly Learning Spark教科书中复制并粘贴确切的代码,但出现错误:org.apache.spark.SparkException:由于阶段失败,作业中止了

我试图了解这段代码在做什么,但是由于无法运行而难以理解:

nums = sc.parallelize([1, 2, 3, 4])
sumCount = nums.combineByKey((lambda x: (x,1)),
 (lambda x, y: (x[0] + y, x[1] + 1)),
 (lambda x, y: (x[0] + y[0], x[1] + y[1])))

sumCount.map(lambda key, xy: (key, xy[0]/xy[1])).collectAsMap()

下面是完整错误,您有何见解?

Job aborted due to stage failure: Task 3 in stage 26.0 failed 1 times, most recent failure: Lost task 3.0 in stage 26.0 (TID 73, localhost, executor driver): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "/databricks/spark/python/pyspark/worker.py", line 480, in main
    process()
  File "/databricks/spark/python/pyspark/worker.py", line 470, in process
    out_iter = func(split_index, iterator)
  File "/databricks/spark/python/pyspark/rdd.py", line 2543, in pipeline_func
    return func(split, prev_func(split, iterator))
  File "/databricks/spark/python/pyspark/rdd.py", line 353, in func
    return f(iterator)
  File "/databricks/spark/python/pyspark/rdd.py", line 1905, in combineLocally
    merger.mergeValues(iterator)
  File "/databricks/spark/python/pyspark/shuffle.py", line 238, in mergeValues
    for k, v in iterator:
TypeError: 'int' object is not iterable

    at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.handlePythonException(PythonRunner.scala:514)
    at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRunner.scala:650)
    at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRunner.scala:633)
    at org.apache.spark.api.python.BasePythonRunner$ReaderIterator.hasNext(PythonRunner.scala:468)
    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.doRunTask(Task.scala:139)
    at org.apache.spark.scheduler.Task.run(Task.scala:112)
    at org.apache.spark.executor.Executor$TaskRunner$$anonfun$13.apply(Executor.scala:497)
    at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1526)
    at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:503)
    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:

1 个答案:

答案 0 :(得分:1)

好吧。

假设上面的数字不是那么好,因为它不是(K,V)元组,则假定代码如下:

 data = sc.parallelize( [(0, 2.), (0, 4.), (1, 0.), (1, 10.), (1, 20.)] )

 sumCount = data.combineByKey(lambda value: (value, 1),
                              lambda x, value: (x[0] + value, x[1] + 1),
                              lambda x, y: (x[0] + y[0], x[1] + y[1]))

 averageByKey = sumCount.map(lambda (label, (value_sum, count)): (label, value_sum / count))

 print averageByKey.collectAsMap()

在Spark下,使用python2(pyspark),以上代码可以正常运行。

在Spark下,使用python3(pyspark),以上代码在以下位置生成错误:

 averageByKey = sumCount.map(lambda (label, (value_sum, count)): (label, value_sum / count))

https://www.python.org/dev/peps/pep-3113/解释了为什么在Python 3中删除了“元组参数解包”功能。这似乎让我有些失望。

解决该问题的简单方法是将上述代码在线传递到https://www.pythonconverter.com/中并运行代码转换器。这是:

data         = sc.parallelize( [(0, 2.), (0, 4.), (1, 0.), (1, 10.), (1, 20.)] )

sumCount     = data.combineByKey(lambda value: (value, 1),
                                 lambda x, value: (x[0] + value, x[1] + 1),
                                 lambda x, y: (x[0] + y[0], x[1] + y[1]))

averageByKey = sumCount.map(lambda label_value_sum_count: (label_value_sum_count[0], label_value_sum_count[1][0] / label_value_sum_count[1][1]))

print(averageByKey.collectAsMap()) 

正确返回:

{0: 3.0, 1: 10.0}

averageByKey现在有一个不同的声明。您需要学习和阅读该链接,并熟悉Python 2 to 3 Converter。节省一些时间,您可以轻松地进入它。尊敬的SO会员保险柜与此也有一些问题,所以就在那里,不是那么简单。