使用时未找到的值:在Spark的Scala REPL中加载

时间:2016-08-11 18:35:15

标签: scala apache-spark read-eval-print-loop

任何想法为什么Spark的REPL(Spark 2.0.0)会抛出以下错误:

scala> :load ../StatsWithMissing.scala
Loading ../StatsWithMissing.scala...
import org.apache.spark.util.StatCounter
defined class NAStatCounter
<console>:22: error: illegal start of statement (no modifiers allowed here)
   override def toString: String =
   ^
<console>:26: error: not found: value stats
           "stats " + stats.toString + "NaN " + missing
<console>:26: error: not found: value missing
       "stats " + stats.toString + "NaN " + missing

<console>:1: error: eof expected but '}' found.
}
^
defined object NAStatCounter
warning: previously defined class NAStatCounter is not a companion to object     NAStatCounter.
Companions must be defined together; you may wish to use :paste mode for this.
<console>:27: error: value add is not a member of NAStatCounter
     def apply(x: Double) = new NAStatCounter().add(x)

加载以下文件时:

import org.apache.spark.util.StatCounter

class NAStatCounter extends Serializable 
{
  val stats: StatCounter = new StatCounter()
  var missing: Long = 0

  def add(x: Double): NAStatCounter = 
  {
    if (java.lang.Double.isNaN(x))
      missing += 1
    else
      stats.merge(x)

    this
  }

  def merge(other: NAStatCounter): NAStatCounter =
  {
    stats.merge(other.stats)
    missing += other.missing
    this
  }

  override def toString: String = 
  {
    "stats " + stats.toString + "NaN " + missing
  }
}

object NAStatCounter extends Serializable 
{
  def apply(x: Double) = new NAStatCounter().add(x)
}

使用:Load ../StatsWithMissing.scala不会产生任何错误,但在尝试创建NAStatCounter时,我收到以下错误:

scala> :paste ../StatsWithMissing.scala
Pasting file ../StatsWithMissing.scala...
import org.apache.spark.util.StatCounter
defined class NAStatCounter
defined object NAStatCounter

scala> val nas1 = NAStatCounter(10.0)
<console>:28: error: reference to NAStatCounter is ambiguous;
it is imported twice in the same scope by
import $line48$read.NAStatCounter
and import INSTANCE.NAStatCounter
       val nas1 = NAStatCounter(10.0)
                  ^

1 个答案:

答案 0 :(得分:0)

你没有使用K&amp; R大括号,并且由于repl逐行解释,所以左大括号不是该类的主体。

尝试class NAStatCounter {,即支撑在同一条线上。

否则,class C本身就是一个完整的定义。