concept approximation in category spark

appears as: n approximation, approximation
Spark in Action, Second Edition

This is an excerpt from Manning's book Spark in Action, Second Edition.

You’re still reading? Fine! In this short section, you will learn how to get an approximation of π by throwing darts and then implement the code in Spark. The result and actual code are in the next section.

Listing 6.1 Calculating an approximation of π
package net .jgp.books.spark.ch05.lab200_pi_compute_cluster;
...
public class PiComputeClusterApp implements Serializable {
...
     private final class DartMapper
         implements MapFunction<Row, Integer> {
...
    }
 
     private final class DartReducer implements ReduceFunction<Integer> {
...
    }
 
     public static void main(String[] args ) {
      PiComputeClusterApp app = new PiComputeClusterApp();
       app .start(10);
    }
 
     private void start(int slices ) {
       int numberOfThrows = 100000 * slices ;
...
      SparkSession spark = SparkSession #1
            .builder() #1
            .appName( "JavaSparkPi on a cluster" ) #1
            .master( "spark://un:7077" ) #1
            .config( "spark.executor.memory" , "4g" ) #1
            .getOrCreate(); #1
...
      List<Integer> l = new ArrayList<>( numberOfThrows );
       for ( int i = 0; i < numberOfThrows ; i ++) {
         l .add( i );
      }
      Dataset<Row> incrementalDf = spark #2
            .createDataset( l , Encoders.INT()) #2
            .toDF(); #2
...
      Dataset<Integer> dartsDs = incrementalDf #3
            .map( new DartMapper(), Encoders.INT()); #3
...
       int dartsInCircle = dartsDs .reduce( new DartReducer()); #4
...
      System. out .println( "Pi is roughly " +
➥ 4.0 * dartsInCircle / numberOfThrows );
 
       spark .stop();
    }
 }