【问题标题】:otherwise-clause not working as expect , whats wrong here?否则 - 子句没有按预期工作,这里有什么问题?
【发布时间】:2020-08-03 14:53:43
【问题描述】:

我正在使用 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|
+---+-----+------+----------+------+------+

为什么“否则”条件没有按预期工作。知道这里有什么问题吗??

【问题讨论】:

    标签: apache-spark apache-spark-sql


    【解决方案1】:

    列应该包含相同的数据类型。

    注意 - 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
                          )
    

    【讨论】:

    【解决方案2】:

    我建议修改为-

    df
                     .withColumn("value1",
                            when(col("code") === lit("rate") , functions.callUDF("udfFunc",col("value1")))
                             .otherwise(col("value1").cast(StringType))
                          )
    

    检查一次

    【讨论】:

    • 抱歉没有运气.. 除了 "rate" 之外,value1 列仍为空。
    • 正如 Srinivas 解释的,这是因为强制转换,将其更改为 StringType
    • 任何建议如何在 spark 中处理这种情况? stackoverflow.com/questions/63668096/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 2018-07-20
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 2022-08-02
    相关资源
    最近更新 更多