和join的区别是连接后的新Dataset的schema会不一样
代码
object TypedOperation {
case class Employee(name: String, age: Long, depId: Long, gender: String, salary: Long)
case class Department(id: Long, name: String)
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 departmentPath = this.getClass.getClassLoader.getResource("department.json").getPath
val employeeDF = sparkSession.read.json(employeePath)
val departmentDF = sparkSession.read.json(departmentPath)
val employeeDS = employeeDF.as[Employee]
val departmentDS = departmentDF.as[Department]
employeeDS.joinWith(departmentDS, $"depId" === $"id").show()
}
}
网友评论