scala - 使用所需的键和值更新 Map 类型的 Spark 数据框的列
问题描述
我有一个以下 spark 数据框,其中所有列(主键列 emp_id 除外)都由一个映射组成(键 'from' 和 'to' 可以有空值)。我想评估每列的'from'和'to'(emp_id除外)并向地图添加一个新键(名为'change'),如果'from'值为null,则值为a)'insert'并且'to' 不为 null b) 如果 'to' 值为 null 且 'from' 不为 null b) 'update' 如果 'from' 和 'to' 不为 null & 'from' 值不同于 '到'值
注意:具有空值的列将保持不变。
我们如何在 Scala 中实现这一点。
|emp_id|emp_city |emp_name |emp_phone |emp_sal |emp_site |
|1 |null |[from -> Will, to -> Watson]|null |[from -> 1000, to -> 8000]|[from ->, to -> Seattle] |
|3 |null |[from -> Norman, to -> Nate]|null |[from -> 1000, to -> 8000]|[from -> CherryHill, to -> Newark]|
|4 |[from ->, to -> Iowa]|[from ->, to -> Ian] |[from ->, to -> 1004]|[from ->, to -> 8000] |[from ->, to -> Des Moines] |
预期的:
|emp_id|emp_city |emp_name |emp_phone |emp_sal |emp_site |
|1 |null |[from -> Will, to -> Watson, change -> update]|null |[from -> 1000, to -> 8000, change -> update]|[from ->, to -> Seattle, change -> insert] |
|3 |null |[from -> Norman, to -> Nate, change -> update]|null |[from -> 1000, to -> 8000, change -> update]|[from -> CherryHill, to -> Newark, change -> update]|
|4 |[from ->, to -> Iowa, change -> insert]|[from ->, to -> Ian, change -> insert] |[from ->, to -> 1004, change -> insert]|[from ->, to -> 8000, change -> insert] |[from ->, to -> Des Moines, change -> insert] |
解决方案
实现此目的的一种方法是使用UDF
,这不是一个很好的解决方案,但我想不出其他解决方案。
尽量不要UDF
使用
val updateMap = udf((input: Map[String, String]) => {
if (input == null || input.isEmpty)
Map.empty[String, String]
else if (input("from") == null && input("to") != null)
input + ("change" -> "insert")
else if (input("from") != null && input("to") == null)
input + ("change" -> "delete")
else if (!(input("from").equals(input("to"))))
input + ("change" -> "update")
else
Map.empty[String, String]
})
val result = df.columns.tail.foldLeft(df) { (acc, name) =>
acc.withColumn(name, updateMap(col(name)))
}
确保您的列是Map[String, String]
希望这可以帮助!
推荐阅读
- angularjs - Angularjs 控制器已定义但从未使用过
- c++ - 是否有任何 2 补码平台/编译器,其中带符号的移位不做通常的事情?
- python - Selenium 被检测到,并且在第一次打开Bloomberg URL 后立即弹出验证码
- java - 项目中所需库 tomcat-embed-websocket 的存档无法读取或不是有效的 ZIP 文件
- javascript - 反应动态表创建
- javascript - 未捕获的 ReferenceError:未定义数据点。从 api 调用 json 数据时
- android - Android响应汽车中的蓝牙音频按钮 - ExoPlayer
- selenium - Xpath con-cat 可以参考 List
硒中的物体? - javascript - 如何访问 edgeHTML 17 中发布的精确触摸板指针事件
- javascript - 我的第一个 ExtJs 6 应用程序出错。Ext.app 未定义