gpt4 book ai didi

java - Scala-Spark : save outcome into a file

转载 作者:太空宇宙 更新时间:2023-11-04 12:34:50 25 4
gpt4 key购买 nike

我正在使用 Scala API 在 Spark 中运行序列模型。这是查看结果的代码行:

model.freqSequences.collect().foreach { freqSequence => println(freqSequence.sequence.map(_.mkString("[", ", ", "]")).mkString("[", ", ", "]") + ", " + freqSequence.freq)}

问题是结果越来越大,不想再使用collect(),而是将其保存在HDFS或本地的文件中。我试过这个:

scala> val outcome = model.freqSequences.foreach { freqSequence => println(freqSequence.sequence.map(_.mkString("[", ", ", "]")).mkString("[", ", ", "]") + ", " + freqSequence.freq)}

scala> outcome.saveAsTextFile("tmp/outcome1/")

error: saveAsTextFile is not a member of Unit

结果是一个单位,我无法使用 saveAsTextFile。还有什么办法可以挽救这个结果吗?发送。

最佳答案

foreach 返回一个 Unit

您希望首先映射String,以便可以另存为文件。像这样的东西:

val outcome = model.freqSequences.map { freqSequence => freqSequence.sequence.map(_.mkString("[", ", ", "]")).mkString("[", ", ", "]") + ", " + freqSequence.freq}
// print
outcome.foreach(println)
// save
outcome.saveAsTextFile("tmp/outcome1/")

关于java - Scala-Spark : save outcome into a file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37444021/

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