Spark2 Dataset之视图与SQL

// 创建视图 
data.createOrReplaceTempView("Affairs")

val df1 = spark.sql("SELECT * FROM Affairs WHERE age BETWEEN 20 AND 25")
df1: org.apache.spark.sql.DataFrame = [affairs: double, gender: string ... 7 more fields]

// 子查询
val df2 = spark.sql("select gender, age,rating from  ( SELECT * FROM Affairs WHERE age BETWEEN 20 AND 25 ) t ")
df2: org.apache.spark.sql.DataFrame = [gender: string, age: double ... 1 more field]

df2.show
+------+----+------+
|gender| age|rating|
+------+----+------+
|  male|22.0|   3.0|
|female|22.0|   3.0|
|  male|22.0|   5.0|
+------+----+------+

 

posted @ 2016-11-25 17:01  智能先行者  阅读(2551)  评论(0编辑  收藏  举报