设为首页 收藏本站
查看: 981|回复: 0

[经验分享] Spark开发环境搭建

[复制链接]

尚未签到

发表于 2017-3-2 10:50:43 | 显示全部楼层 |阅读模式
  Spark的一些基本情况如下:


Spark:一个java web框架

License:Apache License

服务器:Jettry

jre版本:8

github地址:https://github.com/perwendel/spark

官方文档:http://sparkjava.com/documentation



准备工作:

    下载并配置eclipse for javaee、maven、jdk1.8

    大概的了解一下以下东西:

            java8里的lambda表达式是什么

            jetty是什么

            jetty嵌入方式开发是什么样的



环境搭建:

    1,新建一个web maven工程

            注:若eclipse新建的maven工程并没有包含web相关配置,可以新建一个web工程然后对工程右键configure --> convert to maven project,来转换为maven工程即可。

            检查工程中是否有src/mian/java、src/test/java、src/main/resources这几个source folder,若没有则在windows资源管理器中手动创建,然后回到工程打开“project视图”,找到刚才新建的目录,点击右键”build path->Use as Source Folder“将其作为source folder。



    2,将工程编码设置为utf-8,maven也是,如下




<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>


    3,设置maven的使用java8编译




<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>




4,加入Spark和日志类库(加入spark后会自动加入jettry的类库)




<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>




5,编写“hello world”



下面是官方示例




package me.ooi.testSpark;
import static spark.Spark.*;
public class HelloSpark {
public static void main(String[] args) {
get("/hello", (request, response) -> "Hello World!");
}
}
      注:若你在 get("/hello", ......) ,这个地方感觉很奇怪,奇怪这个get是哪里来的,可以先看一下如下写法





package me.ooi.testSpark;
import spark.Spark;
public class HelloSpark {
public static void main(String[] args) {        
Spark.get("/hello", (request, response) -> "Hello World!");        
}
}






然后看一下“import static”的用处,就会明白了

“import static”的官方解释:https://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html



6,执行这个main方法就可以访问了



    访问地址:http://localhost:4567/hello



7,发布(发布为单个jar文件)

在pom.xml中添加如下配置以用于打成单个jar包(“me.ooi.testSpark.HelloSpark”这个文件是入口)




<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<!-- This tells Maven to include all dependencies -->
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>me.ooi.testSpark.HelloSpark</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
  并将<packaging>war</packaging>改为<packaging>jar</packaging>





然后对工程右键“Run As -> maven clean”,若没有问题,再“Run As -> maven install”,若没有报错就ok了。

导出的jar包文件名形如“testSpark-0.0.1-SNAPSHOT-jar-with-dependencies.jar”这样的。

运行:

        在cmd窗口使用“java -jar testSpark-0.0.1-SNAPSHOT-jar-with-dependencies.jar”

停止:

        直接关闭cmd窗口即可





注:若需要修改日志输出参数,则需要在classpath下面新建一个名为simplelogger.properties(若使用的是slf4j-simple)的文件,下面是slf4j-simple官网上可以设置的参数说明




#org.slf4j.simpleLogger.logFile - The output target which can be the path to a file, or the special values "System.out" and "System.err". Default is "System.err".
#org.slf4j.simpleLogger.defaultLogLevel - Default log level for all instances of SimpleLogger. Must be one of ("trace", "debug", "info", "warn", or "error"). If not specified, defaults to "info".
#org.slf4j.simpleLogger.log.a.b.c - Logging detail level for a SimpleLogger instance named "a.b.c". Right-side value must be one of "trace", "debug", "info", "warn", or "error". When a SimpleLogger named "a.b.c" is initialized, its level is assigned from this property. If unspecified, the level of nearest parent logger will be used, and if none is set, then the value specified by org.slf4j.simpleLogger.defaultLogLevel will be used.
#org.slf4j.simpleLogger.showDateTime - Set to true if you want the current date and time to be included in output messages. Default is false
#org.slf4j.simpleLogger.dateTimeFormat - The date and time format to be used in the output messages. The pattern describing the date and time format is defined by SimpleDateFormat. If the format is not specified or is invalid, the number of milliseconds since start up will be output.
#org.slf4j.simpleLogger.showThreadName -Set to true if you want to output the current thread name. Defaults to true.
#org.slf4j.simpleLogger.showLogName - Set to true if you want the Logger instance name to be included in output messages. Defaults to true.
#org.slf4j.simpleLogger.showShortLogName - Set to true if you want the last component of the name to be included in output messages. Defaults to false.
#org.slf4j.simpleLogger.levelInBrackets - Should the level string be output in brackets? Defaults to false.
#org.slf4j.simpleLogger.warnLevelString - The string value output for the warn level. Defaults to WARN.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-349214-1-1.html 上篇帖子: 各种 starter poms (启动器) 下篇帖子: Java web项目
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表