我正在使用pyspark和hivecontext.sql我想从我的数据中过滤掉所有空值和空值。
所以我使用简单的sql命令首先过滤掉空值,但它不起作用。
我的代码:
hiveContext.sql("select column1 from table where column2 is not null")
但它在没有表达式"其中列 2 不为空"的情况下工作
错误:
Py4JavaError: An error occurred while calling o577.showString
我认为这是由于我的选择错误。
数据示例:
column 1 | column 2
null | 1
null | 2
1 | 3
2 | 4
null | 2
3 | 8
目的:
column 1 | column 2
1 | 3
2 | 4
3 | 8
啧
我们不能将 Hive 表名称直接传递给 Hive 上下文 sql 方法,因为它不理解 Hive 表名称。读取Hive表的方法之一是使用pysaprk shell。
我们需要注册从读取 Hive 表获得的数据框。然后我们可以运行 SQL 查询。
您必须提供 database_name.table 并运行相同的查询才能工作。如果有帮助,请告诉我
它对我有用:
df.na.drop(subset=["column1"])
Have you entered null values manually?
If yes then it will treat those as normal strings,
I tried following two use cases
dbname.person table in hive
name age
aaa null // this null is entered manually -case 1
Andy 30
Justin 19
okay NULL // this null came as this field was left blank. case 2
---------------------------------
hiveContext.sql("select * from dbname.person").show();
+------+----+
| name| age|
+------+----+
| aaa |null|
| Andy| 30|
|Justin| 19|
| okay|null|
+------+----+
-----------------------------
case 2
hiveContext.sql("select * from dbname.person where age is not null").show();
+------+----+
| name|age |
+------+----+
| aaa |null|
| Andy| 30 |
|Justin| 19 |
+------+----+
------------------------------------
case 1
hiveContext.sql("select * from dbname.person where age!= 'null'").show();
+------+----+
| name| age|
+------+----+
| Andy| 30|
|Justin| 19|
| okay|null|
+------+----+
------------------------------------
我希望上述用例可以消除您对过滤空值的疑虑 外。 如果您正在查询在 Spark 中注册的表,请使用 sqlContext。