You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by al...@apache.org on 2014/09/22 14:29:18 UTC

[36/60] git commit: Pi estimation example job in Scala

Pi estimation example job in Scala


Project: http://git-wip-us.apache.org/repos/asf/incubator-flink/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-flink/commit/299cef75
Tree: http://git-wip-us.apache.org/repos/asf/incubator-flink/tree/299cef75
Diff: http://git-wip-us.apache.org/repos/asf/incubator-flink/diff/299cef75

Branch: refs/heads/master
Commit: 299cef7586195f0a8ff26ab70a46cbf5e0f229db
Parents: 0dc7614
Author: Kostas Tzoumas <ko...@kostass-mbp.fritz.box>
Authored: Thu Sep 11 18:25:40 2014 +0200
Committer: Aljoscha Krettek <al...@gmail.com>
Committed: Mon Sep 22 09:59:58 2014 +0200

----------------------------------------------------------------------
 .../examples/scala/misc/PiEstimation.scala      | 53 ++++++++++++++++++++
 1 file changed, 53 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-flink/blob/299cef75/flink-examples/flink-scala-examples/src/main/scala/org/apache/flink/examples/scala/misc/PiEstimation.scala
----------------------------------------------------------------------
diff --git a/flink-examples/flink-scala-examples/src/main/scala/org/apache/flink/examples/scala/misc/PiEstimation.scala b/flink-examples/flink-scala-examples/src/main/scala/org/apache/flink/examples/scala/misc/PiEstimation.scala
new file mode 100644
index 0000000..d702f61
--- /dev/null
+++ b/flink-examples/flink-scala-examples/src/main/scala/org/apache/flink/examples/scala/misc/PiEstimation.scala
@@ -0,0 +1,53 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.examples.scala.misc
+
+import org.apache.flink.api.scala._
+
+object PiEstimation {
+
+  def main(args: Array[String]) {
+
+    val numSamples: Long = if (args.length > 0) args(0).toLong else 1000000
+
+    val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment
+
+    // count how many of the samples would randomly fall into
+    // the unit circle
+    val count =
+      env.generateSequence(1, numSamples)
+      .map (sample => {
+        val x = Math.random()
+        val y = Math.random()
+        if (x * x + y * y < 1) 1L else 0L
+      })
+      .reduce(_+_)
+
+    // the ratio of the unit circle surface to 4 times the unit square is pi
+    val pi = count
+      .map (_ * 4.0 / numSamples)
+
+    println("We estimate Pi to be:")
+
+    pi.print()
+
+    env.execute("PiEstimation example")
+  }
+
+}