pyspark .show() 有效,但 .collect() 无效

Posted

技术标签:

【中文标题】pyspark .show() 有效,但 .collect() 无效【英文标题】:pyspark .show() works but .collect() does not 【发布时间】:2018-02-07 17:19:00 【问题描述】:

我有一个非常简单的 pyspark 程序,它使用数据框从一组 ORC 文件中查询数据。我在 Windows 中使用 anaconda python 并在其上安装了 pyspark。

程序是这样的:

from pyspark.sql import SparkSession

spark_session = SparkSession.builder.appName("test").getOrCreate()
df_orc = spark_session .read.orc("./raw_data/")
df_orc.createOrReplaceTempView("orc")

这很好用:

spark.sql("select count(*) from orc").show()

但这会产生错误:

spark.sql("select count(*) from orc").collect()

错误信息是:

WARN Utils: Truncated the string representation of a plan since it was too large. This behavior can be adjusted by setting 'spark.debug.maxToStringFields' in SparkEnv.conf.

Py4JJavaError: An error occurred while calling o81.collectToPython.
: java.lang.IllegalArgumentException
        at org.apache.xbean.asm5.ClassReader.<init>(Unknown Source)
        at org.apache.xbean.asm5.ClassReader.<init>(Unknown Source)
        at org.apache.xbean.asm5.ClassReader.<init>(Unknown Source)
        at org.apache.spark.util.ClosureCleaner$.getClassReader(ClosureCleaner.scala:46)
        at org.apache.spark.util.FieldAccessFinder$$anon$3$$anonfun$visitMethodInsn$2.apply(ClosureCleaner.sca
        at org.apache.spark.util.FieldAccessFinder$$anon$3$$anonfun$visitMethodInsn$2.apply(ClosureCleaner.sca
        at scala.collection.TraversableLike$WithFilter$$anonfun$foreach$1.apply(TraversableLike.scala:733)
        at scala.collection.mutable.HashMap$$anon$1$$anonfun$foreach$2.apply(HashMap.scala:103)
        at scala.collection.mutable.HashMap$$anon$1$$anonfun$foreach$2.apply(HashMap.scala:103)
        at scala.collection.mutable.HashTable$class.foreachEntry(HashTable.scala:230)
        at scala.collection.mutable.HashMap.foreachEntry(HashMap.scala:40)
        at scala.collection.mutable.HashMap$$anon$1.foreach(HashMap.scala:103)
        at scala.collection.TraversableLike$WithFilter.foreach(TraversableLike.scala:732)
        at org.apache.spark.util.FieldAccessFinder$$anon$3.visitMethodInsn(ClosureCleaner.scala:426)
        at org.apache.xbean.asm5.ClassReader.a(Unknown Source)
        at org.apache.xbean.asm5.ClassReader.b(Unknown Source)
        at org.apache.xbean.asm5.ClassReader.accept(Unknown Source)
        at org.apache.xbean.asm5.ClassReader.accept(Unknown Source)
        at org.apache.spark.util.ClosureCleaner$$anonfun$org$apache$spark$util$ClosureCleaner$$clean$14.apply(
        at org.apache.spark.util.ClosureCleaner$$anonfun$org$apache$spark$util$ClosureCleaner$$clean$14.apply(
        at scala.collection.immutable.List.foreach(List.scala:381)
        at org.apache.spark.util.ClosureCleaner$.org$apache$spark$util$ClosureCleaner$$clean(ClosureCleaner.sc
        at org.apache.spark.util.ClosureCleaner$.clean(ClosureCleaner.scala:156)
        at org.apache.spark.SparkContext.clean(SparkContext.scala:2294)
        at org.apache.spark.SparkContext.runJob(SparkContext.scala:2068)
        at org.apache.spark.SparkContext.runJob(SparkContext.scala:2094)
        at org.apache.spark.rdd.RDD$$anonfun$collect$1.apply(RDD.scala:936)
        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:362)
        at org.apache.spark.rdd.RDD.collect(RDD.scala:935)
        at org.apache.spark.sql.execution.SparkPlan.executeCollect(SparkPlan.scala:278)
        at org.apache.spark.sql.Dataset$$anonfun$collectToPython$1.apply$mcI$sp(Dataset.scala:2808)
        at org.apache.spark.sql.Dataset$$anonfun$collectToPython$1.apply(Dataset.scala:2805)
        at org.apache.spark.sql.Dataset$$anonfun$collectToPython$1.apply(Dataset.scala:2805)
        at org.apache.spark.sql.execution.SQLExecution$.withNewExecutionId(SQLExecution.scala:65)
        at org.apache.spark.sql.Dataset.withNewExecutionId(Dataset.scala:2828)
        at org.apache.spark.sql.Dataset.collectToPython(Dataset.scala:2805)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.base/java.lang.reflect.Method.invoke(Unknown Source)
        at py4j.reflection.MethodInvoker.invoke(MethodInvoker.java:244)
        at py4j.reflection.ReflectionEngine.invoke(ReflectionEngine.java:357)
        at py4j.Gateway.invoke(Gateway.java:280)
        at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
        at py4j.commands.CallCommand.execute(CallCommand.java:79)
        at py4j.GatewayConnection.run(GatewayConnection.java:214)
        at java.base/java.lang.Thread.run(Unknown Source)

【问题讨论】:

df_orc.count() 工作吗? 没有。类似的错误消息。 再观察:同样的代码在 ubuntu linux 中运行良好,anaconda python 也一样。 可能是你的java版本问题。 java 13 或 14 上的 collect() 似乎有问题。== 【参考方案1】:

为什么可以正常工作的原因:

spark.sql("select count(*) from orc").show()

是因为.show() 仅适用于数据的前 5 行

但是当你运行时:

spark.sql("select count(*) from orc").collect()

.collect() 将适用于您的所有数据

来自您的错误消息:

WARN Utils: Truncated the string representation of a plan since it was too large. This behavior can be adjusted by setting 'spark.debug.maxToStringFields' in SparkEnv.conf.

根据spark documentation:

为宽模式创建和记录字符串的性能开销可能很大。为了限制影响,我们限制了默认包含的字段数量。这可以通过在 SparkEnv 中设置 'spark.debug.maxToStringFields' conf 来覆盖。

但是,它可能会影响你的工作表现,所以你需要这样的东西:

spark = SparkSession 
.builder 
.master('local[*]') 
.appName('Notebook') 
.config('spark.sql.debug.maxToStringFields', 200) 
.getOrCreate()

这是一个新的 SQL 配置 spark.sql.debug.maxToStringFields,它控制 truncatedString 剪切其输入序列的最大字段数。

默认值为:DEFAULT_MAX_TO_STRING_FIELDS = 25

您也可以将spark.sql.debug.maxToStringFields=100添加到spark-defaults.conf

以前版本的 Spark 使用 spark.debug.maxToStringFields 而不是 spark.sql.debug.maxToStringFields

【讨论】:

以上是关于pyspark .show() 有效,但 .collect() 无效的主要内容,如果未能解决你的问题,请参考以下文章

PySpark 用户定义函数 (UDF) 创建新列

何时使用 UDF 与 PySpark 中的函数? [复制]

在另一列pyspark中创建具有字符串长度的列

Pyspark Dataframe 将条件添加到`reduce(add,(F.col(x) ... `

数据类型转换火花数据框列 - pyspark

PySpark 数据框如何使用平面图