【问题标题】:Is dataframe.show() an action in spark?dataframe.show() 是火花中的动作吗?
【发布时间】:2021-10-06 18:28:46
【问题描述】:

我有以下代码:

val df_in = sqlcontext.read.json(jsonFile) // the file resides in hdfs

//some operations in here to create df as df_in with two more columns "terms1" and "terms2" 

val intersectUDF = udf( (seq1:Seq[String], seq2:Seq[String] ) => {     seq1 intersect seq2 } ) //intersects two sequences
val symmDiffUDF = udf( (seq1:Seq[String], seq2:Seq[String] ) => { (seq1 diff seq2) ++ (seq2 diff seq1) } ) //compute the difference of two sequences

val df1 = (df.withColumn("termsInt", intersectUDF(df("terms1"), df1("terms2") ) )
             .withColumn("termsDiff", symmDiffUDF(df("terms1"),     df1("terms2") ) )
             .where( size(col("termsInt")) >0 && size(col("termsDiff")) > 0 && size(col("termsDiff")) <= 2 )
             .cache()
           ) // add the intersection and difference columns and filter the resulting DF 

df1.show()
df1.count()

show() 之前,该应用程序运行正常且快速,但在count() 步骤中,它会创建40000 个任务。

我的理解是df1.show() 应该触发完整的df1 创建,df1.count() 应该非常快。我在这里想念什么?为什么count() 这么慢?

非常感谢您, 罗克珊娜

【问题讨论】:

    标签: apache-spark


    【解决方案1】:

    show 确实是一个动作,但它足够聪明,知道什么时候不需要运行所有东西。如果你有一个orderBy,那也需要很长时间,但在这种情况下,你所有的操作都是映射操作,所以不需要计算整个决赛桌。然而,count 需要物理地遍历整个表才能计算它,这就是它需要这么长时间的原因。您可以通过在df1 的定义中添加orderBy 来测试我所说的内容——那么它应该需要很长时间。

    编辑:另外,40k 任务可能是由于您的 DF 被划分到的分区数量。尝试使用df1.repartition(&lt;a sensible number here, depending on cluster and DF size&gt;) 并再次尝试计数。

    【讨论】:

    • 是的,确实... orderBy 需要很长时间。
    • coalesce 可能比repartition 更好,因为它避免了随机播放。
    • 为未来做笔记:)
    【解决方案2】:

    show() 默认只显示 20 行。如果第 1 个分区返回的行数超过 20 行,则不会执行其余分区。

    注意show 有很多变化。如果你运行show(false),这意味着显示所有结果,所有分区都将被执行,可能需要更多时间。所以,show() 等于 show(20),这是一个部分操作

    【讨论】:

    • 这意味着如果我运行 show() 或说 show(10),只有数据帧中的一个分区被带到驱动程序并显示记录对吗? @thebluephantom
    • @subhayang 如果第一个分区有足够的记录,否则它将连续查找其他分区。
    猜你喜欢
    • 2011-01-22
    • 2017-11-08
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多