gpt4 book ai didi

java - 示例 Mlib 程序中的 AbstractMethodError

转载 作者:行者123 更新时间:2023-12-01 22:22:51 28 4
gpt4 key购买 nike

我正在尝试从 Apache Spark 示例 mlib 推荐器 http://spark.apache.org/docs/1.2.1/mllib-collaborative-filtering.html#examples 构建示例推荐器在Java中我得到了,但是当我构建它时(在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)

初学者,所以可以告诉我错误到底是什么?

这是源代码(与 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 - 示例 Mlib 程序中的 AbstractMethodError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389193/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com