pyspark - spark.sql() 中不等于什么
问题描述
我正在练习使用spark.sql()
pyspark 的函数。当我在 spark 中使用 not equal 函数时,我似乎无法使用它<> != Not
来执行复杂的查询。
示例查询:
+--------------------+-------------+--------------------+
| Party| Handle| Tweet|
+--------------------+-------------+--------------------+
| Democrat|RepDarrenSoto|Today, Senate Dem...|
| Democrat|RepDarrenSoto|RT @WinterHavenSu...|
| Democrat|RepDarrenSoto|RT @NBCLatino: .@...|
|Congress has allo...| null| null|
| Democrat|RepDarrenSoto|RT @NALCABPolicy:...|
| Democrat|RepDarrenSoto|RT @Vegalteno: Hu...|
| Democrat|RepDarrenSoto|RT @EmgageActionF...|
| Democrat|RepDarrenSoto|Hurricane Maria l...|
| Democrat|RepDarrenSoto|RT @Tharryry: I a...|
| Democrat|RepDarrenSoto|RT @HispanicCaucu...|
| Democrat|RepDarrenSoto|RT @RepStephMurph...|
| Democrat|RepDarrenSoto|RT @AllSaints_FL:...|
| Democrat|RepDarrenSoto|.@realDonaldTrump...|
| Democrat|RepDarrenSoto|Thank you to my m...|
| Democrat|RepDarrenSoto|We paid our respe...|
|Sgt Sam Howard - ...| null| null|
| Democrat|RepDarrenSoto|RT @WinterHavenSu...|
| Democrat|RepDarrenSoto|Meet 12 incredibl...|
| Democrat|RepDarrenSoto|RT @wildlifeactio...|
| Democrat|RepDarrenSoto|RT @CHeathWFTV: K...|
+--------------------+-------------+--------------------+
spark.sql("""select Party from tweets_tempview where Party <>'Democrat' or 'Republican' """).show(20,False)
错误信息:
"cannot resolve '((NOT (tweets_tempview.`Party` = 'Democrat')) OR 'Republican')' due to data type mismatch: differing types in '((NOT (tweets_tempview.`Party` = 'Democrat')) OR 'Republican')' (boolean and string).; line 1 pos 40;\n'Project ['Party]\n+- 'Filter (NOT (Party#98 = Democrat) || Republican)\n +- SubqueryAlias `tweets_tempview`\n +- Relation[Party#98,Handle#99,Tweet#100] csv\n"
什么是让 where 子句值都起作用的 spark sql 函数?
解决方案
您无法使用单个<>
操作比较两个字符串。要么使用:
where Party <> 'Democrat' and Party <> 'Republican'
或者按照评论中的建议使用它
where Party not in ('Democrat', 'Republican')
推荐阅读
- android - 无法识别的 VM 选项“MaxPermSize=2048m”
- c++ - 编码初学者,卡在识别 C++ 中的质数的代码中......有人可以纠正这段代码中的错误吗?
- python - 将具有依赖关系的 .py 文件转换为 .exe
- c# - 在 SendGrid 中添加联系人并获取联系人 ID
- c# - 有条件地提供 ASP.NET Identity OAuth 令牌
- powerbi - 错误消息:R 脚本错误。错误:出现意外符号:“# Epilog - Auto Generated # setwd” 执行停止
- java - Hibernate 不会在 MySql 中自动创建数据库中不存在的表,但我没有收到任何错误
- correlation - 与数千个变量的相关性 - FDR
- javascript - 仅当从另一个选择元素列表中选择特定元素时才显示选择字段
- php - Codeigniter mysqli 问题