Let's say we have two tables - A and B, whose column keys are country, name, age. I want to create a union table from A and B, called result, such that in the result table will be all the unique pairs of country and name, with a preference to pair from A.
For example, assume this is table A:
val column = Seq("country","name", "age")
val A = Seq(("New York","Smith","10"),
("Washington","Rose","5"),
("Mexico","David","1"))
val aDF = A.toDF(column:_*)
aDF.show(false)
+----------+-----+---+
|country |name |age|
+----------+-----+---+
|New York |Smith|10 |
|Washington|Rose |5 |
|Mexico |David|1 |
+----------+-----+---+
This is table B:
val B = Seq(("New York","Smith","5"),
("Florida","Smith","5"),
("Washington","Jef","5"),
("Russia","Boris","12"))
val bDF = B.toDF(column:_*)
bDF.show(false)
+----------+-----+---+
|country |name |age|
+----------+-----+---+
|New York |Smith|5 |. // Should not be included in the result table
|Florida |Smith|5 |
|Washington|Jef |5 |
|Russia |Boris|12 |
+----------+-----+---+
And the result table will be -
+----------+-----+---+
|country |name |age|
+----------+-----+---+
|New York |Smith|10 | // <New York, Smith> contained in A and B - we take from A
|Washington|Rose |5 |. // Contained in A
|Mexico |David|1 |. // Contained in A
|Florida |Smith|5 |. // Contained in B
|Washington|Jef |5 |. // Contained in B
|Russia |Boris|12 |. // Contained in B
+----------+-----+---+
How can I do that with spark?