2016-01-24 52 views
7

下面的示例代碼來自Advanced Analytics with Spark一書。當我將其加載到火花外殼(1.4.1版),它提供了以下錯誤,表明它無法找到StatCounter的:Spark-shell爲什麼不加載Spark示例代碼?

import org.apache.spark.util.StatCounter 
<console>:9: error: not found: type StatCounter 
     val stats: StatCounter = new StatCounter() 
       ^
<console>:9: error: not found: type StatCounter 
     val stats: StatCounter = new StatCounter() 
            ^
<console>:23: error: not found: type NAStatCounter 
     def apply(x: Double) = new NAStatCounter().add(x) 

如果我只是做了以下火花殼沒有問題:

scala> import org.apache.spark.util.StatCounter 
import org.apache.spark.util.StatCounter 

scala> val statsCounter: StatCounter = new StatCounter() 
statsCounter: org.apache.spark.util.StatCounter = (count: 0, mean: 0.000000, stdev: NaN, max: -Infinity, min: Infinity) 

該問題似乎與在spark-shell中的load命令相同。

下面的代碼:

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 = { 
     "stats: " + stats.toString + " NaN: " + missing 
    } 
} 

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

的路徑在類庫路徑?你能告訴我們該庫的位置並打印出你的lib路徑嗎? –

+4

我發現我必須在聲明它時完全限定StatCounter,即使我導入它:'val stats:org.apache.spark.util.StatCounter = new org.apache.spark.util.StatCounter()' –

+0

它在類路徑默認。上面中間代碼塊中spark-shell的兩行示例顯示了這一點。當我加載文件的時候是問題發生的時候。 –

回答

3

我有完全相同的問題與您聯繫。
我解決它,你嘗試過,
CHANGE

val stats: StatCounter = new StatCounter() 

INTO

val stats: org.apache.spark.util.StatCounter = new org.apache.spark.util.StatCounter() 

的原因可能是系統不知道StatCounter的

+0

請將語法高亮添加到您的源代碼中。 – Robson

相關問題