我有一个Java Map
变量,例如 Map<String, String> singleColMap
。我想添加这个Map
Spark 2.2 (Java 1.8) 中将变量作为新列值添加到数据集。
我尝试了以下代码,但它不起作用:
ds.withColumn("cMap", lit(singleColMap).cast(MapType(StringType, StringType)))
有人可以帮忙解决这个问题吗?
最佳答案
您可以使用typedLit
这是在 Spark 2.2.0 中引入的,来自文档:
The difference between this function and lit is that this function can handle parameterized scala types e.g.: List, Seq and Map.
所以在这种情况下,以下内容就足够了
ds.withColumn("cMap", typedLit(singleColMap))
关于java - 如何向 Spark 数据集添加 Map 列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52417532/