【发布时间】:2020-08-31 09:20:22
【问题描述】:
我在 Java 8 中使用 spark-sql-2.4.1v。我有一个场景,我需要从查找表中动态添加一列。
我有带有列的数据框 A, B, C, ..., X,Y, Z
当少数(原始)列(例如:A、B、C)值为 null 时,我需要取/替换列(例如:X、Y、Z)值,否则取原始列值。 我将获取此映射信息作为业务逻辑的一部分。 如果是这种情况,我将遵循下面的硬编码代码
Dataset<Row> substitutedDs = ds
.withColumn("A",
when(col("A").isNull() , col("X").cast(DataTypes.StringType))
.otherwise(col("A").cast(DataTypes.StringType))
)
.withColumn("C",
when(col("C").isNull() , col("Z").cast(DataTypes.StringType))
.otherwise(col("C").cast(DataTypes.StringType))
哪个工作正常。但我需要动态/可配置地执行此操作以避免硬编码。
我将得到包含“code”和“code_substitutes”列信息的查找表,如下所示
-------------------------
| Code | Code_Substitute |
-------------------------
A X
B Y
C Z
-------------------------
我需要在上面动态构造“submittedDs”,怎么做?
【问题讨论】:
标签: apache-spark apache-spark-sql