<spark>JavaAggregate,自定义数据处理
来源:互联网 发布:苹果mac系统更新 编辑:程序博客网 时间:2024/04/29 17:38
import org.apache.kafka.common.metrics.stats.Avg;import org.apache.spark.SparkConf;import org.apache.spark.api.java.JavaRDD;import org.apache.spark.api.java.JavaSparkContext;import org.apache.spark.api.java.function.Function2;import java.io.Serializable;import java.util.Arrays;/** * 求平均值 * Created by hadoop on 17-2-23. */public class JavaAggregate { public static void main(String[] args) throws Exception { SparkConf conf = new SparkConf().setAppName("Aggregate"); JavaSparkContext sc = new JavaSparkContext(conf); JavaRDD<Integer> rdd = sc.parallelize(Arrays.asList(1,2,3,4,5,6)); class AvgCount implements Serializable { public int total; public int num; public AvgCount(int total, int num) { this.total = total; this.num = num; } public double avg() { return total / (double) num; } } //增加数据 Function2<AvgCount, Integer, AvgCount> addAndCount = new Function2<AvgCount, Integer, AvgCount>() { @Override public AvgCount call(AvgCount avgCount, Integer integer) throws Exception { avgCount.total += integer; avgCount.num += 1; return avgCount; } }; //将数据加在一起 Function2<AvgCount, AvgCount, AvgCount> combine = new Function2<AvgCount, AvgCount, AvgCount>() { @Override public AvgCount call(AvgCount avgCount, AvgCount avgCount2) throws Exception { avgCount.total += avgCount2.total; avgCount.num += avgCount2.num; return avgCount; } }; //需要传入一个单位元用于运算的初始化 AvgCount initial = new AvgCount(0, 0); AvgCount result = rdd.aggregate(initial, addAndCount, combine); System.out.println(result.avg()); }}
0 0
- <spark>JavaAggregate,自定义数据处理
- spark流数据处理
- Spark 大数据处理技术
- 【spark 大数据处理技术】
- spark scala 数据处理程序
- spark流数据处理:Spark Streaming的使用
- spark流数据处理:Spark Streaming的使用
- spark数据处理示例一:分类
- spark数据处理示例一:分类
- Spark SQL结构化数据处理
- 关于spark中dataframe数据处理
- 开源数据处理框架Spark
- storm、hadoop、spark大数据处理工具
- Spark Streaming:大规模流式数据处理
- spark大数据处理技术--阅读笔记
- 用Apache Spark进行大数据处理
- 【Spark大数据处理】动手写WordCount
- 【大数据处理架构】1.spark streaming
- 两种方法在github中做在线Demo演示
- 如何在个人主页里插入音乐
- tensorflow 对Recursive NN的完美实现
- PL/SQL标准建表规范
- hdu 2546 饭卡 dp
- <spark>JavaAggregate,自定义数据处理
- 希尔排序
- oracle 常用知识点整理
- 拍照相册和裁剪保存图片集合
- JDBC
- POJ 2955 Brackets (区间DP入门)
- iOS 用XIB约束后,用代码修改约束
- 算法提高 01背包 hdu2602 Bone Collector
- (转载)SpringMVC注解@RequestParam全面解析