I need to extract data from two hive tables, which are very large. They are in two different schemas, but have same definition.
I need to compare the two tables and identify following in PySpark
- rows that are present in table1, but missing in table2
- rows that are present in both tables , but there is mismatch in values in any of the non key columns
- rows that are present in table2 , but missing in table1
e.g. Let's say the table has following cols
ProductId - BigInteger - PK
ProductVersion - int - PK
ProductName - char
ProductPrice - decimal
ProductDesc - varchar
Let's say the data is as follows
Table1 in Schema1
[1, 1, "T-Shirt", 10.50, "Soft-Washed Slub-Knit V-Neck"] -> Matches with Table2
[1, 2, "T-Shirt", 10.50, "Soft-Washed Striped Crew-Neck "] -> Price is different in Table1
[2, 1, "Short Sleeve Shirt", 10.50, "Everyday Printed Short-Sleeve Shirt"] -> Missing in Table2
[3, 1, "T-Shirt", 10.50, "Breathe ON Camo Tee"] -> Prod Desc is different in Table2
Table2 in Schema2
[1, 1, "T-Shirt", 10.50, "Soft-Washed Slub-Knit V-Neck"] -> Matches with Table1
[2, 1, "Short Sleeve Shirt", 12.50, "Everyday Printed Short-Sleeve Shirt"] -> Price is different
[3, 1, "T-Shirt", 10.50, "Breathe ON Camo"] -> Prod Desc is different in Table2
[3, 2, "T-Shirt", 20, "Breathe ON ColorBlock Tee"] -> Missing in Table1
The expected result will be three separate data frames
dfOut1- will contain the rows that are present in table1 , but missing in table2 based on the primary key
["Missing in Table2", [1, 2, "T-Shirt", 10.50, "Soft-Washed Striped Crew-Neck "]]
The first column will indicate the difference type,
If the difference type is "Missing in Table1" or "Missing in Table2", the entire row from the source table will be available i
dfdiff-
["Difference", "ProductPrice", 2, 1, 10.50, 12.50]
["Difference", "ProductDesc", 3,1, "Breathe ON Camo Tee", "Breathe ON Camo"]
dfout2-
["Missing in Table1", [3, 2, "T-Shirt", 20, "Breathe ON ColorBlock Tee"]]
I am thinking of following approach
1. Create df1 from table1 using query "select * from schema1.table1"
2. Create df2 from table2 using query "select * from schema2.table2"
3. Use df1.except(df2)
I referred to the documentation
I am not sure if this approach will work
Will df1.except(df2) compare all the fields , or just the key columns ?
Also, not sure how to separate the output further