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
Enter Spark, click Finish
Right click Project name spark you just created, click New->Package
Enter the name of Package: using reverse domain name, like below, click Finish
Right mouse click Package name, select New->Scala Object, enter reverse domain name+name of object which is pi in this instance, click Finish
Copy below code after the package line, in my example, โ€œpackage com.jentekco.sparkโ€
1
import scala.math.random
2
import org.apache.spark._
3
import org.apache.log4j._
4
import org.apache.spark.sql.SparkSession
5
object pi {
6
def main(args: Array[String]): Unit = {
7
Logger.getLogger("org").setLevel(Level.ERROR)
8
val spark = SparkSession
9
.builder
10
.master("local")
11
.appName("Spark Pi")
12
.getOrCreate()
13
val slices = if (args.length > 0) args(0).toInt else 2
14
val n = math.min(100000L * slices, Int.MaxValue).toInt // avoid overflow
15
val count = spark.sparkContext.parallelize(1 until n, slices).map { i =>
16
val x = random * 2 - 1
17
val y = random * 2 - 1
18
if (x*x + y*y <= 1) 1 else 0
19
}.reduce(_ + _)
20
println(s"Pi is roughly ${4.0 * count / (n - 1)}")
21
spark.stop()
22
} }
Copied!
To fix it, right click project spark, choose Properties,
Choose Java Build Path->Add External JARS
Navigate to the SPARK Home folder, jars sub folder, select all jar files, then click Open
Then the errors go away, Scala code is compiled successfully.
To run the Scala pi program, click Run->Run Configuration.
Then click Java Application (Scala is Java at the end of day)
In the Name field, enter pi
in the Project field, enter Spark, in the Main class field, in my example, I enter com.jentekco.spark.pi, then click Run
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
Choose JAR file and click Next
Under the resource to export, select the right project, in this case, spark
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:
1
%SPARK_HOME%\bin\spark-submit --class com.jentekco.spark.pi pi.jar
Copied!
Pi is roughly 3.14279571397857
Last modified 1yr ago
Copy link