Skip to content
Snippets Groups Projects
Commit 8ac0f35b authored by Stephen Haberman's avatar Stephen Haberman
Browse files

Add JavaRDDLike.keyBy.

parent 4ee6b227
No related branches found
No related tags found
No related merge requests found
...@@ -298,4 +298,12 @@ trait JavaRDDLike[T, This <: JavaRDDLike[T, This]] extends Serializable { ...@@ -298,4 +298,12 @@ trait JavaRDDLike[T, This <: JavaRDDLike[T, This]] extends Serializable {
* Save this RDD as a SequenceFile of serialized objects. * Save this RDD as a SequenceFile of serialized objects.
*/ */
def saveAsObjectFile(path: String) = rdd.saveAsObjectFile(path) def saveAsObjectFile(path: String) = rdd.saveAsObjectFile(path)
/**
* Creates tuples of the elements in this RDD by applying `f`.
*/
def keyBy[K](f: JFunction[T, K]): JavaPairRDD[K, T] = {
implicit val kcm: ClassManifest[K] = implicitly[ClassManifest[AnyRef]].asInstanceOf[ClassManifest[K]]
JavaPairRDD.fromRDD(rdd.keyBy(f))
}
} }
...@@ -629,4 +629,16 @@ public class JavaAPISuite implements Serializable { ...@@ -629,4 +629,16 @@ public class JavaAPISuite implements Serializable {
floatAccum.setValue(5.0f); floatAccum.setValue(5.0f);
Assert.assertEquals((Float) 5.0f, floatAccum.value()); Assert.assertEquals((Float) 5.0f, floatAccum.value());
} }
@Test
public void keyBy() {
JavaRDD<Integer> rdd = sc.parallelize(Arrays.asList(1, 2));
List<Tuple2<String, Integer>> s = rdd.keyBy(new Function<Integer, String>() {
public String call(Integer t) throws Exception {
return t.toString();
}
}).collect();
Assert.assertEquals(new Tuple2<String, Integer>("1", 1), s.get(0));
Assert.assertEquals(new Tuple2<String, Integer>("2", 2), s.get(1));
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment