博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spark: rdd的应用(java api)
阅读量:2110 次
发布时间:2019-04-29

本文共 2866 字,大约阅读时间需要 9 分钟。

  1. wordcount: 统计词频, 排序 (jdk7, jdk8-lambda表达式)
  2. 历年温度最值: max, min, avg
org.apache.spark
spark-core_2.11
2.1.0

jdk8 : lambda表达式

idea中添加maven jdk8构建依赖

org.apache.maven.plugins
maven-compiler-plugin
8
8
public static void main(String[] args){        //conf        SparkConf conf = new SparkConf();        conf.setMaster("local");        conf.setAppName("wc");        //context        JavaSparkContext context = new JavaSparkContext(conf);        JavaRDD
rdd1 = context.textFile("/home/wang/txt/word.txt"); //lambda: 表达式 JavaRDD
rdd2 = rdd1.flatMap(s -> Arrays.asList(s.split(" ")).iterator()); JavaPairRDD
rdd3 = rdd2.mapToPair(s -> new Tuple2
(s, 1)); JavaPairRDD
rdd4 = rdd3.reduceByKey((x, y) -> x + y); //按单词升序 List
> list1 = rdd4.sortByKey(true).collect(); //按词频降序 JavaPairRDD
rdd5 = rdd4.mapToPair(x -> new Tuple2
(x._2, x._1)) .sortByKey() .mapToPair(x -> new Tuple2
(x._2, x._1)); List
> list2 = rdd5.collect(); }

jdk7 : spark api

public static void main(String[] args){        //conf, context........     同上         //1, a b c ===> split( ) : string[] {a,b,c}        JavaRDD
rdd2 = rdd1.flatMap(new FlatMapFunction
() { public Iterator
call(String s) throws Exception { String[] arr = s.split(" "); return Arrays.asList(arr).iterator(); } }); //2, string[] {a,b,c}==>(a,1),(b,1) JavaPairRDD
rdd3 = rdd2.mapToPair(new PairFunction
() { public Tuple2
call(String s) throws Exception { return new Tuple2
(s, 1); } }).filter(new Function
, Boolean>() {//过滤空字符 public Boolean call(Tuple2
v1) throws Exception { return v1._1.trim().length()>0 ; } }); //3, (a,1),(b,1) ==> reduceByKey: (a,4), (b,3) JavaPairRDD
rdd4 = rdd3.reduceByKey(new Function2
() { public Integer call(Integer v1, Integer v2) throws Exception { return v1+v2; } }); //4.1 排序( 字母生序) JavaPairRDD
rddRes = rdd4.sortByKey(); List
> list1 = rddRes.collect(); //4.2排序( 词频降序) JavaPairRDD
sortRdd1 = rdd4.mapToPair(new PairFunction
, Integer, String>() { public Tuple2
call(Tuple2
tup) throws Exception { return new Tuple2
(tup._2, tup._1); } }); JavaPairRDD
sortRdd2 = sortRdd1.sortByKey(false).mapToPair(new PairFunction
, String, Integer>() { public Tuple2
call(Tuple2
tup) throws Exception { return new Tuple2
(tup._2, tup._1); } }); List
> list2 = sortRdd2.collect(); }

转载地址:http://gpdef.baihongyu.com/

你可能感兴趣的文章
Leetcode C++ 《第203场周赛》
查看>>
云原生 第十三章 Kubernetes网络概念及策略控制
查看>>
《redis设计与实现》 第一部分:数据结构与对象 || 读书笔记
查看>>
《redis设计与实现》 第二部分(第9-11章):单机数据库的实现
查看>>
算法工程师 面经2019年5月
查看>>
搜索架构师 一面面经2019年6月
查看>>
稻草人手记
查看>>
第一次kaggle比赛 回顾篇
查看>>
leetcode 50. Pow(x, n)
查看>>
leetcode 130. Surrounded Regions
查看>>
【托业】【全真题库】TEST2-语法题
查看>>
博客文格式优化
查看>>
【托业】【新托业全真模拟】疑难语法题知识点总结(01~05)
查看>>
【SQL】group by 和order by 的区别。
查看>>
【Python】详解Python多线程Selenium跨浏览器测试
查看>>
Jmeter之参数化
查看>>
Shell 和Python的区别。
查看>>
Python 列表(list)、字典(dict)、字符串(string)常用基本操作小结
查看>>
Loadrunner之https协议录制回放报错如何解决?(九)
查看>>
python中xrange和range的异同
查看>>