except:获取在当前dataset中有,但是在另外一个dataset中没有的元素
filter:根据我们自己的逻辑,如果返回true,那么就保留该元素,否则就过滤掉该元素
intersect:获取两个数据集的交集
代码
object TypedOperation {
case class Employee(name: String, age: Long, depId: Long, gender: String, salary: Long)
def main(args: Array[String]): Unit = {
val sparkSession = SparkSession
.builder()
.appName("BasicOperation")
.master("local")
.getOrCreate()
import sparkSession.implicits._
import org.apache.spark.sql.functions._
val employeePath = this.getClass.getClassLoader.getResource("employee.json").getPath
val employee2Path = this.getClass.getClassLoader.getResource("employee2.json").getPath
val employeeDF = sparkSession.read.json(employeePath)
val employee2DF = sparkSession.read.json(employee2Path)
val employeeDS = employeeDF.as[Employee]
val employee2DS = employee2DF.as[Employee]
employeeDS.except(employee2DS).show()
employeeDS.filter(employee => employee.age > 30).show()
employeeDS.intersect(employee2DS).show()
}
}
网友评论