package net.jgp.labs.spark.l801_pi_in_progress;

import java.util.ArrayList;
import java.util.List;

import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.sql.SparkSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.jgp.labs.spark.x.datasource.SubStringCounterDataSource;

public class PiApp {
	private static Logger log = LoggerFactory.getLogger(SubStringCounterDataSource.class);
	private static final int NUM_SAMPLES = 10;

	public static void main(String[] args) {
		PiApp app = new PiApp();
		app.start();
	}

	private void start() {
	// @formatter:off
    SparkSession spark = SparkSession.builder()
	.appName("JavaSparkPi")
	.master("spark://52.41.197.177:7077")
	.config("spark.executor.memory", "1g")
	.config("spark.executor.cores", "1")
	.config("spark.cores.max","2")
	.getOrCreate();
    // @formatter:on

		JavaSparkContext jsc = new JavaSparkContext(spark.sparkContext());

		int n = 1 * NUM_SAMPLES;
		List<Integer> l = new ArrayList<>(n);
		for (int i = 0; i < n; i++) {
			l.add(i);
		}

		JavaRDD<Integer> dataSet = jsc.parallelize(l, NUM_SAMPLES);

		long t0 = System.currentTimeMillis();

		long count = dataSet.map(integer -> {
			double x = Math.random() * 2 - 1;
			double y = Math.random() * 2 - 1;
			return (x * x + y * y <= 1) ? 1 : 0;
		}).reduce((integer, integer2) -> integer + integer2);

		long t1 = System.currentTimeMillis();

		log.info("Pi is roughly ..... {}", 4.0 * count / n);
		log.info("Processing time ... {} ms", t1 - t0);

		spark.stop();
	}
}

@@ -376,17 +376,20 @@
 PiApp %7B%0A
-%09
+    
 private 
@@ -467,17 +467,20 @@
 class);%0A
-%09
+    
 private 
@@ -515,17 +515,20 @@
  = 10;%0A%0A
-%09
+    
 public s
@@ -560,18 +560,24 @@
 args) %7B%0A
-%09%09
+        
 PiApp ap
@@ -593,18 +593,24 @@
 iApp();%0A
-%09%09
+        
 app.star
@@ -614,21 +614,27 @@
 tart();%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 private 
@@ -652,27 +652,13 @@
 ) %7B%0A
-%09// @formatter:off%0A
+%0A    
     
@@ -700,18 +700,16 @@
 uilder()
-%0A%09
 .appName
@@ -724,17 +724,32 @@
 arkPi%22)%0A
-%09
+                
 .master(
@@ -761,30 +761,43 @@
 k://
-52.41.197.177:7077%22)%0A%09
+10.0.100.81:7077%22)%0A                
 .con
@@ -831,17 +831,32 @@
 , %221g%22)%0A
-%09
+                
 .config(
@@ -884,17 +884,32 @@
 %22, %221%22)%0A
-%09
+                
 .config(
@@ -930,14 +930,91 @@
 ax%22,
+ 
 %222%22)%0A
-%09
+                .config(%22spark.driver.host%22, %2210.0.100.182%22)%0A                
 .get
@@ -1029,32 +1029,17 @@
 ();%0A
+%0A
     
-// @formatter:on%0A%0A%09%09
+    
 Java
@@ -1102,18 +1102,24 @@
 xt());%0A%0A
-%09%09
+        
 int n = 
@@ -1135,18 +1135,24 @@
 AMPLES;%0A
-%09%09
+        
 List%3CInt
@@ -1181,18 +1181,24 @@
 t%3C%3E(n);%0A
-%09%09
+        
 for (int
@@ -1223,28 +1223,49 @@
 ) %7B%0A
-%09%09%09l.add(i);%0A%09%09%7D%0A%0A%09%09
+            l.add(i);%0A        %7D%0A%0A        
 Java
@@ -1317,26 +1317,32 @@
 _SAMPLES);%0A%0A
-%09%09
+        
 long t0 = Sy
@@ -1364,26 +1364,32 @@
 eMillis();%0A%0A
-%09%09
+        
 long count =
@@ -1414,19 +1414,28 @@
 er -%3E %7B%0A
-%09%09%09
+            
 double x
@@ -1460,19 +1460,28 @@
  2 - 1;%0A
-%09%09%09
+            
 double y
@@ -1510,11 +1510,20 @@
  1;%0A
-%09%09%09
+            
 retu
@@ -1555,18 +1555,24 @@
  1 : 0;%0A
-%09%09
+        
 %7D).reduc
@@ -1618,18 +1618,24 @@
 ger2);%0A%0A
-%09%09
+        
 long t1 
@@ -1665,18 +1665,24 @@
 lis();%0A%0A
-%09%09
+        
 log.info
@@ -1726,18 +1726,24 @@
 t / n);%0A
-%09%09
+        
 log.info
@@ -1787,10 +1787,16 @@
 );%0A%0A
-%09%09
+        
 spar
@@ -1809,9 +1809,12 @@
 ();%0A
-%09
+    
 %7D%0A%7D%0A

Work on S3 amd Pi