大数据入门到精通5--spark 的 RDD 的 reduce方法使用
培训系列5--spark 的 RDD 的 reduce方法使用
1.spark-shell环境下准备数据
val collegesRdd= sc.textFile("/user/hdfs/CollegeNavigator.csv")
val header= collegesRdd.first
val headerlessRdd= collegesRdd.filter( line=>{ line!= header } )
2.准备学生数的map
val countStuMap= headerlessRdd.map(line=>{
val strCount=line.split("\",\"")(7)
if (strCount.length()>0) strCount.toInt
else 0
})
countStuMap.take(10).foreach(println)
在map函数里面增加if else语句主要是数据中“”的空字符串,如果直接转换int会报错
3.写r求学生总数的reduce rdd
val totalcount=countStuMap.reduce((stuCount1,stuCount2)=>stuCount1+stuCount2)
得到所有学校的学生综述
3.写求学校类型的总数
scala> header
res12: String = "Name","Address","Website","Type","Awards offered","Campus setting","Campus housing","Student population","Undergraduate students","Graduation Rate","Transfer-Out Rate","Cohort Year *","Net Price **","Largest Program","IPEDS ID","OPE ID"
scala> val typeMap= headerlessRdd.map(line=>{
| val strtype=line.split("\",\"")(3)
| strtype
| })
typeMap: org.apache.spark.rdd.RDD[String] = MapPartitionsRDD[13] at map at <console>:30
scala> typeMap.count
res13: Long = 503
scala> typeMap.distinct.count
res14: Long = 5
一个rdd中如果有重复的值,可以直接通过distinct来去重。
4.求平均学校学生人数
求学校总数,可以通过headerlessRdd.count来获得,也可以用map reduce来做,map和reduce可以连写,
val collegeCount=headerlessRdd.map(line=>1).reduce((line1,line2)=>line1+line2)
totalcount/collegeCount
这里的数据量比表少,如果数据量比较多,会发发现平均值不对。
主要是由于计算totalcount的时候使用了int类型,int类型的最大值是有限的,实际计算中要把toInt 最好换成 toLong
if (strCount.length()>0) strCount.toLong
else 0
大数据入门到精通5--spark 的 RDD 的 reduce方法使用的更多相关文章
- 大数据学习(26)—— Spark之RDD
做大数据一定要有一个概念,需要处理的数据量非常大,少则几十T,多则上百P,全部放内存是不可能的,会OOM,必须要用迭代器一条一条处理. RDD叫做弹性分布式数据集,是早期Spark最核心的概念,是一种 ...
- 大数据入门到精通2--spark rdd 获得数据的三种方法
通过hdfs或者spark用户登录操作系统,执行spark-shell spark-shell 也可以带参数,这样就覆盖了默认得参数 spark-shell --master yarn --num-e ...
- 大数据入门到精通18--sqoop 导入关系库到hdfs中和hive表中
一,选择数据库,这里使用标准mysql sakila数据库 mysql -u root -D sakila -p 二.首先尝试把表中的数据导入到hdfs文件中,这样后续就可以使用spark来dataf ...
- 大数据入门到精通13--为后续和MySQL数据库准备
We will be using the sakila database extensively inside the rest of the course and it would be great ...
- 大数据入门到精通12--spark dataframe 注册成hive 的临时表
一.获得最初的数据并形成dataframe val ny= sc.textFile("data/new_york/")val header=ny.firstval filterNY ...
- 大数据入门到精通11-spark dataframe 基础操作
// dataframe is the topic 一.获得基础数据.先通过rdd的方式获得数据 val ny= sc.textFile("data/new_york/")val ...
- 大数据入门到精通8-spark RDD 复合key 和复合value 的map reduce操作
一.做基础数据准备 这次使用fights得数据. scala> val flights= sc.textFile("/user/hdfs/data/Flights/flights.cs ...
- 大数据入门到精通4--spark的rdd的map使用方式
学习了之前的rdd的filter以后,这次来讲spark的map方式 1.获得文件 val collegesRdd= sc.textFile("/user/hdfs/CollegeNavig ...
- 大数据入门到精通1--大数据环境下的基础文件HDFS 操作
1.使用hdfs用户或者hadoop用户登录 2.在linux shell下执行命令 hadoop fs -put '本地文件名' hadoop fs - put '/home/hdfs/sample ...
随机推荐
- PathUtil
public String getParentPath(final String originalPath) { boolean isSplitRequired = true; int lastSla ...
- PHP读取txt文件到数组
$file_path = "test.txt"; if(file_exists($file_path)){ $file_arr = file($file_path); for($i ...
- python 生成器 的send
>>> def f(): c=yield 5 print c d=yield c+5 print d >>> b=f() >>> b.send(N ...
- 使用STM32CubeMX生成RTC工程[秒中断]
现在我们在之前的工程(http://www.cnblogs.com/libra13179/p/7170791.html)中修改 /** ******************************** ...
- linux中根据进程的PID值来查找执行文件的及其路径
lsof -p PID http://blog.csdn.net/great_smile/article/details/50114133
- sizeof 空类
C++标准规定类的大小不为0,空类的大小为1,当类不包含虚函数和非静态数据成员时,其对象大小也为1. 如果在类中声明了虚函数(不管是1个还是多个),那么在实例化对象时,编译器会自动在对象里安插一个指针 ...
- zookeeper windows7下集群搭建
模拟分布式环境!!! 搞了好几天,各种错误!!终于成功了. 环境: windows7 /centos/xsheel 安装了三个虚拟机... 1.下载zookeeper http://archi ...
- Delphi XE8 中TIdTCPClient的WriteLn编码变化!
//XE2中正常的代码: IdTCPClient1.IOHandler.WriteLn(sendData,TEncoding.ANSI); //XE8中不行了,解决方法: //,引入“IdGlobal ...
- 1.Zabbix配置[仅环境搭建]
Zabbix配置分为两部分1.Zabbix服务端的配置 安装Zabbix源: [root@c71 ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabb ...
- C# 容器重用避免GC 的论证
var lst = new List<int>(); lst.Capacity = ; var cnt = lst.Count; var cp = lst.Capacity; ; i< ...