使用Maven对Java独立应用程序进行编译打包
1. 安装Maven
网盘下载 apache-maven-3.9.2-bin.zip
链接为:https://pan.baidu.com/s/181shkgg-i0WEytQMqeeqxA(提取码:9ekc )
sudo unzip /export/server/apache-maven-3.9.2-bin.zip -d /export/server/ cd /export/server/ sudo mv apache-maven-3.9.2/ ./maven sudo chown -R hadoop ./maven
2. Java应用程序代码
cd /export/server mkdir -p sparkapp2/src/main/java
在 ./sparkapp2/src/main/java 下建立一个名为 SimpleApp.java 的文件(vim ./sparkapp2/src/main/java/SimpleApp.java),添加代码如下:
import org.apache.spark.api.java.*; import org.apache.spark.api.java.function.Function; import org.apache.spark.SparkConf; public class SimpleApp { public static void main(String[] args) { String logFile = "file:///export/server/spark/README.md"; // Should be some file on your system SparkConf conf=new SparkConf().setMaster("local").setAppName("SimpleApp"); JavaSparkContext sc=new JavaSparkContext(conf); JavaRDD<String> logData = sc.textFile(logFile).cache(); long numAs = logData.filter(new Function<String, Boolean>() { public Boolean call(String s) { return s.contains("a"); } }).count(); long numBs = logData.filter(new Function<String, Boolean>() { public Boolean call(String s) { return s.contains("b"); } }).count(); System.out.println("Lines with a: " + numAs + ", lines with b: " + numBs); } }
该程序依赖Spark Java API,因此我们需要通过Maven进行编译打包。在./sparkapp2目录中新建文件pom.xml,命令如下:
cd /export/server/sparkapp2 vim pom.xml
在pom.xml中添加相关依赖
<project> <groupId>cn.edu.xmu</groupId> <artifactId>simple-project</artifactId> <modelVersion>4.0.0</modelVersion> <name>Simple Project</name> <packaging>jar</packaging> <version>1.0</version> <repositories> <repository> <id>jboss</id> <name>JBoss Repository</name> <url>http://repository.jboss.com/maven2/</url> </repository> </repositories> <dependencies> <dependency> <!-- Spark dependency --> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.12</artifactId> <version>3.4.0</version> </dependency> </dependencies> </project>
3.使用Maven打Java程序
cd ~/sparkapp2 /export/server/maven/bin/mvn package
如出现下信息,说明生成Jar包成功:
参考博客:https://dblab.xmu.edu.cn/blog/4322/
标签:Java,应用程序,server,Maven,export,apache,java From: https://www.cnblogs.com/jiacheng-712/p/17794203.html