3
votes

I have a below requirement to aggregate the data on Spark dataframe in scala. And, I have two datasets.

Dataset 1 contains values (val1, val2..) for each "t" types distributed on several different columns like (t1,t2...) .

val data1 = Seq(
    ("1","111",200,"221",100,"331",1000),
    ("2","112",400,"222",500,"332",1000),
    ("3","113",600,"223",1000,"333",1000)
).toDF("id1","t1","val1","t2","val2","t3","val3")

data1.show()

+---+---+----+---+----+---+----+
|id1| t1|val1| t2|val2| t3|val3|
+---+---+----+---+----+---+----+
|  1|111| 200|221| 100|331|1000|
|  2|112| 400|222| 500|332|1000|
|  3|113| 600|223|1000|333|1000|
+---+---+----+---+----+---+----+    

Dataset 2 represent the same thing by having a separate row for each "t" type.

val data2 = Seq(("1","111",200),("1","221",100),("1","331",1000),
  ("2","112",400),("2","222",500),("2","332",1000),
  ("3","113",600),("3","223",1000), ("3","333",1000)
).toDF("id*","t*","val*")

data2.show()    

+---+---+----+
|id*| t*|val*|
+---+---+----+
|  1|111| 200|
|  1|221| 100|
|  1|331|1000|
|  2|112| 400|
|  2|222| 500|
|  2|332|1000|
|  3|113| 600|
|  3|223|1000|
|  3|333|1000|
+---+---+----+      

Now,I need to groupBY(id,t,t*) fields and print the balances for sum(val) and sum(val*) as a separate record. And both balances should be equal.

My output should look like below:
+---+---+--------+---+---------+
|id1| t |sum(val)| t*|sum(val*)|
+---+---+--------+---+---------+
|  1|111|     200|111|      200|
|  1|221|     100|221|      100|
|  1|331|    1000|331|     1000|
|  2|112|     400|112|      400|
|  2|222|     500|222|      500|
|  2|332|    1000|332|     1000|
|  3|113|     600|113|      600|
|  3|223|    1000|223|     1000|
|  3|333|    1000|333|     1000|
+---+---+--------+---+---------+

I'm thinking of exploding the dataset1 into mupliple records for each "t" type and then join with dataset2. But could you please suggest me a better approach which wouldn't affect the performance if datasets become bigger?

1

1 Answers

1
votes

The simplest solution is to do sub-selects and then union datasets:

val ts = Seq(1, 2, 3)
val dfs = ts.map (t => data1.select("t" + t as "t", "v" + t as "v"))
val unioned = dfs.drop(1).foldLeft(dfs(0))((l, r) => l.union(r))

val ds = unioned.join(df2, 't === col("t*")
here aggregation

You can also try array with explode:

val df1 = data1.withColumn("colList", array('t1, 't2, 't3))
               .withColumn("t", explode(colList))
               .select('t, 'id1 as "id")

val ds = df2.withColumn("val", 
          when('t === 't1, 'val1)
          .when('t === 't2, 'val2)
          .when('t === 't3, 'val3)
          .otherwise(0))

The last step is to join this Dataset with data2:

ds.join(data2, 't === col("t*"))
  .groupBy("t", "t*")
  .agg(first("id1") as "id1", sum(val), sum("val*"))