Spark- Transformation实战
RDD的算子分为两类,是 Trans formation(Lazy),一类是 Action(触发任务执行
RDD不存在真正要计算的数据,而是记录了RDD的转换关系(调用了什么方法,传入什么函数)
RDD的 Trans formation的特点
1. lazy
2.生成新的RDD
package cn.rzlee.spark.core import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext} object TransformationOperation {
def main(args: Array[String]): Unit = { //map()
//filter()
//flatMap()
// groupByKey()
//reduceByKey()
//sortByKey()
join()
} // 将集合中每个元素乘以2
def map(){
val conf = new SparkConf().setAppName(this.getClass.getSimpleName).setMaster("local[1]")
val sc = new SparkContext(conf) val numbers = Array(1,2,3,4,5)
val numberRDD: RDD[Int] = sc.parallelize(numbers,1)
numberRDD.foreach(num=>println(num)) } // 过滤出集合中的偶数
def filter(): Unit ={
val conf = new SparkConf().setAppName(this.getClass.getSimpleName).setMaster("local[1]")
val sc = new SparkContext(conf) val numbers = Array(1,2,3,4,5)
val numberRDD: RDD[Int] = sc.parallelize(numbers,1)
val evenNumbersRdd = numberRDD.filter(num=>num%2==0)
evenNumbersRdd.foreach(num=>println(num))
} // 将行拆分为单词
def flatMap(): Unit ={
val conf = new SparkConf().setAppName(this.getClass.getSimpleName).setMaster("local[1]")
val sc = new SparkContext(conf) val lineArray = Array("hello you", "just do it", "go go go")
val lines = sc.parallelize(lineArray, 1)
val words: RDD[String] = lines.flatMap(line=>line.split(" "))
words.foreach(word=>println(word))
} // 将每个班级的成绩进行分组
def groupByKey(): Unit ={
val conf = new SparkConf().setAppName(this.getClass.getSimpleName).setMaster("local[1]")
val sc = new SparkContext(conf)
val scoresList = Array(Tuple2("class1", 50), Tuple2("class1", 95), Tuple2("class2", 60), Tuple2("class2", 88))
val scores: RDD[(String, Int)] = sc.parallelize(scoresList, 1)
val groupedScoreds = scores.groupByKey()
groupedScoreds.foreach(scored=>{
println(scored._1)
scored._2.foreach(singleScore=>println(singleScore))
println("=====================================")
})
} // 统计每个班级的总分
def reduceByKey(): Unit ={
val conf = new SparkConf().setAppName(this.getClass.getSimpleName).setMaster("local[1]")
val sc = new SparkContext(conf) val scoresList = Array(Tuple2("class1", 50), Tuple2("class1", 95), Tuple2("class2", 60), Tuple2("class2", 88))
val scores: RDD[(String, Int)] = sc.parallelize(scoresList, 1)
val totalScores: RDD[(String, Int)] = scores.reduceByKey(_+_)
totalScores.foreach(totalScore=>println(totalScore._1 +" : " + totalScore._2)) } //将学生分数进行排序
def sortByKey(): Unit ={
val conf = new SparkConf().setAppName(this.getClass.getSimpleName).setMaster("local[1]")
val sc = new SparkContext(conf)
val scoreList = Array(Tuple2(90,"leo"), Tuple2(99, "kent"), Tuple2(80,"Jeo"), Tuple2(91,"Ben"), Tuple2(96,"Sam"))
val scores: RDD[( Int,String)] = sc.parallelize(scoreList, 1)
val sortedScores = scores.sortByKey(false)
sortedScores.foreach(student=>println(student._2 +" : " + student._1))
} // 打印每个学生的成绩
def join(): Unit ={
val conf = new SparkConf().setAppName(this.getClass.getSimpleName).setMaster("local[1]")
val sc = new SparkContext(conf) val studentsList = Array(Tuple2(1,"leo"), Tuple2(2, "Sam"), Tuple2(3, "kevin"))
val scoresList = Array(Tuple2(1,60), Tuple2(2,70), Tuple2(3,80)) val students: RDD[(Int, String)] = sc.parallelize(studentsList,1)
val scores: RDD[(Int, Int)] = sc.parallelize(scoresList,1)
val studentScores: RDD[(Int, (String, Int))] = students.join(scores)
studentScores.foreach(studentScore=>{
println("studentid: "+studentScore._1)
println("studentNmae:"+studentScore._2._1)
println("studentScore: "+ studentScore._2._2)
println("###################################################")
})
}
// 打印每个学生的成绩
// cogroup相当于full join
def cogroup(): Unit ={
val conf = new SparkConf().setAppName(this.getClass.getSimpleName).setMaster("local[1]")
val sc = new SparkContext(conf) val studentsList = Array(Tuple2(1,"leo"), Tuple2(2, "Sam"), Tuple2(3, "kevin"))
val scoresList = Array(Tuple2(1,60), Tuple2(2,70), Tuple2(3,80)) val students: RDD[(Int, String)] = sc.parallelize(studentsList,1)
val scores: RDD[(Int, Int)] = sc.parallelize(scoresList,1) val studentScores: RDD[(Int, (Iterable[String], Iterable[Int]))] = students.cogroup(scores)
studentScores.foreach(studentScore =>{
println("studentid: " + studentScore._1)
println("studentname: "+ studentScore._2._1)
println("studentscore: "+ studentScore._2._2) })
}
#union求并集,注意类型要一致
val rdd6 = sc.parallelize(List(5,6,4,7))
val rdd7 = sc.parallelize(List(1,2,3,4))
val rdd8 = rdd6.union(rdd7)
rdd8.distinct.sortBy(x=>x).collect
#intersection求交集
val rdd9 = rdd6.intersection(rdd7)
#join(连接) 注意按照key相join
val rdd1 = sc.parallelize(List(("tom", 1), ("jerry", 2), ("kitty", 3)))
val rdd2 = sc.parallelize(List(("jerry", 9), ("tom", 8), ("shuke", 7), ("tom", 2)))
val rdd3 = rdd1.join(rdd2)
val rdd3 = rdd1.leftOuterJoin(rdd2)
val rdd3 = rdd1.rightOuterJoin(rdd2)
#cogroup 有点像全外连接
// cogroup
val rdd1 = sc.parallelize(List(("tom", 1), ("tom", 2), ("jerry", 3), ("kitty", 2)))
val rdd2 = sc.parallelize(List(("jerry", 2), ("tom", 1), ("shuke", 2)))
val rdd3 = rdd1.cogroup(rdd2)
println(rdd3.collect().toBuffer)
#cartesian笛卡尔积
val rdd1 = sc.parallelize(List("tom", "jerry"))
val rdd2 = sc.parallelize(List("tom", "kitty", "shuke"))
val rdd3 = rdd1.cartesian(rdd2)
Spark- Transformation实战的更多相关文章
- Spark入门实战系列--1.Spark及其生态圈简介
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .简介 1.1 Spark简介 年6月进入Apache成为孵化项目,8个月后成为Apache ...
- Spark入门实战系列--3.Spark编程模型(上)--编程模型及SparkShell实战
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Spark编程模型 1.1 术语定义 l应用程序(Application): 基于Spar ...
- Spark入门实战系列--7.Spark Streaming(上)--实时流计算Spark Streaming原理介绍
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Spark Streaming简介 1.1 概述 Spark Streaming 是Spa ...
- 【Todo】【读书笔记】大数据Spark企业级实战版 & Scala学习
下了这本<大数据Spark企业级实战版>, 另外还有一本<Spark大数据处理:技术.应用与性能优化(全)> 先看前一篇. 根据书里的前言里面,对于阅读顺序的建议.先看最后的S ...
- 《大数据Spark企业级实战 》
基本信息 作者: Spark亚太研究院 王家林 丛书名:决胜大数据时代Spark全系列书籍 出版社:电子工业出版社 ISBN:9787121247446 上架时间:2015-1-6 出版日期:20 ...
- 倾情大奉送--Spark入门实战系列
这一两年Spark技术很火,自己也凑热闹,反复的试验.研究,有痛苦万分也有欣喜若狂,抽空把这些整理成文章共享给大家.这个系列基本上围绕了Spark生态圈进行介绍,从Spark的简介.编译.部署,再到编 ...
- Spark入门实战系列--10.分布式内存文件系统Tachyon介绍及安装部署
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .Tachyon介绍 1.1 Tachyon简介 随着实时计算的需求日益增多,分布式内存计算 ...
- Spark入门实战系列--2.Spark编译与部署(上)--基础环境搭建
[注] 1.该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取: 2.Spark编译与部署将以CentOS 64位操作系统为基础,主要是考虑到实际应用 ...
- Spark入门实战系列--2.Spark编译与部署(中)--Hadoop编译安装
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .编译Hadooop 1.1 搭建环境 1.1.1 安装并设置maven 1. 下载mave ...
- Spark入门实战系列--2.Spark编译与部署(下)--Spark编译安装
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .编译Spark .时间不一样,SBT是白天编译,Maven是深夜进行的,获取依赖包速度不同 ...
随机推荐
- 实际项目中,看 ECharts 和 HighCharts 渲染性能对比,表面看衣装,本质看内功!!!
最近做项目,使用的是echarts显示图表数据,但是数据量比较多的时候,有卡顿的情况.后来同事拿echarts和HighCharts做了对比,仅供大家参考.同时感谢同事做的工作. 一.查询1天的源数据 ...
- IDEA中配置svn
反正这个命令行我是勾选了的,软件占用内存不大,我就都选了. 然后是配置IDEA 试一下效果,果然阔以,哈哈.
- ssh key 免密码登陆服务器,批量分发管理以及挂载远程目录的sshfs
ssh key 免密码登陆服务器,批量分发管理以及挂载远程目录的sshfs 第一部分:使用ssh key 实现服务器间的免密码交互登陆 步骤1: 安装openssh-clients [root@001 ...
- Linux高并发应用类型对系统内核的优化
Linux操作系统内核参数优化 net.ipv4.tcp_max_tw_buckets = net.ipv4.ip_local_port_range = net.ipv4.tcp_tw_recycle ...
- python学习 01 变量
1.变量不是‘盒子’. 1.1 不同的值,变量名没变, 变量地址也会变. 1.2 相同的值,不同的变量名,变量地址是相同的
- cf 251 B Playing with Permutations 暴力 分类讨论
题链;http://codeforces.com/problemset/problem/251/B B. Playing with Permutations time limit per test 2 ...
- Java Enum 比较用 == 还是 eques
我是把枚举当作常量来使用的,枚举中还有两个自己的属性,关注到这个地方的朋友对枚举已经有了认识,这里就不再编写枚举的demo了,这里我直接说结果吧,在枚举中使用==和equals比较效果是一样的,查看源 ...
- POJ 2031 Building a Space Station【经典最小生成树】
链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- A vectorized example
http://cs231n.stanford.edu/slides/2017/cs231n_2017_lecture4.pdf
- [php]Maximum function nesting level of '100' reached错误
今天在做后台一个模块的时候报出了这个错误. Maximum function nesting level of '100' reached 仔细分析之后发现是在类的初始化过程中(__construct ...