-2

I'm working on 2 CSV files to join data and produce JSON Payload using json4s library. I'm facing the issue in mapping spark dataset row with UDF.

I tried to create a simple UDF accepting row and returning hardcoded values. The issue remains the same.

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)

The error i get is :

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)
Tushar Kesarwani
  • 77
  • 1
  • 1
  • 14

1 Answers1

1

The error message tells you pretty much all there is to know here. Th function you defined is Row => (String, String) while you map over a Dataset[FinalDataStruct] (that's not udf) and need FinalDataStruct => ?.

If you want to use this one apply it on DataFrame:

FinalDataDf.map(convertRowToJSON)

On Dataset[FinalDataStruct] use:

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))
}

Though in practice it would be preferable to replace map with to_json call - Spark Row to JSON.

Additionally note that Rows are indexed from 0 not from 1.