示例Mlib程序中的AbstractMethodError

时间:2015-04-01 11:07:54

标签: java apache-spark recommendation-engine apache-spark-mllib

我正在尝试使用Java中的Apache spark样本mlib推荐程序http://spark.apache.org/docs/1.2.1/mllib-collaborative-filtering.html#examples构建一个示例推荐程序但是当我构建它时(在IDEA intellij中)输出日志显示

线程中的异常" 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)

初学者要发火花,那么可以告诉我错误究竟是什么?

这是源代码(exaclty类似于mlib docs one,输入文件的名称除外)

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);

    }
}

错误似乎在第36行。 Java版本使用1.8.40并使用maven获取spark依赖性

2 个答案:

答案 0 :(得分:2)

确保您拥有最新版本的spark和mlib

的pom.xml:

<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.10</artifactId>
    <version>1.3.1</version>
</dependency>

<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-mllib_2.10</artifactId>
    <version>1.3.1</version>
</dependency>

答案 1 :(得分:1)

解决了这个问题 只有在我们尝试调用抽象方法时才会发生java.lang.AbstractMethodError,并且可以在编译时捕获此过程。

在运行时唯一的时间是在IDE中键入方法时的类与运行时期间的类不同。

因此,这是一个非常奇怪的jar文件损坏案例。清理了m2 home和mvn clean再次安装,它运行良好。 P!