Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/1311#discussion_r14862915
--- Diff: docs/mllib-collaborative-filtering.md ---
@@ -99,7 +99,88 @@ val model = ALS.trainImplicit(ratings, rank,
numIterations, alpha)
All of MLlib's methods use Java-friendly types, so you can import and call
them there the same
way you do in Scala. The only caveat is that the methods take Scala RDD
objects, while the
Spark Java API uses a separate `JavaRDD` class. You can convert a Java RDD
to a Scala one by
-calling `.rdd()` on your `JavaRDD` object.
+calling `.rdd()` on your `JavaRDD` object. A standalone application example
+that is equivalent to the provided example in Scala is given bellow:
+
+{% highlight java %}
+import scala.Product;
+import scala.Tuple2;
+
+import org.apache.spark.api.java.*;
+import org.apache.spark.SparkConf;
+import org.apache.spark.api.java.function.Function;
+import org.apache.spark.mllib.recommendation.ALS;
+import org.apache.spark.mllib.recommendation.Rating;
+import org.apache.spark.mllib.recommendation.MatrixFactorizationModel;
+
+public class CollaborativeFiltering {
+ public static void main(String[] args) {
+ SparkConf conf = new SparkConf().setAppName("Collaborative Filtering
Example");
+ JavaSparkContext sc = new JavaSparkContext(conf);
+
+ // Load and parse the data
+ String path = "/home/michael/workspace/spark/mllib/data/als/test.data";
+ JavaRDD<String> data = sc.textFile(path);
+ JavaRDD<Rating> ratings = data.map(
+ new Function<String, Rating>() {
+ public Rating call(String s) {
+ String[] sarray = s.split(",");
+ return new Rating(Integer.parseInt(sarray[0]),
Integer.parseInt(sarray[1]),
+ Double.parseDouble(sarray[2]));
+ }
+ }
+ );
+
+ // Build the recommendation model using ALS
+ int rank = 10;
+ int numIterations = 20;
+ MatrixFactorizationModel model = ALS.train(JavaRDD.toRDD(ratings),
rank, numIterations, 0.01);
+
+ // Evaluate the model on rating data
+ JavaRDD<Tuple2<Object, Object>> userProducts = ratings.map(
+ new Function<Rating, Tuple2<Object, Object>>() {
+ public Tuple2<Object, Object> call(Rating r) {
+ return new Tuple2<Object, Object>(r.user(), r.product());
+ }
+ }
+ );
+ JavaPairRDD<Tuple2<Integer, Integer>, Double> predictions =
JavaPairRDD.fromJavaRDD(
+ model.predict(JavaRDD.toRDD(userProducts)).toJavaRDD().map(
+ new Function<Rating, Tuple2<Tuple2<Integer, Integer>, Double>>() {
+ public Tuple2<Tuple2<Integer, Integer>, Double> call(Rating r){
+ return new Tuple2<Tuple2<Integer, Integer>, Double>(
+ new Tuple2<Integer, Integer>(r.user(), r.product()),
r.rating());
+ }
+ }
+ ));
+ JavaPairRDD<Tuple2<Integer, Integer>, Tuple2<Double, Double>>
ratesAndPreds =
+ JavaPairRDD.fromJavaRDD(ratings.map(
+ new Function<Rating, Tuple2<Tuple2<Integer, Integer>, Double>>() {
+ public Tuple2<Tuple2<Integer, Integer>, Double> call(Rating r){
+ return new Tuple2<Tuple2<Integer, Integer>, Double>(
+ new Tuple2<Integer, Integer>(r.user(), r.product()),
r.rating());
+ }
+ }
+ )).join(predictions);
+ double MSE = JavaDoubleRDD.fromRDD(JavaRDD.toRDD(ratesAndPreds.map(
+ new Function<Tuple2<Tuple2<Integer, Integer>, Tuple2<Double,
Double>>, Object>() {
+ public Object call(Tuple2<Tuple2<Integer, Integer>, Tuple2<Double,
Double>> key_value) {
--- End diff --
`key_value` -> `keyValue` or `pair`
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---