当前位置:   article > 正文

error: value foreach is not a member of Object

value foreach is not a member of object

spark3.0.1,scala 2.12使用foreachPartition报错:error: value foreach is not a member of Object

代码:

df.foreachPartition(partition=>partition.foreach(println))

报错:
value foreach is not a member of Object
    df.foreachPartition(partition=>partition.foreach(println))

这个报错看不出什么东西,因为还不是真正的错误信息,将代码改为:

df.foreachPartition(partition=>println(1))

报错:

ambiguous reference to overloaded definition,
both method foreachPartition in class Dataset of type (func: org.apache.spark.api.java.function.ForeachPartitionFunction[org.apache.spark.sql.Row])Unit
and  method foreachPartition in class Dataset of type (f: Iterator[org.apache.spark.sql.Row] => Unit)Unit
match argument types (Object => Unit)
   df.foreachPartition(partition=>println(1))

这里说模糊引用了重载定义的方法foreachPartition,那我们就显示的指定foreachPartition里面的函数即可

将代码改为:

  1. df.foreachPartition((partition:Iterator[Row])=>{
  2. partition.foreach(println)
  3. })

执行成功!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/899607
推荐阅读
相关标签
  

闽ICP备14008679号