Skip to content

Commit 4ec4fdb

Browse files
Wosinpivovarit
authored andcommitted
Bael 43 (eugenp#6423)
* BAEL-43: Apache Spark with Spring Cloud Data Flow * Replaced functions with lambdas + removed unnecessary type casting.
1 parent c0744de commit 4ec4fdb

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>spring-cloud-data-flow</artifactId>
7+
<groupId>com.baeldung</groupId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>apache-spark-job</artifactId>
13+
<dependencies>
14+
<dependency>
15+
<groupId>org.springframework.cloud</groupId>
16+
<artifactId>spring-cloud-task-core</artifactId>
17+
<version>2.0.0.RELEASE</version>
18+
</dependency>
19+
<dependency>
20+
<groupId>org.apache.spark</groupId>
21+
<artifactId>spark-core_2.10</artifactId>
22+
<version>1.6.2</version>
23+
<exclusions>
24+
<exclusion>
25+
<groupId>log4j</groupId>
26+
<artifactId>log4j</artifactId>
27+
</exclusion>
28+
<exclusion>
29+
<groupId>org.slf4j</groupId>
30+
<artifactId>slf4j-log4j12</artifactId>
31+
</exclusion>
32+
</exclusions>
33+
</dependency>
34+
</dependencies>
35+
36+
</project>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baeldung.spring.cloud;
2+
3+
import org.apache.spark.SparkConf;
4+
import org.apache.spark.api.java.JavaRDD;
5+
import org.apache.spark.api.java.JavaSparkContext;
6+
import org.apache.spark.api.java.function.Function;
7+
import org.apache.spark.api.java.function.Function2;
8+
import org.apache.spark.rdd.RDD;
9+
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.IntStream;
13+
14+
public class PiApproximation {
15+
public static void main(String[] args) {
16+
SparkConf conf = new SparkConf().setAppName("BaeldungPIApproximation");
17+
JavaSparkContext context = new JavaSparkContext(conf);
18+
int slices = args.length >= 1 ? Integer.valueOf(args[0]) : 2;
19+
int n = (100000L * slices) > Integer.MAX_VALUE ? Integer.MAX_VALUE : 100000 * slices;
20+
21+
List<Integer> xs = IntStream.rangeClosed(0, n)
22+
.mapToObj(element -> Integer.valueOf(element))
23+
.collect(Collectors.toList());
24+
25+
JavaRDD<Integer> dataSet = context.parallelize(xs, slices);
26+
27+
JavaRDD<Integer> pointsInsideTheCircle = dataSet.map(integer -> {
28+
double x = Math.random() * 2 - 1;
29+
double y = Math.random() * 2 - 1;
30+
return (x*x + y*y ) < 1 ? 1: 0;
31+
});
32+
33+
int count = pointsInsideTheCircle.reduce((integer, integer2) -> integer + integer2);
34+
35+
System.out.println("The pi was estimated as:" + count / n);
36+
37+
context.stop();
38+
}
39+
}

‎spring-cloud-data-flow/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<module>log-sink</module>
2121
<module>batch-job</module>
2222
<module>etl</module>
23+
<module>apache-spark-job</module>
2324
</modules>
2425

2526
</project>

0 commit comments

Comments
 (0)