Spark2 Dataset之檢視與SQL

智慧先行者發表於2016-11-25
// 建立檢視 
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|
+------+----+------+

 

相關文章