@@ -0,0 +1,2238 @@
+/**%0A * Illustrates Kryo serialization in Java%0A */%0Apackage com.oreilly.learningsparkexamples.java;%0A%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A%0Aimport org.apache.commons.lang.StringUtils;%0A%0Aimport org.apache.spark.SparkConf;%0Aimport org.apache.spark.serializer.KryoRegistrator;%0Aimport org.apache.spark.api.java.JavaRDD;%0Aimport org.apache.spark.api.java.JavaSparkContext;%0Aimport org.apache.spark.api.java.function.Function2;%0A%0Aimport com.esotericsoftware.kryo.Kryo;%0A%0Apublic class BasicAvgWithKryo %7B%0A  // This is our custom class we will configure Kyro to serialize%0A  class AvgCount %7B%0A    public AvgCount(int total, int num) %7B%0A      total_ = total;%0A      num_ = num;%0A    %7D%0A    public int total_;%0A    public int num_;%0A    public float avg() %7B%0A      return total_ / (float) num_;%0A    %7D%0A  %7D%0A%0A  public class AvgRegistrator implements KryoRegistrator %7B%0A    public void registerClasses(Kryo kryo) %7B%0A      kryo.register(AvgCount.class);%0A    %7D%0A  %7D%0A%0A  public static void main(String%5B%5D args) throws Exception %7B%0A%09%09String master;%0A%09%09if (args.length %3E 0) %7B%0A      master = args%5B0%5D;%0A%09%09%7D else %7B%0A%09%09%09master = %22local%22;%0A%09%09%7D%0A    BasicAvg avg = new BasicAvg();%0A    avg.run(master);%0A  %7D%0A%0A  public void run(String master) throws Exception %7B%0A    SparkConf conf = new SparkConf().setMaster(master).setAppName(%22basicavgwithkyro%22);%0A    conf.set(%22spark.serializer%22, %22org.apache.spark.serializer.KryoSerializer%22);%0A    conf.set(%22spark.kryo.registrator%22, AvgRegistrator.class.getName());%0A%09%09JavaSparkContext sc = new JavaSparkContext(conf);%0A    JavaRDD%3CInteger%3E rdd = sc.parallelize(Arrays.asList(1, 2, 3, 4));%0A    Function2%3CAvgCount, Integer, AvgCount%3E addAndCount = new Function2%3CAvgCount, Integer, AvgCount%3E() %7B%0A      @Override%0A      public AvgCount call(AvgCount a, Integer x) %7B%0A        a.total_ += x;%0A        a.num_ += 1;%0A        return a;%0A      %7D%0A    %7D;%0A    Function2%3CAvgCount, AvgCount, AvgCount%3E combine = new Function2%3CAvgCount, AvgCount, AvgCount%3E() %7B%0A      @Override%0A      public AvgCount call(AvgCount a, AvgCount b) %7B%0A        a.total_ += b.total_;%0A        a.num_ += b.num_;%0A        return a;%0A      %7D%0A    %7D;%0A    AvgCount initial = new AvgCount(0,0);%0A    AvgCount result = rdd.aggregate(initial, addAndCount, combine);%0A    System.out.println(result.avg());%0A%09%7D%0A%7D%0A

Add a basic avg with kryo example in Java