gpt4 book ai didi

scala - 错误: value saveAsTextFile is not a member of Unit

转载 作者:行者123 更新时间:2023-12-03 08:29:11 27 4
gpt4 key购买 nike

我是Spark和Scala编程的新手。

我试图使用Scala执行简单的pagerank算法。但是我在编译时遇到了这个错误。

error: value saveAsTextFile is not a member of Unit

我已经附上了我正在使用的代码。
val output = ranks.collect()
output.foreach(tup => println(tup._1 + " has page rank: " + tup._2)).saveAsTextFile("/user/ssimhadr/ScalaWordCount_Output")

最佳答案

正如Ryan所指出的,foreach仅用于副作用。它返回Unit而不是List本身。 Ergo没有链接。

现在,您实际正在执行以下操作:

val output = ranks.collect()
val realoutput: Unit = output.foreach(tup => println(tup._1 + " has page rank: " + tup._2))
realoutput.saveAsTextFile(...)
saveAsTextFile不是 Unit的成员,您会收到错误消息

您应该这样做:
ranks.foreach(tup => println(tup._1 + " has page rank: " + tup._2))
ranks.saveAsTextFile(...)

要么
ranks.saveAsTextFile(...)
ranks.collect().foreach(tup => println(tup._1 + " has page rank: " + tup._2))

关于scala - 错误: value saveAsTextFile is not a member of Unit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27068648/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com