如何从Scala包中在Java中导入单例对象?

huangapple go评论149阅读模式
英文:

How do I import singleton object from Scala package in Java?

问题

我正在尝试在我的Java程序中使用从a package导入的ARIMA对象(Scala)。尽管编译成功,也就是ARIMA类在编译期间被识别,但在运行时出现了NoClassDefFoundError错误,无法找到ARIMA对象。由于ARIMAModel类是一个类,所以导入没有问题。

有没有办法从我的Java程序中使用这个Scala对象?

这是Scala包中对象的源代码。

文件:.../com/cloudera/sparkts/models/ARIMA.scala

package com.cloudera.sparkts.models

object ARIMA {
    def autoFit(ts: Vector, maxP: Int = 5, maxD: Int = 2, maxQ: Int = 5): ARIMAModel = {
        ...
    }
}

class ARIMAModel(...) {
    ...
}

这是我的Java代码。

文件:src/main/java/SingleSeriesARIMA.java

import com.cloudera.sparkts.models.ARIMA;
import com.cloudera.sparkts.models.ARIMAModel;

public class SingleSeriesARIMA {
    public static void main(String[] args) {
        ... 
        ARIMAModel arimaModel = ARIMA.autoFit(tsVector, 1, 0, 1);
        ...
    }
}

这是错误信息。

Exception in thread "main" java.lang.NoClassDefFoundError: com/cloudera/sparkts/models/ARIMA
    at SingleSeriesARIMA.main(SingleSeriesARIMA.java:43)
    ...
Caused by: java.lang.ClassNotFoundException: com.cloudera.sparkts.models.ARIMA
    ...

我使用的是Scala版本2.11.8和Java 1.8。

英文:

I am trying to use ARIMA object (Scala), which is imported from a package, in my Java program. Although the compilation succeeds, meaning that ARIMA class is recognized during compilation, there is NoClassDefFoundError for the ARIMA object in runtime. ARIMAModel class has no problem with importing since it is a class.

Is there any way to use the Scala object from my Java program?

Here is the source code for the object in Scala package.

File: .../com/cloudera/sparkts/models/ARIMA.scala

package com.cloudera.sparkts.models

object ARIMA {
    def autoFit(ts: Vector, maxP: Int = 5, maxD: Int = 2, maxQ: Int = 5): ARIMAModel = {
        ...
    }
}

class ARIMAModel(...) {
    ...
}

Here is my Java code.

File: src/main/java/SingleSeriesARIMA.java

import com.cloudera.sparkts.models.ARIMA;
import com.cloudera.sparkts.models.ARIMAModel;

public class SingleSeriesARIMA {
    public static void main(String[] args) {
        ... 
        ARIMAModel arimaModel = ARIMA.autoFit(tsVector, 1, 0, 1);
        ...
    }
}

Here is the error.

Exception in thread "main" java.lang.NoClassDefFoundError: com/cloudera/sparkts/models/ARIMA
at SingleSeriesARIMA.main(SingleSeriesARIMA.java:43)
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:498)
at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:729)
at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:185)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:210)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:124)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
Caused by: java.lang.ClassNotFoundException: com.cloudera.sparkts.models.ARIMA
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 10 more

I am using Scala version 2.11.8 and Java 1.8

答案1

得分: 2

你需要使用以下方式将具有Arima对象的依赖项提供给Spark集群,使用--jars选项如下所示:

spark-submit --jars <path>/<to>/sparkts-0.4.1.jar --class SingleSeriesARIMA target/simple-project-1.0.jar

这将传递其他依赖项,同时将应用程序jar可用于spark-runtime

要从Java中调用ARIMA对象,请使用以下方式:

ARIMA$.MODULE$.autoFit(tsVector, 1, 0, 1);
英文:

You need to supply the dependency having Arima object present to the spark cluster using --jars option as below-

spark-submit --jars <path>/<to>/sparkts-0.4.1.jar --class SingleSeriesARIMA target/simple-project-1.0.jar

This will pass the other dependency along with the application jar to be available at spark-runtime.

TO call ARIMA object from java use-

ARIMA$.MODULE$.autoFit(tsVector, 1, 0, 1);

huangapple
  • 本文由 发表于 2020年8月14日 07:16:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63404437.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定