简体   繁体   English

Pyspark:不匹配的输入......期待EOF

[英]Pyspark: mismatched input ... expecting EOF

I want to add a column to a data frame and depending on whether a certain value appears in the source json, the value of the column should be the value from the source or null.我想向数据框中添加一列,根据源 json 中是否出现某个值,该列的值应该是来自源的值或 null。 My code looks like this:我的代码如下所示:

withColumn("STATUS_BIT", expr("case when 'statusBit:' in jsonDF.schema.simpleString() then statusBit else None end"))

When I run this, I am getting "mismatched input ''statusBit:'' expecting {< EOF >, '-'} . Am I doing something wrong with the quotation marks? When I try当我运行这个时,我得到“不匹配的输入 ''statusBit:'' 期待 {< EOF >, '-'} 。我用引号做错了吗?当我尝试

withColumn("STATUS_BIT", expr("case when \'statusBit:\' in jsonDF.schema.simpleString() then statusBit else None end"))

I get the exact same error.我得到完全相同的错误。 Trying the whole thing without expr but as a simple when, triggers the error "condition should be a Column".在没有 expr 的情况下尝试整个事情,但作为一个简单的时间,会触发错误“条件应该是一个列”。 Running 'statusBit:' in jsonDF.schema.simpleString() by itself returns True with the testdata I am using, but somehow I cant integrate it into the dataframe transformation.Thanks a lot for your help in advance.在 jsonDF.schema.simpleString() 中运行 'statusBit:' 本身会使用我正在使用的测试数据返回 True,但不知何故我无法将它集成到数据帧转换中。非常感谢您提前提供帮助。

jsonDF.schema.simpleString() is Python variable, you can use it in Python way jsonDF.schema.simpleString()是 Python 变量,你可以用 Python 的方式使用它

from pyspark.sql import functions as F

df.withColumn("STATUS_BIT", F.lit(df.schema.simpleString()).contains('statusBit:'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM