I'm trying to build a sample recommender from the Apache spark sample mlib recommender http://spark.apache.org/docs/1.2.1/mllib-collaborative-filtering.html#examples in Java am getting but when i build it ( in IDEA intellij) the output logs show
Exception in thread "main" java.lang.AbstractMethodError
at org.apache.spark.Logging$class.log(Logging.scala:52)
at org.apache.spark.mllib.recommendation.ALS.log(ALS.scala:94)
at org.apache.spark.Logging$class.logInfo(Logging.scala:59)
at org.apache.spark.mllib.recommendation.ALS.logInfo(ALS.scala:94)
at org.apache.spark.mllib.recommendation.ALS$$anonfun$run$1.apply$mcVI$sp(ALS.scala:232)
at scala.collection.immutable.Range.foreach$mVc$sp(Range.scala:141)
at org.apache.spark.mllib.recommendation.ALS.run(ALS.scala:230)
at org.apache.spark.mllib.recommendation.ALS$.train(ALS.scala:599)
at org.apache.spark.mllib.recommendation.ALS$.train(ALS.scala:616)
at org.apache.spark.mllib.recommendation.ALS.train(ALS.scala)
at Sample.SimpleApp.main(SimpleApp.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Beginner to spark , so can tell me what the error exactly is ?
Here is the source ( exaclty similar to the mlib docs one , except name of the input file)
package Sample;
import scala.Tuple2;
import org.apache.spark.api.java.*;
import org.apache.spark.api.java.function.Function;
import org.apache.spark.mllib.recommendation.ALS;
import org.apache.spark.mllib.recommendation.MatrixFactorizationModel;
import org.apache.spark.mllib.recommendation.Rating;
import org.apache.spark.SparkConf;
public class SimpleApp {
public static void main(String[] args) {
SparkConf conf = new SparkConf().setAppName("Collaborative Filtering Example").setMaster("local");
JavaSparkContext sc = new JavaSparkContext(conf);
// Load and parse the data
String path = "/home/deeepak/somefile.txt";
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), 10, 20, 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());
}
}
));
JavaRDD<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).values();
double MSE = JavaDoubleRDD.fromRDD(ratesAndPreds.map(
new Function<Tuple2<Double, Double>, Object>() {
public Object call(Tuple2<Double, Double> pair) {
Double err = pair._1() - pair._2();
return err * err;
}
}
).rdd()).mean();
System.out.println("Mean Squared Error = " + MSE);
}
}
The error seems to be on line 36 . Java version used 1.8.40 and getting the spark dependencies using maven
Solved this issue a java.lang.AbstractMethodError occurs only when we are trying to call an abstract method and this ofcourse can be caught at compile time .
The only time it will occur at runtime is when the class during typing the method in the IDE is different from the one during runtime .
So it was a very wierd case of corruption of jar files . Cleared m2 home and mvn clean install'd again and it ran fine . Phew !