Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.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 我不断得到错误:value toDF不是org.apache.spark.rdd.rdd的成员_Scala_Apache Spark - Fatal编程技术网

Scala 我不断得到错误:value toDF不是org.apache.spark.rdd.rdd的成员

Scala 我不断得到错误:value toDF不是org.apache.spark.rdd.rdd的成员,scala,apache-spark,Scala,Apache Spark,我写了“importsqlcontext.implicits.\ux”;然而,它仍然不起作用。它就在火花壳里。为什么在这种情况下是不对的?我见过许多其他方法将rdd转换为数据帧,但我的大部分代码都是以toDF()的形式编写的。如何使toDF工作?错误: import org.apache.spark.ml.evaluation.RegressionEvaluator import org.apache.spark.ml.recommendation.ALS import org.apache.s

我写了“importsqlcontext.implicits.\ux”;然而,它仍然不起作用。它就在火花壳里。为什么在这种情况下是不对的?我见过许多其他方法将rdd转换为数据帧,但我的大部分代码都是以toDF()的形式编写的。如何使toDF工作?错误:

import org.apache.spark.ml.evaluation.RegressionEvaluator
import org.apache.spark.ml.recommendation.ALS
import org.apache.spark.ml.tuning.{ParamGridBuilder, CrossValidator}
import org.apache.spark.sql.SQLContext
import org.apache.spark.sql.functions._
import org.apache.spark.sql.types.DoubleType
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
import sqlContext.implicits._
import sys.process._

"rm -f ./ml-1m.zip".!
"wget http://files.grouplens.org/datasets/movielens/ml-1m.zip".!

"ls ./ml-1m.zip".!

"rm -r ./ml-1m".!
"unzip ml-1m.zip".!

"ls ./ml-1m".!

val ratings_raw = sc.textFile("./ml-1m/ratings.dat")
ratings_raw.takeSample(false,10, seed=0).foreach(println)

case class Rating(userId: Int, movieId: Int, rating: Float)
val ratings = ratings_raw.map(x => x.split("::")).map(r => Rating(r(0).toInt, r(1).toInt, r(2).toFloat)).toDF().na.drop()

如果您在spark shell中,则不需要创建新的SQLContext

val sqlContext = new org.apache.spark.sql.SQLContext(sc)
你可以直接使用spark


scala> import spark.implicits._

scala> val ratings_raw = sc.textFile("./ml-1m/ratings.dat")
ratings_raw: org.apache.spark.rdd.RDD[String] = ./ml-1m/ratings.dat MapPartitionsRDD[1] at textFile at <console>:38

scala> case class Rating(userId: Int, movieId: Int, rating: Float)
defined class Rating

scala> val ratings = ratings_raw.map(x => x.split("::")).map(r => Rating(r(0).toInt, r(1).toInt, r(2).toFloat)).toDF().na.drop()
ratings: org.apache.spark.sql.DataFrame = [userId: int, movieId: int ... 1 more field]

scala> ratings
res3: org.apache.spark.sql.DataFrame = [userId: int, movieId: int ... 1 more field]

scala> ratings.printSchema
root
 |-- userId: integer (nullable = false)
 |-- movieId: integer (nullable = false)
 |-- rating: float (nullable = false)


scala>导入spark.implicits_
scala>val ratings_raw=sc.textFile(“./ml-1m/ratings.dat”)
ratings_raw:org.apache.spark.rdd.rdd[String]=./ml-1m/ratings.dat MapPartitionsRDD[1]位于文本文件38处
scala>案例类评级(userId:Int,movieId:Int,评级:Float)
限定等级
scala>val ratings=ratings\u raw.map(x=>x.split(“:”).map(r=>Rating(r(0).toInt,r(1).toInt,r(2).toFloat)).toDF().na.drop()
评级:org.apache.spark.sql.DataFrame=[userId:int,movieId:int…1更多字段]
scala>评级
res3:org.apache.spark.sql.DataFrame=[userId:int,movieId:int…还有一个字段]
scala>ratings.printSchema
根
|--userId:integer(nullable=false)
|--movieId:integer(nullable=false)
|--额定值:浮动(可为空=假)

如果您在spark shell中,则不需要创建新的SQLContext

val sqlContext = new org.apache.spark.sql.SQLContext(sc)
你可以直接使用spark


scala> import spark.implicits._

scala> val ratings_raw = sc.textFile("./ml-1m/ratings.dat")
ratings_raw: org.apache.spark.rdd.RDD[String] = ./ml-1m/ratings.dat MapPartitionsRDD[1] at textFile at <console>:38

scala> case class Rating(userId: Int, movieId: Int, rating: Float)
defined class Rating

scala> val ratings = ratings_raw.map(x => x.split("::")).map(r => Rating(r(0).toInt, r(1).toInt, r(2).toFloat)).toDF().na.drop()
ratings: org.apache.spark.sql.DataFrame = [userId: int, movieId: int ... 1 more field]

scala> ratings
res3: org.apache.spark.sql.DataFrame = [userId: int, movieId: int ... 1 more field]

scala> ratings.printSchema
root
 |-- userId: integer (nullable = false)
 |-- movieId: integer (nullable = false)
 |-- rating: float (nullable = false)


scala>导入spark.implicits_
scala>val ratings_raw=sc.textFile(“./ml-1m/ratings.dat”)
ratings_raw:org.apache.spark.rdd.rdd[String]=./ml-1m/ratings.dat MapPartitionsRDD[1]位于文本文件38处
scala>案例类评级(userId:Int,movieId:Int,评级:Float)
限定等级
scala>val ratings=ratings\u raw.map(x=>x.split(“:”).map(r=>Rating(r(0).toInt,r(1).toInt,r(2).toFloat)).toDF().na.drop()
评级:org.apache.spark.sql.DataFrame=[userId:int,movieId:int…1更多字段]
scala>评级
res3:org.apache.spark.sql.DataFrame=[userId:int,movieId:int…还有一个字段]
scala>ratings.printSchema
根
|--userId:integer(nullable=false)
|--movieId:integer(nullable=false)
|--额定值:浮动(可为空=假)

我试过你的代码,效果很好

然而,我使用的spark会话如下所示

val spark = SparkSession.builder
            .master("local")
            .appName("test1")
            .getOrCreate()
而不是弃用

val sqlContext = new org.apache.spark.sql.SQLContext(sc)

我试过你的代码,效果很好

然而,我使用的spark会话如下所示

val spark = SparkSession.builder
            .master("local")
            .appName("test1")
            .getOrCreate()
而不是弃用

val sqlContext = new org.apache.spark.sql.SQLContext(sc)

您使用的是哪个版本的Spark?Spark version=2.3.0如果您查看,您将看到,由于
2.0.0
此类已“弃用”。您应该改为使用,并像这样导入隐式
import spark.implicits.\u
您能给我发送导入SparkSession的确切方法吗,因为我使用了此解决方案,但仍然无法工作
import org.apache.spark.sql.sparksSession//val sqlContext=new org.apache.spark.sql.sqlContext(sc)导入spark.implicits.\u
错误为:错误:需要稳定标识符,但找到了此。$line7$read.spark.implicits。导入spark.implicits.\u^您需要首先实例化
SparkSession
,并将值分配给名为spark的val(按惯例)。在shell中,您不需要这样做,因为(正如Joohnnie已经说过的)shell已经为您提供了这样的实例。如果您正在编写一个应用程序,那么您需要使用中显示的
builder
对其进行实例化,您还可以阅读。您使用的是Spark的哪个版本?Spark version=2.3.0如果您查看,您会发现,由于
2.0.0
这个类是“不推荐的”。您应该改为使用,并像这样导入隐式
import spark.implicits.\u
您能给我发送导入SparkSession的确切方法吗,因为我使用了此解决方案,但仍然无法工作
import org.apache.spark.sql.sparksSession//val sqlContext=new org.apache.spark.sql.sqlContext(sc)导入spark.implicits.\u
错误为:错误:需要稳定标识符,但找到了此。$line7$read.spark.implicits。导入spark.implicits.\u^您需要首先实例化
SparkSession
,并将值分配给名为spark的val(按惯例)。在shell中,您不需要这样做,因为(正如Joohnnie已经说过的)shell已经为您提供了这样的实例。如果您正在编写一个应用程序,那么您需要使用中显示的
builder
对其进行实例化,您也可以阅读。@MehdiSelbi,我使用的是Spark版本2.4。0@MehdiSelbi,我使用了Spark 2.4.0版