Scala - 转换后撤消平面图

时间:2016-03-22 22:00:11

标签: scala apache-spark spark-streaming scala-collections

如何将Seq的{​​{1}}合并到一个Maps,即

Map

例如:

Seq[Map[String, String]] => Map[String, String]

其中val someSeq = rdd.map(_._2).flatMap(...) //some transformation to produce the sequence of maps someSeqSeq(student1, student2)student1student2

Maps

我需要这样的结果:

var student1 = Map(a -> "1", b -> "1")
var student2 = Map(c -> "1", d -> "1")

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

与Spark无关,但一种方法是按顺序折叠序列:

val student1 = Map("a" -> "1", "b" -> "1")
val student2 = Map("c" -> "1", "d" -> "1")

val students = Seq(student1, student2)

students.foldLeft(Map[String, String]())(_ ++ _)

返回

Map(a -> 1, b -> 1, c -> 1, d -> 1)

关于"撤消"平面地图,我不相信这是真的可能。为了实现这一点,请考虑撤消" flatten"。

的概念

例如:

val x = Seq(1, 2)
val y = Seq(3, 4)
val combined = Seq(x, y)
val flattened = combined.flatten

val b = Seq(1, 2, 3)
val c = Seq(4)
val combined2 = Seq(b, c)
val flattened2 = combined2.flatten

flattened == flattened2

返回true。

所以基本上,在这个例子中,你可以从unflatte变为flattened,但反之亦然,因为反之亦然会产生多个答案。