一.环境配置
Myeclipse中虽然已经集成了maven插件,但是由于这个插件版本较低,建立maven project会出现错误。
解决办法:自己到官网http://maven.apache.org/下载最新版本的maven插件,解压,在环境变量中注册。
新建环境变量M2_HOME
在PATH里加入maven的bin的路径
配置完毕后,在Windows命令提示符下,输入mvn -v测试一下,配置成功显示如图:
配置成功后,还需要在Myeclipse中用新的maven插件将就得替换掉,如图:
二. Spark应用开发
1. 创建Maven Project
2. 编写Java源程序
/* SimpleApp.java */ import org.apache.spark.api.java.*; import org.apache.spark.SparkConf; import org.apache.spark.api.java.function.Function; public class SimpleApp { public static void main(String[] args) { String logFile = "file:///spark-bin-0.9.1/README.md"; SparkConf conf =new SparkConf().setAppName("Spark Application in Java"); 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); } }
3. 修改pom.xml添加依赖包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.cas.siat.dolphin</groupId> <artifactId>spark.SimpleApp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spark.SimpleApp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-core_2.10</artifactId> <version>1.0.2</version> </dependency> </dependencies> </project>
4. 编译打包:
分别执行Maven clean 、 Maven install,这样再工程项目下的target目录中会生成项目的jar包,如下图:
5. 运行Spark应用
上传编译好的jar包至spark集群client,执行以下命令运行程序
./spark-submit --class "foo.App" --master spark://172.21.5.235:7077 /home/hadoop121/Dolphin/Spark1.0.2/spark.SimpleApp-0.0.1-SNAPSHOT.jar
6.执行结果
Web UI结果