@@ -0,0 +1,1890 @@
+/**%0A * Illustrates a simple map partitions in Java to compute the average%0A */%0Apackage com.oreilly.learningsparkexamples.java;%0A%0Aimport java.util.Arrays;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Iterator;%0A%0Aimport org.apache.commons.lang.StringUtils;%0A%0Aimport org.eclipse.jetty.client.ContentExchange;%0Aimport org.eclipse.jetty.client.HttpClient;%0A%0A%0Aimport org.apache.spark.api.java.JavaRDD;%0Aimport org.apache.spark.api.java.JavaSparkContext;%0Aimport org.apache.spark.api.java.function.FlatMapFunction;%0Aimport org.apache.spark.api.java.function.Function2;%0A%0Apublic class BasicAvgMapPartitions %7B%0A  class AvgCount %7B%0A    public AvgCount(Integer total, Integer num) %7B%0A%09    total_ = total;%0A%09    num_ = num;%0A    %7D%0A    public AvgCount merge(Iterable%3CInteger%3E input) %7B%0A      for (Integer elem : input) %7B%0A        num_ += 1;%0A        total_ += elem;%0A      %7D%0A      return this;%0A    %7D%0A    public Integer total_;%0A    public Integer num_;%0A    public float avg() %7B%0A%09    return total_ / (float) num_;%0A    %7D%0A  %7D%0A%0A  public static void main(String%5B%5D args) throws Exception %7B%0A    String master;%0A    if (args.length %3E 0) %7B%0A      master = args%5B0%5D;%0A    %7D else %7B%0A      master = %22local%22;%0A    %7D%0A    JavaSparkContext sc = new JavaSparkContext(%0A      master, %22basicavgmappartitions%22, System.getenv(%22SPARK_HOME%22), System.getenv(%22JARS%22));%0A    JavaRDD%3CInteger%3E rdd = sc.parallelize(%0A      Arrays.asList(1, 2, 3, 4, 5));%0A    FlatMapFunction%3CIterator%3CInteger%3E, AvgCount%3E setup = new FlatMapFunction%3CIterator%3CInteger%3E, AvgCount%3E() %7B%0A    %7D;%0A    Function2%3CAvgCount, AvgCount, AvgCount%3E combine = new Function2%3CAvgCount, AvgCount, AvgCount%3E() %7B%0A%09    @Override%0A%09    public AvgCount call(AvgCount a, AvgCount b) %7B%0A        a.total_ += b.total_;%0A        a.num_ += b.num_;%0A        return a;%0A%09    %7D%0A    %7D;%0A%0A    AvgCount result = rdd.mapPartitions(setup).reduce(combine);%0A    System.out.println(result.avg());%0A  %7D%0A%7D%0A

add BasicAvgMapPartitions example to java