Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 为什么赢了';此火花样本代码是否加载在火花壳中?_Scala_Apache Spark - Fatal编程技术网

Scala 为什么赢了';此火花样本代码是否加载在火花壳中?

Scala 为什么赢了';此火花样本代码是否加载在火花壳中?,scala,apache-spark,Scala,Apache Spark,下面的示例代码来自《Spark高级分析》一书。当我将其加载到spark shell(版本1.4.1)中时,会出现以下错误,表明它找不到StatCounter: import org.apache.spark.util.StatCounter <console>:9: error: not found: type StatCounter val stats: StatCounter = new StatCounter() ^ <

下面的示例代码来自《Spark高级分析》一书。当我将其加载到spark shell(版本1.4.1)中时,会出现以下错误,表明它找不到StatCounter:

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)
问题似乎在于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)
}

我和你有完全相同的问题
我会像你一样解决它,
改变

进入

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

原因可能是系统不知道StatCounter的路径

库在类路径中吗?您能告诉我们该库的位置并打印出您的库路径吗?我发现在声明它时必须完全限定StatCounter,即使我导入了它:
val stats:org.apache.spark.util.StatCounter=new org.apache.spark.util.StatCounter()
默认情况下它在类路径中。上面的中间代码块中的Shell shell的两行示例显示。当我加载一个文件时,问题就出现了。除了scala版本错误(这会不时引起问题)之外,我不能说是什么导致了问题。当我将代码复制并保存到文件中时,我在Spark 1.4.1 shell中得到了以下信息:scala>:load/tmp/test.scala加载/tmp/test.scala。。。import org.apache.spark.util.StatCounter定义的类NAStatCounter定义的模块NAStatCounter警告:以前定义的类NAStatCounter不是对象NAStatCounter的同伴。必须一起定义同伴;您可能希望为此使用:粘贴模式。请在源代码中添加语法突出显示。
val stats: StatCounter = new StatCounter() 
val stats: org.apache.spark.util.StatCounter = new org.apache.spark.util.StatCounter()