我正在处理 2 个 CSV 文件,以使用 json4s 库连接数据并生成 JSON 有效负载。我在使用 UDF 映射火花数据集行时遇到了问题。
我尝试创建一个简单的UDF,接受行并返回硬编码值。问题仍然是一样的。
val station_data = spark.read.format("csv").option("sep", ",").option("inferSchema", "false").option("header", "true").load("gs://loyds-assignment/station_data.csv").drop("lat").drop("long").drop("dockcount").drop("installation")
val trip_data = spark.read.format("csv").option("sep", ",").option("inferSchema", "false").option("header", "true").load("gs://loyds-assignment/trip_data.csv").drop("Start Date").drop("End Date").drop("Subscriber Type").drop("Zip Code")
val getConcatenated = udf((first: String, second: String) => {
first + "," + second
})
val StatStationData = trip_data.join(station_data, col("Start Terminal") === col("station_id"), "inner").withColumn("Start Station", col("name")).withColumn("StartStationlandmark", col("landmark")).drop("name").drop("Start Terminal").drop("station_id").drop("landmark")
val FinalData = StatStationData.join(station_data, col("End Terminal") === col("station_id"), "inner").withColumn("End Station", col("name")).withColumn("Final landmark", when(col("landmark") === col("StartStationlandmark"), col("landmark")).otherwise(getConcatenated($"landmark", $"StartStationlandmark"))).drop("name").drop(("End Terminal")).drop("station_id").drop("landmark").drop("StartStationlandmark")
val FinalDataDf = FinalData.withColumn("TripID", col("Trip ID")).withColumn("EndStation", col("End Station")).withColumn("landmark", split(col("Final landmark"), "\,")).withColumn("Bike", col("Bike #")).withColumn("StartStation", col("Start Station")).drop("Trip ID").drop("End Station").drop("Final landmark").drop("Bike #").drop("Start Station")
FinalDataDf.show(false)
case class FinalDataStruct(TripID: String, Duration: String, Bike: String, StartStation: String, EndStation: String, landmark: String)
val encoder = org.apache.spark.sql.Encoders.product[FinalDataStruct]
val FinalDataDS = FinalDataDf.as(encoder)
FinalDataDS.show(false)
import spark.sqlContext.implicits._
import org.apache.spark.sql._
import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.jackson.JsonMethods._
def convertRowToJSON(row: Row) = {
val json =
("bike" -> row(3).toString) ~
("start_station" -> row(4).toString) ~
("end_station" -> row(5).toString) ~
("landmarks" -> row(6).toString) ~
("total_duration" -> row(2).toString)
(row(1).toString, compact(render(json)).toString)
}
val JsonPlayloadData = FinalDataDS.map(convertRowToJSON)
// To Test
def convertRowToJSONTtry(row: Row) = {
(11, "Hello".toString)
}
val JsonPlayloadDataTest1 = FinalDataDS.map(convertRowToJSONTtry)
我得到的错误是:
scala> val JsonPlayloadData = FinalDataDS.map(convertRowToJSON)
<console>:42: error: type mismatch;
found : org.apache.spark.sql.Row => (String, String)
required: FinalDataStruct => ?
val JsonPlayloadData = FinalDataDS.map(convertRowToJSON)
错误消息几乎告诉您这里需要了解的所有信息。您定义的函数在映射Dataset[FinalDataStruct]
(不是 udf)并需要FinalDataStruct => ?
时Row => (String, String)
。
如果要使用这个,请在DataFrame
上应用它:
FinalDataDf.map(convertRowToJSON)
使用Dataset[FinalDataStruct]
:
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.Serialization
import org.json4s.jackson.Serialization.write
FinalDataDS.map { x =>
implicit val formats = DefaultFormats
(x.TripID, write(x))
}
尽管在实践中最好将 map 替换为to_json
调用 - Spark Row 到 JSON。
另请注意,Rows
是从 0 而不是从 1 开始索引的。