简体   繁体   English

java如何在apache服务器中运行spark app

[英]java how to run spark app within apache server

I have a Spark MVC app, which is very simple. 我有一个Spark MVC应用程序,非常简单。

According to spark documentation , this should be enough to run the app: 根据spark文档 ,这应该足以运行应用程序:

public class SparkServer {
    public static void main(String args[]) {
        Spark.staticFileLocation("src/main/webapp");
        System.out
                .println("bla bla bla");
        RService rService = new SparqlRService();
        new RController(rService);
    }
}

I put that class in a package inside my project, and I run the web app (dynamic web app) on Apache Tomcat server. 我将该类放在项目中的一个包中,然后在Apache Tomcat服务器上运行Web应用程序(动态Web应用程序)。

The print statement doesn't appear when run Apache Tomcat, that means this class is not being called. 运行Apache Tomcat时不会出现print语句,这意味着此类未被调用。 I know it makes sense . 我知道这很有道理 that is why i am asking. 这就是我要问的原因。 please how can I let Apache Tomcat run my spark app? 请问如何让Apache Tomcat运行我的spark应用程序?

Update 更新

After @mlk answer , i did the following: @mlk回答后 ,我做了以下事情:

public class SparkServer implements SparkApplication {

    @Override
    public void init() {
        Spark.staticFileLocation("src/main/webapp");
        System.out
                .println("bla bla lba");
        RService rService = new SparqlRService();
        new RController(rService);
    }
}

and in my web.xml i did: 在我的web.xml中我做了:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>SRecommender</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>SparkFilter</filter-name>
        <filter-class>spark.servlet.SparkFilter</filter-class>
        <init-param>
            <param-name>
    applicationClass</param-name>
            <param-value>com.srecommender.SparkServer</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>SparkFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

Where my SparkServer is in a package: com.recommender that exists in a source folder: src/main/java 我的SparkServer在一个包中: com.recommender存在于源文件夹中: src/main/java

I run apache tomcat, but still when i call any path from spark, it is returning 404. 我运行apache tomcat,但是当我从spark调用任何路径时,它仍然返回404。

HITE i can run spark from the main method and call my pages, they are working. HITE我可以从main方法运行spark并调用我的页面,它们正在工作。 so the problem with the way i configured spark to run in apache tomcat 所以我配置spark的方式问题在apache tomcat中运行

Update 2 更新2

This is how to set the path of the view 这是如何设置视图的路径

public RecommendationController(RecommendationService service) {
        get("/", (request, response) -> {

            Map<String, Object> model = new HashMap<>();
            model.put("data", "forza ROMA");
            model.put("data2", "It's Rome, It's home");

            return View("src/main/webapp/template/index.html", model);
        });

Spark comes with a container built in, so you don't need tomcat or jetty. Spark附带一个内置容器,因此您不需要tomcat或jetty。 If you want to deploy to a full blow container then you can by creating a web.xml file and implementing spark.servlet.SparkApplication . 如果要部署到完整的容器,则可以创建web.xml文件并实现spark.servlet.SparkApplication

Edit: you are missing the applicationClass property in your Web.xml. 编辑:您缺少Web.xml中的applicationClass属性。

WebApps don't execute static void main() methods, they bootstrap according to their web.xml from their deployed WAR file. WebApps不执行static void main()方法,它们根据其部署的WAR文件中的web.xml引导。

Do you have any experience with running webapps? 你有运行webapps的经验吗? You need a container like Tomcat or Jetty. 你需要一个像Tomcat或Jetty这样的容器。 Apache server is just for serving HTTP and is not an application container in itself. Apache服务器仅用于提供HTTP,并且本身不是应用程序容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Apache Spark 集群上运行 Java 程序? - How to run a java program on Apache Spark Cluster? 如何在Apache Spark Java应用程序中为每个Apache Spark执行器分配唯一的整数键? - How can I assign a unique integer key to every Apache Spark Executor within an Apache Spark Java Application? 如何从单独的Java程序中在群集上运行Spark程序? - How to run a spark program on cluster from within a separate java program? 如何在Xampp的apache服务器上运行eclipse中创建的Java webservice? - How run a java webservice made in eclipse run on the apache server in xampp? 如何使用Spark在服务器上运行Java程序? - How do I run a java program on a server with Spark? 如何在服务器上运行maven Java应用程序 - How to run a maven Java app on a server 如何在Apache Web服务器上运行Grails应用程序WAR文件? - How to run Grails app WAR file on Apache web server? 如何在 Apache 上运行 Java Server Faces tomcat 10 - How to run Java Server Faces on Apache tomcat 10 如何在没有Tomcat的情况下在Apache 2.2上运行Java应用程序? - How can I run a Java app on Apache 2.2 without Tomcat? Apache Spark:如何在Java中访问Tuple2对象中的嵌套整数数组? - Apache Spark: How can i access nested array of integers within Tuple2 object in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM