Ejemplo: Unir tablas
Unir tablas que tenga el identificador comun
val df = Seq( (1,"Paco","Garcia",24,24000), (2,"Juan","Garcia",26,27000), (3,"Lola","Martin",29,31000), (4,"Sara","Garcia",35,34000) ).toDF("id","nombre", "apellido","edad","salario") df.createOrReplaceTempView("personas") val df = Seq( (1,"Rojo","Pasta"), (2,"Amarillo","Pizza"), (3,"Azul","Patatas"), (5,"Rojo","Pizza"), (6,"Negro","Pulpo") ).toDF("id", "color","comida") df.createOrReplaceTempView("gustos") spark .sql( """select p.*, g.* |from personas p |inner join gustos g |on p.id = g.id """.stripMargin) .show
+---+------+--------+----+-------+---+--------+-------+ | id|nombre|apellido|edad|salario| id| color| comida| +---+------+--------+----+-------+---+--------+-------+ | 1| Paco| Garcia| 24| 24000| 1| Rojo| Pasta| | 2| Juan| Garcia| 26| 27000| 2|Amarillo| Pizza| | 3| Lola| Martin| 29| 31000| 3| Azul|Patatas| +---+------+--------+----+-------+---+--------+-------+
Ejemplo: Unir tablas por la derecha
spark .sql( """select p.*, g.* |from personas p |left outer join gustos g |on p.id = g.id """.stripMargin) .show
+---+------+--------+----+-------+----+--------+-------+ | id|nombre|apellido|edad|salario| id| color| comida| +---+------+--------+----+-------+----+--------+-------+ | 1| Paco| Garcia| 24| 24000| 1| Rojo| Pasta| | 2| Juan| Garcia| 26| 27000| 2|Amarillo| Pizza| | 3| Lola| Martin| 29| 31000| 3| Azul|Patatas| | 4| Sara| Garcia| 35| 34000|null| null| null| +---+------+--------+----+-------+----+--------+-------+
Ejemplo: Unir tablas por la izquierda
spark .sql( """select p.*, g.* |from personas p |right outer join gustos g |on p.id = g.id """.stripMargin) .show
+----+------+--------+----+-------+---+--------+-------+ | id|nombre|apellido|edad|salario| id| color| comida| +----+------+--------+----+-------+---+--------+-------+ | 1| Paco| Garcia| 24| 24000| 1| Rojo| Pasta| | 2| Juan| Garcia| 26| 27000| 2|Amarillo| Pizza| | 3| Lola| Martin| 29| 31000| 3| Azul|Patatas| |null| null| null|null| null| 5| Rojo| Pizza| |null| null| null|null| null| 6| Negro| Pulpo| +----+------+--------+----+-------+---+--------+-------+
0 comentarios