2

I have a sequence of a case class, which has a String followed by a sequence of Strings. How do I flatmap the sequence of Strings (second column) without losing the first column?

I tried this:

flatmap(_.second)

But in this case I lose the first column.

Here is my code:

case class A(
            first:String,
            second:Seq[String]
            )

val ds = Seq(
  A("1", Seq("A","B")),
  A("2",  Seq("C"))
) toDS
Tzach Zohar
  • 37,442
  • 3
  • 79
  • 85
Ulrich Zendler
  • 141
  • 1
  • 10

1 Answers1

3

A way of exploding your second col and keep the first col is effectively flatMap :

ds.flatMap{ case A(a,b) => b.map((a,_)) }

should output :

+---+---+
| _1| _2|
+---+---+
|  1|  A|
|  1|  B|
|  2|  C|
+---+---+
Etienne Herlaut
  • 526
  • 4
  • 12