Apache spark 使用;或;火花
使用“或”筛选数据帧时出错。 代码如下:Apache spark 使用;或;火花,apache-spark,apache-spark-sql,Apache Spark,Apache Spark Sql,使用“或”筛选数据帧时出错。 代码如下: df.select("InvoiceNo","Description").where((col("InvoiceNo") !== 536365) || (col("UnitPrice") > 600)) 我也尝试过使用“或”,但得到了相同的错误 df.select("InvoiceNo","Description").where((col("InvoiceNo") !== 536365).or(col("UnitPrice") > 600)
df.select("InvoiceNo","Description").where((col("InvoiceNo") !== 536365) || (col("UnitPrice") > 600))
我也尝试过使用“或”,但得到了相同的错误
df.select("InvoiceNo","Description").where((col("InvoiceNo") !== 536365).or(col("UnitPrice") > 600))
错误:
org.apache.spark.sql.AnalysisException: cannot resolve 'UnitPrice' given input columns: [InvoiceNo, Description]
我哪里会出错?
请提供帮助。您只选择了两列
InvoiceNo,Description
,代码将尝试根据您选择的不存在UnitPrice
的列筛选
您可以尝试以下操作:
df.select("InvoiceNo","Description","UnitPrice").where((col("InvoiceNo") !== 536365).or(col("UnitPrice") > 600))
val df2 = df
.where((col("InvoiceNo") !== 536365).or(col("UnitPrice") > 600)) // projection (π)
.select("InvoiceNo","Description") // selection (σ)
如果需要选择特定列,请在where
之后使用选择
df.where((col("InvoiceNo") !== 536365).or(col("UnitPrice") > 600)).select("InvoiceNo","Description","UnitPrice")
在方面,当您使用Spark SQL执行选择(select
)时,这会缩小所选列的范围
因此,您将无法调用尚未选择的对象来执行投影(where
,filter
)
该逻辑与常规SQL逻辑略有不同,因此在您的情况下,主要需要执行以下操作:
df.select("InvoiceNo","Description","UnitPrice").where((col("InvoiceNo") !== 536365).or(col("UnitPrice") > 600))
val df2 = df
.where((col("InvoiceNo") !== 536365).or(col("UnitPrice") > 600)) // projection (π)
.select("InvoiceNo","Description") // selection (σ)
这是可行的,但我的要求没有得到满足。在where之后应该遵循select方法。是的。。如果您只需要选择两列,那么您的选择应该在where后面。