示例 Mlib 程序中的 AbstractMethodError

AbstractMethodError in sample Mlib program

我正在尝试从 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.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)

火花初学者,所以能告诉我错误到底是什么吗?

这是源代码(除了输入文件的名称外,与 mlib 文档非常相似)

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 依赖项

确保您拥有最新版本的 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>

解决了这个问题 java.lang.AbstractMethodError 仅在我们尝试调用抽象方法时出现,当然可以在编译时捕获。

唯一会在运行时发生的情况是在 IDE 中键入方法期间的 class 与运行时不同。

所以这是一个非常奇怪的 jar 文件损坏案例。清除了 m2 home 并再次进行了 mvn clean install',它 运行 很好。呸!