How to get the size of result generated using concat_ws?

Viewed 1588

I am performing groupBy on COL1 and getting the concatenated list of COL2 using concat_ws. How can I get the count of values in that list? Here's my code:

Dataset<Row> ds = df.groupBy("COL1").agg(org.apache.spark.sql.functions
    .concat_ws(",",org.apache.spark.sql.functions.collect_list("COL2")).as("sample"));
1 Answers

Use size function.

size(e: Column): Column Returns length of array or map.


The following example is in Scala and am leaving it to you to convert it to Java, but the general idea is exactly the same regardless of the programming language.

val input = spark.range(4)
  .withColumn("COL1", $"id" % 2)
  .select($"COL1", $"id" as "COL2")
scala> input.show
+----+----+
|COL1|COL2|
+----+----+
|   0|   0|
|   1|   1|
|   0|   2|
|   1|   3|
+----+----+

val s = input
  .groupBy("COL1")
  .agg(
    concat_ws(",", collect_list("COL2")) as "concat",
    size(collect_list("COL2")) as "size") // <-- size
scala> s.show
+----+------+----+
|COL1|concat|size|
+----+------+----+
|   0|   0,2|   2|
|   1|   1,3|   2|
+----+------+----+

In Java that'd be as follows. Thanks Krishna Prasad for sharing the code with the SO/Spark community!

Dataset<Row> ds = df.groupBy("COL1").agg(
  org.apache.spark.sql.functions.concat_ws(",",org.apache.spark.sql.functions.collect_list("‌​COL2")).as("sample")‌​, 
  org.apache.spark.sql.functions.size(org.apache.spark.sql.functions.collect_list("COL2‌​")).as("size"));
Related