2016-08-11 27 views
0

任何想法,爲什麼斯巴克的REPL(火花2.0.0)將被拋出了以下錯誤:價值:負載星火的斯卡拉REPL

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) 
       ^

回答

0

您不使用K & R括號,並且由於repl逐行解釋,所以左括號不是類的主體。

嘗試class NAStatCounter {,即,大括號在同一行上。

否則,class C本身就是一個完整的定義。

+0

切換到K&R產生一個不同的錯誤: 進口org.apache.spark.util.StatCounter :13:錯誤:未找到:類型StatCounter的 VAL統計:StatCounter的新= StatCounter的() :13:錯誤:未發現:類型StatCounter的 VAL統計:StatCounter的=新StatCounter的() :26:錯誤:未發現:NAStatCounter 高清應用類型。(X:雙人間)=新NAStatCounter()加(X) – Ikhalaf

+0

其實用':paste'修復了大括號問題並將同伴定義在一起。您可能正在使用Spark shell正確執行導入操作。我會稍後嘗試;除非有人先回答。 –

+0

正確,:粘貼確實解決了這兩個問題。在這一點上,我試圖理解爲什麼這是出於好奇。謝謝。 – Ikhalaf