आप कुछ इस तरह कर सकते हैं:
val df = Seq(
(1,12345678,"this is a test"),
(1,23456789, "another test"),
(2,2345678,"2nd test"),
(2,1234567, "2nd another test")
).toDF("id","timestamp","data")
+---+---------+----------------+
| id|timestamp| data|
+---+---------+----------------+
| 1| 12345678| this is a test|
| 1| 23456789| another test|
| 2| 2345678| 2nd test|
| 2| 1234567|2nd another test|
+---+---------+----------------+
df.join(
df.groupBy($"id").agg(max($"timestamp") as "r_timestamp").withColumnRenamed("id", "r_id"),
$"id" === $"r_id" && $"timestamp" === $"r_timestamp"
).drop("r_id").drop("r_timestamp").show
+---+---------+------------+
| id|timestamp| data|
+---+---------+------------+
| 1| 23456789|another test|
| 2| 2345678| 2nd test|
+---+---------+------------+
तुम वहाँ एक id
के लिए एक दोहराया timestamp
हो सकता है (नीचे टिप्पणी देखें) उम्मीद हैं, तो आप ऐसा कर सकते हैं:
df.dropDuplicates(Seq("id", "timestamp")).join(
df.groupBy($"id").agg(max($"timestamp") as "r_timestamp").withColumnRenamed("id", "r_id"),
$"id" === $"r_id" && $"timestamp" === $"r_timestamp"
).drop("r_id").drop("r_timestamp").show
स्रोत
2016-04-14 13:54:35
क्या है 'डेटामैप 'का उद्देश्य? – zero323
वास्तव में यह 'data.map होना चाहिए (लैम्ब्डा x: (x.id, x))' (या 'keyBy')। आइए इसे ठीक करें। – zero323
बिल्कुल सही, अच्छा पकड़ – David