pyspark:重新分区后出现“太多值”错误
Posted
技术标签:
【中文标题】pyspark:重新分区后出现“太多值”错误【英文标题】:pyspark: "too many values" error after repartitioning 【发布时间】:2015-11-20 22:54:52 【问题描述】:我有一个 DataFrame(被转换为 RDD)并且想要重新分区,以便每个键(第一列)都有自己的分区。这就是我所做的:
# Repartition to # key partitions and map each row to a partition given their key rank
my_rdd = df.rdd.partitionBy(len(keys), lambda row: int(row[0]))
但是,当我尝试将其映射回 DataFrame 或保存时,我收到此错误:
Caused by: org.apache.spark.api.python.PythonException: Traceback (most recent call last):
File "spark-1.5.1-bin-hadoop2.6/python/lib/pyspark.zip/pyspark/worker.py", line 111, in main
process()
File "spark-1.5.1-bin-hadoop2.6/python/lib/pyspark.zip/pyspark/worker.py", line 106, in process
serializer.dump_stream(func(split_index, iterator), outfile)
File "spark-1.5.1-bin-hadoop2.6/python/lib/pyspark.zip/pyspark/serializers.py", line 133, in dump_stream
for obj in iterator:
File "spark-1.5.1-bin-hadoop2.6/python/pyspark/rdd.py", line 1703, in add_shuffle_key
for k, v in iterator:
ValueError: too many values to unpack
at org.apache.spark.api.python.PythonRunner$$anon$1.read(PythonRDD.scala:166)
at org.apache.spark.api.python.PythonRunner$$anon$1.<init>(PythonRDD.scala:207)
at org.apache.spark.api.python.PythonRunner.compute(PythonRDD.scala:125)
at org.apache.spark.api.python.PythonRDD.compute(PythonRDD.scala:70)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:297)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:264)
at org.apache.spark.api.python.PairwiseRDD.compute(PythonRDD.scala:342)
at org.apache.spark.rdd.RDD.computeOrReadCheckpoint(RDD.scala:297)
at org.apache.spark.rdd.RDD.iterator(RDD.scala:264)
at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:73)
at org.apache.spark.scheduler.ShuffleMapTask.runTask(ShuffleMapTask.scala:41)
at org.apache.spark.scheduler.Task.run(Task.scala:88)
at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:214)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
... 1 more
更多测试表明,即使这样也会导致相同的错误: my_rdd = df.rdd.partitionBy(x) # x = 可以是 5、100 等
你们有没有遇到过这种情况。如果有,你是怎么解决的?
【问题讨论】:
【参考方案1】:partitionBy
需要一个PairwiseRDD
,它在 Python 中相当于长度为 2 的元组(列表)的RDD
,其中第一个元素是键,第二个元素是值。
partitionFunc
获取密钥并将其映射到分区号。当您在 RDD[Row]
上使用它时,它会尝试将行解压缩为键值并失败:
from pyspark.sql import Row
row = Row(1, 2, 3)
k, v = row
## Traceback (most recent call last):
## ...
## ValueError: too many values to unpack (expected 2)
即使你提供了正确的数据做这样的事情:
my_rdd = (df.rdd.map(lambda row: (int(row[0]), row)).partitionBy(len(keys))
这真的没有意义。对于DataFrames
,分区并不是特别有意义。有关详细信息,请参阅 my answer 至 How to define partitioning of DataFrame?。
【讨论】:
以上是关于pyspark:重新分区后出现“太多值”错误的主要内容,如果未能解决你的问题,请参考以下文章