Riccardo Fiorini
Riccardo Fiorini

Reputation: 31

How to transform a DataFrame to and RDD[Point] instead of RDD[ROW]?

I have a dataframe with many columns, that I have created from a csv file defining a schema. The only column I'm interest in is a column called "Point", where I defined a magellan Point(long, lat). What I need to do now, is creating an RDD[Point] from that dataframe.

Below is the code that I have tried, but it does not work since rdd is a RDD[Row] instead of RDD[Point].

val schema = StructType(Array(
         StructField("vendorId", StringType, false),
         StructField("lpep_pickup_datetime", StringType, false),
         StructField("Lpep_dropoff_datetime", StringType, false),
         StructField("Store_and_fwd_flag",StringType, false),
         StructField("RateCodeID", IntegerType, false),
         StructField("Pickup_longitude", DoubleType, false),
         StructField("Pickup_latitude", DoubleType, false),
         StructField("Dropoff_longitude", DoubleType, false),
         StructField("Dropoff_latitude", DoubleType, false),
         StructField("Passenger_count", IntegerType, false),
         StructField("Trip_distance", DoubleType, false),
         StructField("Fare_amount", StringType, false),
         StructField("Extra", StringType, false),
         StructField("MTA_tax", StringType, false),
         StructField("Tip_amount", StringType, false),
         StructField("Tolls_amount", StringType, false),
         StructField("Ehail_fee", StringType, false),
         StructField("improvement_surcharge", StringType, false),
         StructField("Total_amount", DoubleType, false),
         StructField("Payment_type", IntegerType, false),
         StructField("Trip_type", IntegerType, false)))

    import spark.implicits._

    val points = spark.read.option("mode", "DROPMALFORMED")
     .schema(schema)
     .csv("/home/riccardo/Scrivania/Progetto/Materiale/NYC-taxi/")
     .withColumn("point", point($"Pickup_longitude",$"Pickup_latitude"))
     .limit(2000)

    val rdd = points.select("point").rdd

How can I obtain an RDD[Point] instead of RDD[Row] from the dataframe? If it is not possible, which solution would you suggest? I need a RDD[Point] to work with a provided library that takes RDD[Point] as input.

Upvotes: 0

Views: 143

Answers (2)

pasha701
pasha701

Reputation: 7207

Methods "as" and "rdd" can help:

case class Point(latitude: Double, longitude: Double)

val df = Seq((1.0, 2.0)).toDF("Pickup_longitude", "Pickup_latitude")

val rdd = df
  .select(
    $"Pickup_longitude".alias("latitude"),
    $"Pickup_latitude".alias("longitude"))
  .as[Point].rdd

rdd.foreach(println)

Output:

Point(1.0,2.0)

Upvotes: 0

Prasad Khode
Prasad Khode

Reputation: 6739

If I understand correctly, you want the result to be of a custom class type which is Point instead of Row type

This is what I have tried:

My input data sample is :

latitude,longitude
44.968046,-94.420307
44.968046,-94.420307
44.33328,-89.132008
33.755787,-116.359998
33.844843,-116.54911
44.92057,-93.44786
44.240309,-91.493619
44.968041,-94.419696
44.333304,-89.132027

I have created my custom class with toString()

case class Pair(latitude: Double, longitude: Double) {
  override def toString: String = s"Pair($latitude, $longitude)"
}

Now I read the input file using spark as DataFrame and covert the same into RDD

val df = sparkSession.read.option("inferSchema", "true")
  .option("header", "true")
  .csv("/home/prasadkhode/sample_input.csv")

df.printSchema()
df.show()

val rdd = df.rdd.map(row => {
  Pair(row.getAs[Double]("latitude"), row.getAs[Double]("longitude"))
})

println(s"df count : ${df.count}")
println(s"rdd count : ${rdd.count}")

rdd.take(20).foreach(println)

and finally the result is as follows:

root
 |-- latitude: double (nullable = true)
 |-- longitude: double (nullable = true)

+---------+-----------+
| latitude|  longitude|
+---------+-----------+
|44.968046| -94.420307|
|44.968046| -94.420307|
| 44.33328| -89.132008|
|33.755787|-116.359998|
|33.844843| -116.54911|
| 44.92057|  -93.44786|
|44.240309| -91.493619|
|44.968041| -94.419696|
|44.333304| -89.132027|
+---------+-----------+

df count : 9
rdd count : 9

Pair(44.968046, -94.420307)
Pair(44.968046, -94.420307)
Pair(44.33328, -89.132008)
Pair(33.755787, -116.359998)
Pair(33.844843, -116.54911)
Pair(44.92057, -93.44786)
Pair(44.240309, -91.493619)
Pair(44.968041, -94.419696)
Pair(44.333304, -89.132027)

Hope this helps you... :-)

Upvotes: 1

Related Questions