apache-spark - 否则 - 子句没有按预期工作,这里有什么问题?
问题描述
我正在使用 spark-sql-2.4.1v 如何根据列的值进行各种连接,我需要为给定的值列获取 map_val 列的多个查找值,如下所示。
样本数据:
val data = List(
("20", "score", "school", "2018-03-31", 14 , 12),
("21", "score", "school", "2018-03-31", 13 , 13),
("22", "rate", "school", "2018-03-31", 11 , 14),
("21", "rate", "school", "2018-03-31", 13 , 12)
)
val df = data.toDF("id", "code", "entity", "date", "value1", "value2")
df.show
+---+-----+------+----------+------+------+
| id| code|entity| date|value1|value2|
+---+-----+------+----------+------+------+
| 20|score|school|2018-03-31| 14| 12|
| 21|score|school|2018-03-31| 13| 13|
| 22| rate|school|2018-03-31| 11| 14|
| 21| rate|school|2018-03-31| 13| 12|
+---+-----+------+----------+------+------+
val resultDs = df
.withColumn("value1",
when(col("code").isin("rate") , functions.callUDF("udfFunc",col("value1")))
.otherwise(col("value1").cast(DoubleType))
)
udfFunc 映射如下
11->a
12->b
13->c
14->d
预期产出
+---+-----+------+----------+------+------+
| id| code|entity| date|value1|value2|
+---+-----+------+----------+------+------+
| 20|score|school|2018-03-31| 14| 12|
| 21|score|school|2018-03-31| 13| 13|
| 22| rate|school|2018-03-31| a | 14|
| 21| rate|school|2018-03-31| c | 12|
+---+-----+------+----------+------+------+
但它给出的输出为
+---+-----+------+----------+------+------+
| id| code|entity| date|value1|value2|
+---+-----+------+----------+------+------+
| 20|score|school|2018-03-31| null| 12|
| 21|score|school|2018-03-31| null| 13|
| 22| rate|school|2018-03-31| a | 14|
| 21| rate|school|2018-03-31| c | 12|
+---+-----+------+----------+------+------+
为什么“否则”条件没有按预期工作。知道这里有什么问题吗?
解决方案
列应包含相同的数据类型。
注意-DoubleType
无法存储StringTyp
数据,因此您需要转换DoubleType
为StringType
.
val resultDs = df
.withColumn("value1",
when(col("code") === lit("rate") ,functions.callUDF("udfFunc",col("value1")))
.otherwise(col("value1").cast(StringType)) // Should be StringType
)
或者
val resultDs = df
.withColumn("value1",
when(col("code").isin("rate") , functions.callUDF("udfFunc",col("value1")))
.otherwise(col("value1").cast(StringType)) // Modified to StringType
)
推荐阅读
- php - 如何使用 generate_rewrite_rules 在我的 WordPress 插件中添加到索引的路由?
- python - 我收到一条错误消息,显示列表索引超出范围
- javascript - 在.on 内调用警报时不会调用弹出窗口?
- r - ggplot2:如何更改条形/彩色几何图形中颜色美学的宽度?
- javascript - 发布的问题解决方案似乎包含不必要的代码
- linux - 将行转换为列的 Linux 脚本
- javascript - Facebook AR - Javascript
- javascript - 从 API 调用解析 JSON 后,从意图参数中提取的参数未分配给变量
- c# - 如何使用 dotnet core 和 Polly 添加动态重试策略
- javascript - 如何使物体继续在一个轴上移动,尽管它的速度在另一个轴上被阻止