什么时候使用子句有什么问题 [英] what is wrong here using when-clause

查看:66
本文介绍了什么时候使用子句有什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有如下情况:

 val df = // dataset have columns like "col", "col_a",  "col_b".
  val  type = "" //dynamicLLT passed by user 
 //based on this, need to add one more colum "value" based "col" value.


 val valueDs =  df
        .withColumn("type", lit(type).cast(StringType))
         .withColumn("value", 
                 when(col("cal").equalTo(lit("A_B")),concat_ws("_",col("col_a"), col("col_b"))).
                 when(col("cal").equalTo(lit("A")),concat(col("col_a")))
                );

需要根据类型"选择其他列,并适当地填充值"列.

Need to select other columns based on "type" and populate "value" column appropriately.

但是当我运行""col(" cal)时==(" A)""如果由于字段"col_b"不可用而失败.

But when I run """col("cal"). == ("A") """ if fails due to field "col_b" not available .

那么这里出什么问题了?为什么要查找不存在的"col_b". 子句如何解决.

So what is wrong here ? Why it looking for "col_b" which is not there. How to fix this when-clause.

推荐答案

您可以尝试以下吗.

val valueDs =  df
        .withColumn("type", lit(type).cast(StringType))
         .withColumn("value", 
                 when((col("cal") === "A_B"),concat_ws("_",col("col_a"), col("col_b"))).
                 when((col("cal") === "A"),concat(col("col_a")))
                .otherwise("null")
         );

这篇关于什么时候使用子句有什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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