0
votes

I have a spark dataframe like

+-----+---+---+---+------+
|group|  a|  b|  c|config|
+-----+---+---+---+------+
|    a|  1|  2|  3|   [a]|
|    b|  2|  3|  4|[a, b]|
+-----+---+---+---+------+
val df = Seq(("a", 1, 2, 3, Seq("a")),("b", 2, 3,4, Seq("a", "b"))).toDF("group", "a", "b","c", "config")

How can I add an additional column i.e.

df.withColumn("select_by_config", <<>>).show

as a struct or JSON which combines a number of columns (specified by config) in something similar to a hive named struct / spark struct / json column? Note, this struct is specific per group and not constant for the whole dataframe; it is specified in config column.

I can imagine that a df.map could do the trick, but the serialization overhead does not seem to be efficient. How can this be achieved via SQL only expressions? Maybe as a Map-type column?

edit

a possible but really clumsy solution for 2.2 is:

val df = Seq((1,"a", 1, 2, 3, Seq("a")),(2, "b", 2, 3,4, Seq("a", "b"))).toDF("id", "group", "a", "b","c", "config")
  df.show
  import spark.implicits._
  final case class Foo(id:Int, c1:Int, specific:Map[String, Int])
  df.map(r => {
    val config = r.getAs[Seq[String]]("config")
    print(config)
    val others = config.map(elem => (elem, r.getAs[Int](elem))).toMap
    Foo(r.getAs[Int]("id"), r.getAs[Int]("c"), others)
  }).show

are there any better ways to solve the problem for 2.2?

1

1 Answers

1
votes

If you use a recent build (Spark 2.4.0 RC 1 or later) a combination of higher order functions should do the trick. Create a map of columns:

import org.apache.spark.sql.functions.{
  array, col, expr, lit, map_from_arrays, map_from_entries
}

val cols = Seq("a", "b", "c")

val dfm = df.withColumn(
  "cmap", 
  map_from_arrays(array(cols map lit: _*), array(cols map col: _*))
)

and transform the config:

dfm.withColumn(
  "config_mapped",
   map_from_entries(expr("transform(config, k -> struct(k, cmap[k]))"))
).show

// +-----+---+---+---+------+--------------------+----------------+
// |group|  a|  b|  c|config|                cmap|   config_mapped|
// +-----+---+---+---+------+--------------------+----------------+
// |    a|  1|  2|  3|   [a]|[a -> 1, b -> 2, ...|        [a -> 1]|
// |    b|  2|  3|  4|[a, b]|[a -> 2, b -> 3, ...|[a -> 2, b -> 3]|
// +-----+---+---+---+------+--------------------+----------------+