我正在嘗試執行 Learning Spark 一書中的示例。
在where運算式中有這樣一種使用列的形式:
val fewFireDF = fireDF
.select("IncidentNumber", "AvailableDtTm", "CallType")
.where($"CallType" =!= "Medical Incident")
但是 IntelliJ Idea 不理解$"CallType". 它看起來像一個字串。
這些變體效果很好:
.where(col("CallType") =!= "Medical Incident")
.where("CallType != 'Medical Incident'")
更新 似乎我沒有清楚地解釋我的問題。
這是我的代碼:
package org.example.chapter3
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.catalyst.dsl.expressions.{DslExpression, StringToAttributeConversionHelper}
import org.apache.spark.sql.types.{BooleanType, FloatType, IntegerType, StringType, StructField, StructType}
import org.apache.spark.sql.functions._
object DepartmentCalls extends App {
val spark = SparkSession
.builder
.appName("DepartmentCalls")
.getOrCreate()
if (args.length < 1) {
println("usage DepartmentCalls <file path to fire_incidents.csv")
System.exit(1)
}
val schema = StructType(
Array(
StructField("CallNumber", IntegerType),
StructField("UnitID", StringType),
StructField("IncidentNumber", IntegerType),
StructField("CallType", StringType),
StructField("CallDate", StringType),
StructField("WatchDate", StringType),
StructField("CallFinalDisposition", StringType),
StructField("AvailableDtTm", StringType),
StructField("Address", StringType),
StructField("City", StringType),
StructField("Zipcode", IntegerType),
StructField("Battalion", StringType),
StructField("StationArea", StringType),
StructField("Box", StringType),
StructField("OriginalPriority", StringType),
StructField("Priority", StringType),
StructField("FinalPriority", IntegerType),
StructField("ALSUnit", BooleanType),
StructField("CallTypeGroup", StringType),
StructField("NumAlarms", IntegerType),
StructField("UnitType", StringType),
StructField("UnitSequenceInCallDispatch", IntegerType),
StructField("FirePreventionDistrict", StringType),
StructField("SupervisorDistrict", StringType),
StructField("Neighborhood", StringType),
StructField("Location", StringType),
StructField("RowID", StringType),
StructField("Delay", FloatType)
)
)
// Read the file using the CSV DataFrameReader
val sfFireFile= args(0)
val fireDF = spark.read.schema(schema)
.option("header", "true")
.csv(sfFireFile)
println(fireDF.count())
val fewFireDF = fireDF
.select("IncidentNumber", "AvailableDtTm", "CallType")
.where($"CallType" =!= "Medical Incident")
fewFireDF.show(5, false)
}
我有下一個錯誤:
- 無法決議多載方法“where”
- 型別不匹配。必需的運算式,找到的字串 - 在“醫療事故”之后
當我嘗試編譯我的代碼時,出現下一個錯誤:
[error] /Users/xxxxxxx/Workspace/Learning/Spark/learning-spark/src/main/scala/org/example/chapter3/DepartmentCalls.scala:62:28: type mismatch; [error] found : String("Medical Incident") [error] required: org.apache.spark.sql.catalyst.expressions.Expression [error] .where($"CallType" =!= "Medical Incident") [error]
^ [error] one error found [error] (Compile / compileIncremental) Compilation failed
uj5u.com熱心網友回復:
您可能缺少呼叫站點范圍內的匯入。該$<column name>快捷方式通常是通過呼叫介紹import sparksession.implicits._。如果您啟用了“優化匯入”,Intellij 通常會洗掉此匯入,因為它無法識別它正在使用中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/338803.html
標籤:scala apache-spark apache-spark-sql
