pyspark-在将WHEN与LIKE操作或CONTAINS一起使用时获取空值

问题描述 投票:0回答:1

我正在使用胶水etl(pyspark)在“ status_purpose”列中写入条件以验证值“ ITEM”。如果找到该值,则将“ check_status_ind”列设置为“ Y”,否则设置为“ N”。

我尝试使用以下代码,并且所有记录都显示为“ N”值。我希望有10条以上的记录显示为“ Y”。任何建议,将不胜感激。

applymapping_DyF = applymapping_DyF.withColumn('check_status_ind', when(((col('status_purpose').contains("ITEM"))), "Y").otherwise("N"))

applymapping_DyF = applymapping_DyF.withColumn('check_status_ind', 
        F.when(((col('status_purpose').like('%ITEM%'))), "Y").otherwise("N")
    )
python-3.x pyspark pyspark-sql aws-glue
1个回答
0
投票

您正在使用varchar类型。这是Hivestringtype,不应与spark一起使用。根据documentation:

'这些数据类型仅应用于解析,而NOT应该在其他任何地方使用。在分析之前,应将这些数据类型的任何实例替换为StringType。'

因此,在包含之前,将列投射到字符串如下所示,它应该可以工作:

applymapping_DyF = applymapping_DyF\
.withColumn('check_status_ind', when(((col('status_purpose').cast("string").contains("ITEM"))), "Y").otherwise("N"))
© www.soinside.com 2019 - 2024. All rights reserved.