Giter Club home page Giter Club logo

Comments (9)

srowen avatar srowen commented on July 22, 2024

I'm not familiar with this code but the key is not lic but (lic, f(trip)) and so it is likely to be intended. Values are separately sorted and split. @sryza ?

from aas.

yaominator avatar yaominator commented on July 22, 2024

As far as I can tell, for this line

implicit val ordering: Ordering[(K,S)] = Ordering.by(_._1)

K is lic, S is f(trip). Ordering by(_._1) means to order by K , which is lic, right ? Did I miss something here?

from aas.

srowen avatar srowen commented on July 22, 2024

It should apply to the repartition and sort of presess which is not keyed by lic but a different tuple.

from aas.

yaominator avatar yaominator commented on July 22, 2024
val presess = rdd.map {
      case (lic, trip) => {
        ((lic, secondaryKeyFunc(trip)), trip)
      }
    }
    val partitioner = new FirstKeyPartitioner[K, S](numPartitions)
    implicit val ordering: Ordering[(K,S)] = Ordering.by(_._1)
    presess.repartitionAndSortWithinPartitions(partitioner).mapPartitions(groupSorted(_, splitFunc))

Here is what I understand.

presess is RDD of ((lic, secondaryKeyFunc(trip)), trip) . The key is composite key includes lic and timestamp of the trip. The value is the trip.

    implicit val ordering: Ordering[(K,S)] = Ordering.by(_._1)

This line indicates how we want to sort by the key, which is by the first element of the tuple( lic , timestamp) , which is lic in this case.

As I tried to run the code with small set of taxi data, I found that the data are not ordered by the pickup time.

from aas.

sryza avatar sryza commented on July 22, 2024

My reading is that the code in its present form is correct. In this case, secondaryKeyFunc returns the pickup time of the trip, so the tuples in presess are ((lic, pickup time), trip). The given Ordering orders on the first element of the outer tuple, which is the (lic, pickup time) inner tuple. Scala tuples have a natural lexicographic ordering, so the records will first be ordered by license, but within each license, will be ordered by pickup time. Which is the intended behavior. @yaominator does that conflict with what you're observing?

CC @jwills who wrote this code originally.

from aas.

yaominator avatar yaominator commented on July 22, 2024

Here is the mock data I am using for testing purpose

medallion,hack_license,vendor_id,rate_code,store_and_fwd_flag,pickup_datetime,dropoff_datetime,passenger_count,trip_time_in_secs,trip_distance,pickup_longitude,pickup_latitude,dropoff_longitude,dropoff_latitude
89D227B655E5C82AECF13C3F540D4CF4,BA96DE419E711691B9445D6A6307C170,CMT,1,N,2013-01-01 15:11:42,2013-01-01 15:18:10,4,382,1.00,-73.978165,40.757977,-73.989838,40.751171
89D227B655E5C82AECF13C3F540D4CF4,BA96DE419E711691B9445D6A6307C170,CMT,1,N,2013-01-01 15:11:49,2013-01-01 15:18:10,4,382,1.00,-73.978165,40.757977,-73.989838,40.751171
89D227B655E5C82AECF13C3F540D4CF4,BA96DE419E711691B9445D6A6307C170,CMT,1,N,2013-01-01 15:11:46,2013-01-01 15:18:10,4,382,1.00,-73.978165,40.757977,-73.989838,40.751171

I added a line to print out the trip

--- a/ch08-geotime/src/main/scala/com/cloudera/datascience/geotime/RunGeoTime.scala
+++ b/ch08-geotime/src/main/scala/com/cloudera/datascience/geotime/RunGeoTime.scala
@@ -101,7 +101,9 @@ object RunGeoTime extends Serializable {
     def secondaryKeyFunc(trip: Trip) = trip.pickupTime.getMillis
     val sessions = groupByKeyAndSortValues(taxiDone, secondaryKeyFunc, split, 30)
     sessions.cache()
+    sessions.values.foreach( trip => trip.toList.foreach(println))

What I got is

16/02/02 17:07:48 INFO BlockManagerInfo: Added rdd_26_17 in memory on localhost:64693 (size: 17.5 KB, free: 529.8 MB)
Trip(2013-01-01T15:11:42.000-05:00,2013-01-01T15:18:10.000-05:00,{"x":-73.978165,"y":40.757977},{"x":-73.989838,"y":40.751171})
Trip(2013-01-01T15:11:49.000-05:00,2013-01-01T15:18:10.000-05:00,{"x":-73.978165,"y":40.757977},{"x":-73.989838,"y":40.751171})
Trip(2013-01-01T15:11:46.000-05:00,2013-01-01T15:18:10.000-05:00,{"x":-73.978165,"y":40.757977},{"x":-73.989838,"y":40.751171})

As you can see, it is not ordered by pick up time. Keep the same order as is in the text file.

from aas.

sryza avatar sryza commented on July 22, 2024

I tried running the code on @yaominator 's data sample and hit the incorrect behavior as well. It looks my analysis of what was going on was incorrect, because, by the contract of repartionAndSortWithinPartitions, the given ordering only applies to keys, which are the (lic, pickup time) tuples. The ordering overrides the natural lexicographic tuple ordering to only look at the first element.

I've pushed a fix here: 5d0514c.

This code isn't included in the text of the book, so I don't think we need an accompanying change there.

from aas.

yaominator avatar yaominator commented on July 22, 2024

works great !!! +1

from aas.

sryza avatar sryza commented on July 22, 2024

Awesome

from aas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.