Spark Equivalent of IF Then ELSE

后端 未结 4 702
梦如初夏
梦如初夏 2020-11-22 08:37

I have seen this question earlier here and I have took lessons from that. However I am not sure why I am getting an error when I feel it should work.

I want to crea

4条回答
  •  眼角桃花
    2020-11-22 09:35

    Correct structure is either:

    (when(col("iris_class") == 'Iris-setosa', 0)
    .when(col("iris_class") == 'Iris-versicolor', 1)
    .otherwise(2))
    

    which is equivalent to

    CASE 
        WHEN (iris_class = 'Iris-setosa') THEN 0
        WHEN (iris_class = 'Iris-versicolor') THEN 1 
        ELSE 2
    END
    

    or:

    (when(col("iris_class") == 'Iris-setosa', 0)
        .otherwise(when(col("iris_class") == 'Iris-versicolor', 1)
            .otherwise(2)))
    

    which is equivalent to:

    CASE WHEN (iris_class = 'Iris-setosa') THEN 0 
         ELSE CASE WHEN (iris_class = 'Iris-versicolor') THEN 1 
                   ELSE 2 
              END 
    END
    

    with general syntax:

    when(condition, value).when(...)
    

    or

    when(condition, value).otherwise(...)
    

    You probably mixed up things with Hive IF conditional:

    IF(condition, if-true, if-false)
    

    which can be used only in raw SQL with Hive support.

提交回复
热议问题