1)创建一个样例类
scala> case class Person(name: String,age: Long)
defined class Person
2)创建DataSet
scala> val ds = Seq(Person(“zhangwuji”,32)).toDS()

ds: org.apache.spark.sql.Dataset[Person] = [name: string, age: bigint]
3)将DataSet转化为DataFrame
scala> var df = ds.toDF
df: org.apache.spark.sql.DataFrame = [name: string, age: bigint]
4)展示
scala> df.show
+————-+—-+
| name|age|
+————-+—-+
|zhangwuji| 32|
+————-+—-+

代码方式

//创建RDD
val rdd: RDD[(Int, String, Int)] = spark.sparkContext.makeRDD(List((1, “banzhang”, 20), (2, “jingjing”, 18), (3, “wangqiang”, 30)))
//RDD—->DataFrame
val df: DataFrame = rdd.toDF(“id”, “name”, “age”)

//DataFrame—->DataSet
val ds: Dataset[User] = df.as[User]
//DataSet—->DataFrame—->RDD
val df1: DataFrame = ds.toDF()
ds.show()