Spark:使用地图中的键选择 [英] Spark: select with key in map

查看:32
本文介绍了Spark:使用地图中的键选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 DataFramemap 中使用键 geo.cc 进行选择:

I need to select with the key geo.cc in a map in a DataFrame:

 |-- params: map (nullable = true)
 |    |-- key: string
 |    |-- value: string (valueContainsNull = true)

但是键的值,中间有一个点,似乎混淆了 Spark.如果我写:

but the value of the key, with its dot in the middle, seems to confuse Spark. If I write:

X.filter(X("params.geo.cc") === "us")

我收到错误:

org.apache.spark.sql.AnalysisException: Can't extract value from params#3[geo];

我能做什么?(不用说,我不控制密钥,即我不能将 geo.cc 字符串更改为例如 geo_cc.

What can I do? (needless to say, I do not control the key, i.e. I cannot change that geo.cc string to e.g. geo_cc.

推荐答案

你应该使用apply:

val df = Seq((1L, Map("geo.cc" -> "US"))).toDF("id", "params")

df.select($"params"("geo.cc") === "US").show
// +-----------------------+
// |(params['geo.cc'] = US)|
// +-----------------------+
// |                   true|
// +-----------------------+

getItem

df.select($"params".getItem("geo.cc") === "US").show
// +-----------------------+
// |(params['geo.cc'] = US)|
// +-----------------------+
// |                   true|
// +-----------------------+

在特定列上,而不是 DataFrame.

on a specific column, not DataFrame.

这篇关于Spark:使用地图中的键选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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