@@ -0,0 +1,1801 @@
+ *%0A *    http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0Apackage org.apache.spark.examples;%0A%0Aimport org.apache.spark.SparkConf;%0Aimport org.apache.spark.api.java.JavaRDD;%0Aimport org.apache.spark.api.java.JavaSparkContext;%0Aimport org.apache.spark.api.java.function.Function;%0Aimport org.apache.spark.api.java.function.Function2;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A/**%0A * Computes an approximation to pi%0A * Usage: JavaSparkPi %5Bslices%5D%0A */%0Apublic final class JavaSparkPi %7B%0A%0A  public static void main(String%5B%5D args) throws Exception %7B%0A    SparkConf sparkConf = new SparkConf().setAppName(%22JavaSparkPi%22);%0A    JavaSparkContext jsc = new JavaSparkContext(sparkConf);%0A%0A    int slices = (args.length == 1) ? Integer.parseInt(args%5B0%5D) : 2;%0A    int n = 100000 * slices;%0A    List%3CInteger%3E l = new ArrayList%3CInteger%3E(n);%0A    for (int i = 0; i %3C n; i++) %7B%0A      l.add(i);%0A    %7D%0A%0A    JavaRDD%3CInteger%3E dataSet = jsc.parallelize(l, slices);%0A%0A    int count = dataSet.map(new Function%3CInteger, Integer%3E() %7B%0A      @Override%0A      public Integer call(Integer integer) %7B%0A        double x = Math.random() * 2 - 1;%0A        double y = Math.random() * 2 - 1;%0A        return (x * x + y * y %3C 1) ? 1 : 0;%0A      %7D%0A    %7D).reduce(new Function2%3CInteger, Integer, Integer%3E() %7B%0A      @Override%0A      public Integer call(Integer integer, Integer integer2) %7B%0A        return integer + integer2;%0A      %7D%0A    %7D);%0A%0A    System.out.println(%22Pi is rouuuughly %22 + 4.0 * count / n);%0A%0A    jsc.stop();%0A  %7D%0A%7D%0A

Create JavaSparkPi.java