2016-01-24 9 views
7

Il seguente codice di esempio è tratto dal libro Advanced Analytics with Spark. Quando carico in scintilla-shell (versione 1.4.1) dà il seguente errore, indicando che non può trovare StatCounter:Perché questo codice di esempio Spark non viene caricato nella 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) 

Se io faccio solo quanto segue in scintilla-shell non c'è nessun problema :

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) 

Il problema sembra essere il comando: load in spark-shell.

Ecco il codice:

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

È quella libreria nella classe sentiero? Puoi dirci la posizione di quella libreria e stampare il tuo percorso lib? –

+4

Ho scoperto che devo dichiarare completamente StatCounter quando lo dichiaro, anche se l'ho importato: 'val stats: org.apache.spark.util.StatCounter = new org.apache.spark.util.StatCounter()' –

+0

È in il classpath per impostazione predefinita. L'esempio di due righe dalla shell di scintilla nel blocco di codice centrale sopra mostra questo. Quando carico un file è quando si verifica il problema. –

risposta

3

Ho esattamente lo stesso problema con voi.
ho risolto come si è tentato,
CAMBIO

val stats: StatCounter = new StatCounter() 

IN

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

il motivo forse è il sistema non si conosce il percorso di StatCounter

+0

Aggiungi l'evidenziazione della sintassi al codice sorgente. – Robson

Problemi correlati