How can we compare two dataframes in spark scala to find difference between these 2 files, which column ??...












0















I have two files and I created two dataframes prod1 and prod2 out of it.I need to find the records with column names and values that are not matching in both the dfs.
id_sk is the primary key .all the cols are string datatype



dataframe 1 (prod1)



id_sk | uuid|name
1 |10 |a
2 |20 |b
3 |30 |c


dataframe 2 (prod2)



id_sk | uuid|name
2 |20 |b-upd
3 |30-up|c
4 |40 |d


so I need the result dataframe in the below format.



id|col_name|values
2 |name |b,b-upd
3 |uuid |30,30-up


I did a inner join and compared the unmatched records.



I am getting the result as follows :



id_sk | uuid_prod1|uid_prod2|name_prod1|name_prod2
2 |20 |20 |b |b-upd
3 |30 |30-up |c |c




val commmon_rec = prod1.join(prod2,prod1("id_sk")===prod2("id_sk"),"inner").select(prod1("id_sk").alias("id_sk_prod1"),prod1("uuid").alias("uuid_prod1"),prod1("name").alias("name_prod1"),prod1("name").alias("name_prod2")

val compare = spark.sql("select ...from common_rec where col_prod1<>col_prod2")









share|improve this question

























  • Possible duplicate of Compare two Spark dataframes

    – Andronicus
    Jan 20 at 7:18
















0















I have two files and I created two dataframes prod1 and prod2 out of it.I need to find the records with column names and values that are not matching in both the dfs.
id_sk is the primary key .all the cols are string datatype



dataframe 1 (prod1)



id_sk | uuid|name
1 |10 |a
2 |20 |b
3 |30 |c


dataframe 2 (prod2)



id_sk | uuid|name
2 |20 |b-upd
3 |30-up|c
4 |40 |d


so I need the result dataframe in the below format.



id|col_name|values
2 |name |b,b-upd
3 |uuid |30,30-up


I did a inner join and compared the unmatched records.



I am getting the result as follows :



id_sk | uuid_prod1|uid_prod2|name_prod1|name_prod2
2 |20 |20 |b |b-upd
3 |30 |30-up |c |c




val commmon_rec = prod1.join(prod2,prod1("id_sk")===prod2("id_sk"),"inner").select(prod1("id_sk").alias("id_sk_prod1"),prod1("uuid").alias("uuid_prod1"),prod1("name").alias("name_prod1"),prod1("name").alias("name_prod2")

val compare = spark.sql("select ...from common_rec where col_prod1<>col_prod2")









share|improve this question

























  • Possible duplicate of Compare two Spark dataframes

    – Andronicus
    Jan 20 at 7:18














0












0








0








I have two files and I created two dataframes prod1 and prod2 out of it.I need to find the records with column names and values that are not matching in both the dfs.
id_sk is the primary key .all the cols are string datatype



dataframe 1 (prod1)



id_sk | uuid|name
1 |10 |a
2 |20 |b
3 |30 |c


dataframe 2 (prod2)



id_sk | uuid|name
2 |20 |b-upd
3 |30-up|c
4 |40 |d


so I need the result dataframe in the below format.



id|col_name|values
2 |name |b,b-upd
3 |uuid |30,30-up


I did a inner join and compared the unmatched records.



I am getting the result as follows :



id_sk | uuid_prod1|uid_prod2|name_prod1|name_prod2
2 |20 |20 |b |b-upd
3 |30 |30-up |c |c




val commmon_rec = prod1.join(prod2,prod1("id_sk")===prod2("id_sk"),"inner").select(prod1("id_sk").alias("id_sk_prod1"),prod1("uuid").alias("uuid_prod1"),prod1("name").alias("name_prod1"),prod1("name").alias("name_prod2")

val compare = spark.sql("select ...from common_rec where col_prod1<>col_prod2")









share|improve this question
















I have two files and I created two dataframes prod1 and prod2 out of it.I need to find the records with column names and values that are not matching in both the dfs.
id_sk is the primary key .all the cols are string datatype



dataframe 1 (prod1)



id_sk | uuid|name
1 |10 |a
2 |20 |b
3 |30 |c


dataframe 2 (prod2)



id_sk | uuid|name
2 |20 |b-upd
3 |30-up|c
4 |40 |d


so I need the result dataframe in the below format.



id|col_name|values
2 |name |b,b-upd
3 |uuid |30,30-up


I did a inner join and compared the unmatched records.



I am getting the result as follows :



id_sk | uuid_prod1|uid_prod2|name_prod1|name_prod2
2 |20 |20 |b |b-upd
3 |30 |30-up |c |c




val commmon_rec = prod1.join(prod2,prod1("id_sk")===prod2("id_sk"),"inner").select(prod1("id_sk").alias("id_sk_prod1"),prod1("uuid").alias("uuid_prod1"),prod1("name").alias("name_prod1"),prod1("name").alias("name_prod2")

val compare = spark.sql("select ...from common_rec where col_prod1<>col_prod2")






scala apache-spark dataframe rdd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 12:09









Brian Tompsett - 汤莱恩

4,2231338101




4,2231338101










asked Jan 20 at 7:11









SHAMPA PRAMANIKSHAMPA PRAMANIK

1




1













  • Possible duplicate of Compare two Spark dataframes

    – Andronicus
    Jan 20 at 7:18



















  • Possible duplicate of Compare two Spark dataframes

    – Andronicus
    Jan 20 at 7:18

















Possible duplicate of Compare two Spark dataframes

– Andronicus
Jan 20 at 7:18





Possible duplicate of Compare two Spark dataframes

– Andronicus
Jan 20 at 7:18












1 Answer
1






active

oldest

votes


















0














This is a possible solution:



//to create a joined DF with column "col_name" 
//if columns "name" and "uuid" contains different values:
var output = df1.join(df2, df1.col("id_sk")===df2.col("id_sk"))
.where(df1.col("name")=!=df2.col("name") || df1.col("uuid")=!=df2.col("uuid"))
.withColumn("col_name", when(df1.col("name")=!=df2.col("name"), "name")
.otherwise(when(df1.col("uuid")=!=df2.col("uuid"), "uuid")))

//to create the new "col_values" column
//containing concatenated values:
output = output.withColumn("col_values", when(output.col("col_name")==="name", when(df1.col("name")=!=df2.col("name"), concat_ws(",", df1.col("name"), df2.col("name")) ))
.when(output.col("col_name")==="uuid", when(df1.col("uuid")=!=df2.col("uuid"), concat_ws(",", df1.col("uuid"), df2.col("uuid")) )))

output = output.select(df1.col("id_sk"), output.col("col_name"), output.col("col_values"))
+-----+--------+----------+
|id_sk|col_name|col_values|
+-----+--------+----------+
| 2| name| b,b-up|
| 3| uuid| 30,30-up|
+-----+--------+----------+


Note that I don't think this is the best possible solution, but just a starting point (for example what if one row have more than one different column values?).






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54274342%2fhow-can-we-compare-two-dataframes-in-spark-scala-to-find-difference-between-thes%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    This is a possible solution:



    //to create a joined DF with column "col_name" 
    //if columns "name" and "uuid" contains different values:
    var output = df1.join(df2, df1.col("id_sk")===df2.col("id_sk"))
    .where(df1.col("name")=!=df2.col("name") || df1.col("uuid")=!=df2.col("uuid"))
    .withColumn("col_name", when(df1.col("name")=!=df2.col("name"), "name")
    .otherwise(when(df1.col("uuid")=!=df2.col("uuid"), "uuid")))

    //to create the new "col_values" column
    //containing concatenated values:
    output = output.withColumn("col_values", when(output.col("col_name")==="name", when(df1.col("name")=!=df2.col("name"), concat_ws(",", df1.col("name"), df2.col("name")) ))
    .when(output.col("col_name")==="uuid", when(df1.col("uuid")=!=df2.col("uuid"), concat_ws(",", df1.col("uuid"), df2.col("uuid")) )))

    output = output.select(df1.col("id_sk"), output.col("col_name"), output.col("col_values"))
    +-----+--------+----------+
    |id_sk|col_name|col_values|
    +-----+--------+----------+
    | 2| name| b,b-up|
    | 3| uuid| 30,30-up|
    +-----+--------+----------+


    Note that I don't think this is the best possible solution, but just a starting point (for example what if one row have more than one different column values?).






    share|improve this answer




























      0














      This is a possible solution:



      //to create a joined DF with column "col_name" 
      //if columns "name" and "uuid" contains different values:
      var output = df1.join(df2, df1.col("id_sk")===df2.col("id_sk"))
      .where(df1.col("name")=!=df2.col("name") || df1.col("uuid")=!=df2.col("uuid"))
      .withColumn("col_name", when(df1.col("name")=!=df2.col("name"), "name")
      .otherwise(when(df1.col("uuid")=!=df2.col("uuid"), "uuid")))

      //to create the new "col_values" column
      //containing concatenated values:
      output = output.withColumn("col_values", when(output.col("col_name")==="name", when(df1.col("name")=!=df2.col("name"), concat_ws(",", df1.col("name"), df2.col("name")) ))
      .when(output.col("col_name")==="uuid", when(df1.col("uuid")=!=df2.col("uuid"), concat_ws(",", df1.col("uuid"), df2.col("uuid")) )))

      output = output.select(df1.col("id_sk"), output.col("col_name"), output.col("col_values"))
      +-----+--------+----------+
      |id_sk|col_name|col_values|
      +-----+--------+----------+
      | 2| name| b,b-up|
      | 3| uuid| 30,30-up|
      +-----+--------+----------+


      Note that I don't think this is the best possible solution, but just a starting point (for example what if one row have more than one different column values?).






      share|improve this answer


























        0












        0








        0







        This is a possible solution:



        //to create a joined DF with column "col_name" 
        //if columns "name" and "uuid" contains different values:
        var output = df1.join(df2, df1.col("id_sk")===df2.col("id_sk"))
        .where(df1.col("name")=!=df2.col("name") || df1.col("uuid")=!=df2.col("uuid"))
        .withColumn("col_name", when(df1.col("name")=!=df2.col("name"), "name")
        .otherwise(when(df1.col("uuid")=!=df2.col("uuid"), "uuid")))

        //to create the new "col_values" column
        //containing concatenated values:
        output = output.withColumn("col_values", when(output.col("col_name")==="name", when(df1.col("name")=!=df2.col("name"), concat_ws(",", df1.col("name"), df2.col("name")) ))
        .when(output.col("col_name")==="uuid", when(df1.col("uuid")=!=df2.col("uuid"), concat_ws(",", df1.col("uuid"), df2.col("uuid")) )))

        output = output.select(df1.col("id_sk"), output.col("col_name"), output.col("col_values"))
        +-----+--------+----------+
        |id_sk|col_name|col_values|
        +-----+--------+----------+
        | 2| name| b,b-up|
        | 3| uuid| 30,30-up|
        +-----+--------+----------+


        Note that I don't think this is the best possible solution, but just a starting point (for example what if one row have more than one different column values?).






        share|improve this answer













        This is a possible solution:



        //to create a joined DF with column "col_name" 
        //if columns "name" and "uuid" contains different values:
        var output = df1.join(df2, df1.col("id_sk")===df2.col("id_sk"))
        .where(df1.col("name")=!=df2.col("name") || df1.col("uuid")=!=df2.col("uuid"))
        .withColumn("col_name", when(df1.col("name")=!=df2.col("name"), "name")
        .otherwise(when(df1.col("uuid")=!=df2.col("uuid"), "uuid")))

        //to create the new "col_values" column
        //containing concatenated values:
        output = output.withColumn("col_values", when(output.col("col_name")==="name", when(df1.col("name")=!=df2.col("name"), concat_ws(",", df1.col("name"), df2.col("name")) ))
        .when(output.col("col_name")==="uuid", when(df1.col("uuid")=!=df2.col("uuid"), concat_ws(",", df1.col("uuid"), df2.col("uuid")) )))

        output = output.select(df1.col("id_sk"), output.col("col_name"), output.col("col_values"))
        +-----+--------+----------+
        |id_sk|col_name|col_values|
        +-----+--------+----------+
        | 2| name| b,b-up|
        | 3| uuid| 30,30-up|
        +-----+--------+----------+


        Note that I don't think this is the best possible solution, but just a starting point (for example what if one row have more than one different column values?).







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 21 at 11:52









        pheeleeppoopheeleeppoo

        85811420




        85811420
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54274342%2fhow-can-we-compare-two-dataframes-in-spark-scala-to-find-difference-between-thes%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Liquibase includeAll doesn't find base path

            How to use setInterval in EJS file?

            Petrus Granier-Deferre