7

I am new to Spark-SQL. I have information in Spark Dataframe like this

Company Type Status
A       X    done
A       Y    done
A       Z    done
C       X    done
C       Y    done
B       Y    done

I am want to be displayed like the following

Company X-type Y-type Z-type
A       done    done    done
B       pending done    pending
C       done    done    pending

I am not able to acheive this is Spark-SQL

Please Help

zero323
  • 322,348
  • 103
  • 959
  • 935
Vikrant Sonawane
  • 207
  • 1
  • 5
  • 15

1 Answers1

12

You can groupby Company and then use pivot function on column Type

Here is the simple example

import org.apache.spark.sql.functions._

val df = spark.sparkContext.parallelize(Seq(
        ("A", "X", "done"),
        ("A", "Y", "done"),
        ("A", "Z", "done"),
        ("C", "X", "done"),
        ("C", "Y", "done"),
        ("B", "Y", "done")
      )).toDF("Company", "Type", "Status")

val result = df.groupBy("Company")
    .pivot("Type")
    .agg(expr("coalesce(first(Status), \"pending\")"))

result.show()

Output:

+-------+-------+----+-------+
|Company|      X|   Y|      Z|
+-------+-------+----+-------+
|      B|pending|done|pending|
|      C|   done|done|pending|
|      A|   done|done|   done|
+-------+-------+----+-------+

You can rename the column later.

Hope this helps!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
koiralo
  • 22,594
  • 6
  • 51
  • 72