2017-03-28 3 views
1

저는 Spark를 처음 사용하고 있으며 스칼라에서 사용하고 있습니다. 간단한 object을 작성하여 spark-shell:load test.scala을 사용하여 정상적으로로드했습니다.스칼라/스파크의 클래스 가져 오기 오류

import org.apache.spark.ml.feature.StringIndexer 

object Collaborative{ 
    def trainModel() ={ 
     val data = sc.textFile("/user/PT/data/newfav.csv") 
     val df = data.map(_.split(",") match { 
      case Array(user,food,fav) => (user,food,fav.toDouble) 
     }).toDF("userID","foodID","favorite") 
     val userIndexer = new StringIndexer().setInputCol("userID").setOutputCol("userIndex") 
    } 
} 

이제 매개 변수를 전달하기 위해 클래스에 넣을 수 있습니다. 대신 class과 동일한 코드를 사용합니다.

import org.apache.spark.ml.feature.StringIndexer 

class Collaborative{ 
    def trainModel() ={ 
     val data = sc.textFile("/user/PT/data/newfav.csv") 
     val df = data.map(_.split(",") match { 
      case Array(user,food,fav) => (user,food,fav.toDouble) 
     }).toDF("userID","foodID","favorite") 
     val userIndexer = new StringIndexer().setInputCol("userID").setOutputCol("userIndex") 
    } 
} 

이렇게하면 가져 오기 오류가 반환됩니다.

<console>:19: error: value toDF is not a member of org.apache.spark.rdd.RDD[(String, String, Double)] 
      val df = data.map(_.split(",") match { case Array(user,food,fav) => (user,food,fav.toDouble) }).toDF("userID","foodID","favorite") 

<console>:24: error: not found: type StringIndexer 
      val userIndexer = new StringIndexer().setInputCol("userID").setOutputCol("userIndex") 

무엇이 여기에 있습니까?

답변

0

이 하나를 시도해보십시오,이 하나가 정상적으로 작동하는 것 같습니다.

def trainModel() ={ 
    val spark = SparkSession.builder().appName("test").master("local").getOrCreate() 
    import spark.implicits._ 
    val data = spark.read.textFile("/user/PT/data/newfav.csv") 
    val df = data.map(_.split(",") match { 
     case Array(user,food,fav) => (user,food,fav.toDouble) 
    }).toDF("userID","foodID","favorite") 
    val userIndexer = new StringIndexer().setInputCol("userID").setOutputCol("userIndex") 
    }