> For the complete documentation index, see [llms.txt](https://george-jen.gitbook.io/data-science-and-apache-spark/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://george-jen.gitbook.io/data-science-and-apache-spark/run-a-program-to-estimate-pi.md).

# Run a program to estimate pi

### Run a program to estimate PI, which is 3.14592…

First Scala code with Apache Spark using Eclipse IDE

Start Eclipse Scala Plug-ins, click New->Scala Project

![](/files/-M1fdrUA-KKLYwQvZQKN)

Enter Spark, click Finish

![](/files/-M1feqsneI1maqydde4J)

Right click Project name spark you just created, click New->Package

![](/files/-M1fey5SUUWoyj8WQMY6)

Enter the name of Package: using reverse domain name, like below, click Finish

![](/files/-M1ff6Yw1T50qRWZ1VaV)

Right mouse click Package name, select New->Scala Object, enter reverse domain name+name of object which is pi in this instance, click Finish

![](/files/-M1ffEDWsV1Z7Uk3IXP3)

Copy below code after the package line, in my example, “package com.jentekco.spark”

```
import scala.math.random
import org.apache.spark._
import org.apache.log4j._
import org.apache.spark.sql.SparkSession
object pi {
    def main(args: Array[String]): Unit = {
    Logger.getLogger("org").setLevel(Level.ERROR)
    val spark = SparkSession
      .builder
      .master("local")
      .appName("Spark Pi")
      .getOrCreate()
    val slices = if (args.length > 0) args(0).toInt else 2
    val n = math.min(100000L * slices, Int.MaxValue).toInt // avoid overflow
    val count = spark.sparkContext.parallelize(1 until n, slices).map { i =>
      val x = random * 2 - 1
      val y = random * 2 - 1
      if (x*x + y*y <= 1) 1 else 0
    }.reduce(_ + _)
    println(s"Pi is roughly ${4.0 * count / (n - 1)}")
    spark.stop()
  } }
```

![](/files/-M1fg5IQ-D14UUuTzWFN)

To fix it, right click project spark, choose Properties,

![](/files/-M1fgDebIhPKVtx67Cf4)

Choose Java Build Path->Add External JARS

![](/files/-M1fgOhPckoODLA97J51)

Navigate to the SPARK Home folder, jars sub folder, select all jar files, then click Open

![](/files/-M1fgUk60v8mwSVCKYuT)

Then the errors go away, Scala code is compiled successfully.

![](/files/-M1fgcwFehe1YVOgyLE6)

To run the Scala pi program, click Run->Run Configuration.&#x20;

![](/files/-M76IV9lQ1_QIP3w2g4-)

Then click Java Application (Scala is Java at the end of day)

In the Name field, enter pi

![](/files/-M1fgiVpPMZefZoNT_Sa)

in the Project field, enter Spark, in the Main class field, in my example, I enter com.jentekco.spark.pi, then click Run

![](/files/-M1fh1TNMeYaHE4xH5vk)

See output in the Console: “Pi is roughly 3.144195720978605”

Now that you have done your development and test of the scala project, you need to make a jar file to be deployed to the production. Right mouse click on spark, select Export

![](/files/-M1fhAFGCKyoX19ioYtH)

Choose JAR file and click Next

![](/files/-M1fhFEQ-GP9AM6p_TqS)

Under the resource to export, select the right project, in this case, spark

![](/files/-M1fhMl-KHbn8roRkakz)

Choose proper folder to export pi.jar using Browse button, click Finish when done

You can run it on your PC which already has spark home, using spark-submit:

```
%SPARK_HOME%\bin\spark-submit --class com.jentekco.spark.pi pi.jar
```

Pi is roughly 3.14279571397857
