Github user rmetzger commented on a diff in the pull request:
https://github.com/apache/incubator-flink/pull/78#discussion_r15434359
--- Diff:
flink-examples/flink-java-examples/src/main/java/org/apache/flink/example/java/pi/PiEstimation.java
---
@@ -0,0 +1,127 @@
+/**
+ * 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.example.java.pi;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.flink.api.java.DataSet;
+import org.apache.flink.api.java.ExecutionEnvironment;
+import org.apache.flink.api.java.functions.*;
+
+
+/**
+ * Estimates the value of Pi using the Monte Carlo method.
+ * The area of a circle is Pi * R^2, R being the radius of the circle
+ * The area of a square is 4 * R^2, where the length of the square's edge
is 2*R.
+ *
+ * Thus Pi = 4 * (area of circle / area of square).
+ *
+ * The idea is to find a way to estimate the circle to square area ratio.
+ * The Monte Carlo method suggests collecting random points (within the
square)
+ * ```
+ * x = Math.random() * 2 - 1
+ * y = Math.random() * 2 - 1
+ * ```
+ * then counting the number of points that fall within the circle
+ * ```
+ * x * x + y * y < 1
+ * ```
+ */
+public class PiEstimation {
+
+ static int n;
+
+ public static void main(String[] args) throws Exception {
+
+ int blocks = (args.length == 1) ? Integer.parseInt(args[0]) : 2;
+ n = 100000 * blocks;
+ List<Integer> l = new ArrayList<Integer>(n);
+ for (int i = 0; i < n; i++) {
+ l.add(1);
+ }
+
+ //Sets up the execution environment
+ final ExecutionEnvironment env =
ExecutionEnvironment.getExecutionEnvironment();
+ DataSet<Integer> dataSet = env.fromCollection(l);
+
+
+ DataSet<Double> count = dataSet
+ .filter(new PiFilter())
+ .setParallelism(blocks)
+ .reduce(new PiReducer())
+ .map(new PiMapper());
+
+ System.out.println("We estimate Pi to be:");
+ count.print();
+
+ env.execute();
+ }
+
+
+
//*************************************************************************
+ // USER FUNCTIONS
+
//*************************************************************************
+
+ // FilterFunction that filters out all Integers smaller than zero.
+
+ /**
+ * PiFilter randomly emits points that fall within a square of edge 2*x
= 2*y = 2.
+ * It calculates the distance to the center of a virtually centered
circle of radius x = y = 1
+ * If the distance is less than 1, then and only then does it return a
value (in this case 1, a list's value)
+ */
+ public static class PiFilter extends FilterFunction<Integer> {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public boolean filter(Integer value) throws Exception{
+ double x = Math.random() * 2 - 1;
+ double y = Math.random() * 2 - 1;
+ return (x * x + y * y) < 1;
+ }
+ }
+
+
+ /**
+ * PiReducer takes over the filter. It goes through the selected 1s and
returns the sum.
+ */
+ public static final class PiReducer extends ReduceFunction<Integer>{
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public Integer reduce(Integer value1, Integer value2) throws
Exception {
+ return value1 + value2;
+ }
+ }
+
+
+ /**
+ * The PiMapper's role is to apply one final operation on the count
thus returning the estimated Pi value.
+ */
+ public static final class PiMapper extends MapFunction<Integer,Double> {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public Double map(Integer intSum) throws Exception {
+ return intSum*4.0 / n;
--- End diff --
I think `n` will be 0 if you run the Job in a distributed environment on a
cluster.
You'll probably have to pass it either as a configuration value or via the
constructor of the class.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---