如何将Map列添加到Spark数据集? [英] How to add a Map column to Spark dataset?

查看:51
本文介绍了如何将Map列添加到Spark数据集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java Map 变量,例如 Map< String,String>singleColMap .我想将此 Map 变量添加到数据集中,作为Spark 2.2(Java 1.8)中的新列值.

I have a Java Map variable, say Map<String, String> singleColMap. I want to add this Map variable to a dataset as a new column value in Spark 2.2 (Java 1.8).

我尝试了以下代码,但无法正常工作:

I tried the below code but it is not working:

ds.withColumn("cMap", lit(singleColMap).cast(MapType(StringType, StringType)))

有人可以帮忙吗?

推荐答案

您可以使用

You can use typedLit that was introducted in Spark 2.2.0, from the documentation:

此功能与点灯之间的区别在于此功能可以处理参数化的Scala类型,例如:List,Seq和Map.

The difference between this function and lit is that this function can handle parameterized scala types e.g.: List, Seq and Map.

因此,在这种情况下,以下内容就足够了

So in this case, the following should be enough

ds.withColumn("cMap", typedLit(singleColMap))

这篇关于如何将Map列添加到Spark数据集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆