猿问

spark UDF 结果可以“显示”,但不能“过滤”

火花UDF的作品时,我做的show(),但它给了我的错误,当我做 filter的UDF结果。


udf函数


def chkInterPunctuation(sent) :

    for char in sent[1:-2] : 

        if char in ["\"", "'", ".", "!", "?"] :

            return True

    return False


cip = udf(chkInterPunctuation, BooleanType())

show() 作品


df_punct = dfs.withColumn("in_length", length("input")).\

withColumn("out_length", length("output")).withColumn("cip", cip(col("input")))

df_punct.show()

但是当我这样做时它给了我错误 filter

df_punct.where(col("cip") == True).show()

我的谷歌搜索表明,py4jUDF函数没有返回正确的值或有错误时,通常会发生错误。但我UDF function总是返回 true 或 false。此外,当我显示时,spark 查询会返回正确的值。这对我来说没有意义。可能的原因是什么?



翻阅古今
浏览 162回答 3
3回答

慕工程0101907

发生这种情况是因为您没有纠正NULL存在。尝试:def chkInterPunctuation(sent) :    if not sent: return   # In None return    for char in sent[1:-2] :         if char in ["\"", "'", ".", "!", "?"] :            return True    return False

婷婷同学_

通过更新到 spark2.4.0 解决了问题。(我用的是 2.0.0)
随时随地看视频慕课网APP

相关分类

Python
我要回答