【发布时间】:2017-02-27 14:36:21
【问题描述】:
我是 Spark 的新手,正在尝试在 Eclipse 中运行 Naive Bayes Java 示例。尝试保存模型时,它会给出 No such method 错误。
SparkConf sparkConf = new SparkConf().setAppName("JavaNaiveBayesExample").setMaster("local");
JavaSparkContext jsc = new JavaSparkContext(sparkConf);
String path = "SPARK_HOME\\data\\mllib\\sample_libsvm_data.txt";
JavaRDD<LabeledPoint> inputData = MLUtils.loadLibSVMFile(jsc.sc(), path).toJavaRDD();
JavaRDD<LabeledPoint>[] tmp = inputData.randomSplit(new double[]{0.6, 0.4});
JavaRDD<LabeledPoint> training = tmp[0]; // training set
JavaRDD<LabeledPoint> test = tmp[1]; // test set
final NaiveBayesModel model = NaiveBayes.train(training.rdd(), 1.0);
JavaPairRDD<Double, Double> predictionAndLabel =
test.mapToPair(new PairFunction<LabeledPoint, Double, Double>() {
public Tuple2<Double, Double> call(LabeledPoint p) {
return new Tuple2(model.predict(p.features()), p.label());
}
});
double accuracy = predictionAndLabel.filter(new Function<Tuple2<Double, Double>, Boolean>() {
public Boolean call(Tuple2<Double, Double> pl) {
return pl._1().equals(pl._2());
}
}).count() / (double) test.count();
// Save and load model
model.save(jsc.sc(), "PATH_TO_FOLDER\\myNaiveBayesModel");
NaiveBayesModel sameModel = NaiveBayesModel.load(jsc.sc(), "PATH_TO_FOLDER\\myNaiveBayesModel");
jsc.close();
错误发生在 model.save() 行。
堆栈跟踪如下:
Exception in thread "main" java.lang.NoSuchMethodError: scala.reflect.api.JavaUniverse.runtimeMirror(Ljava/lang/ClassLoader;)Lscala/reflect/api/JavaMirrors$JavaMirror;
at org.apache.spark.mllib.classification.NaiveBayesModel$SaveLoadV2_0$.save(NaiveBayes.scala:205)
at org.apache.spark.mllib.classification.NaiveBayesModel.save(NaiveBayes.scala:170)
at AppMain.main(AppMain.java:42)
我该如何解决这个问题?任何帮助表示赞赏。
运行 Spark 2.0.1 和 Scala 2.11.8。
为 spark-core_2.11 和 spark-mllib_2.10 添加了依赖项。
【问题讨论】:
-
为什么你对 spark core 和 spark mllib 使用不同的版本?