统计人口性别和身高

自动生成实验数据:

自动生成1000个样例数据

from pyspark import SparkContext,SparkConf
import random


def getRandomGender():
rand = random.randint(0,2)+1
if rand % 2 ==0:
return "M"
else:
return "F"

sc = SparkContext('local','createDataFile')
outputFile = "file:///usr/local/spark/mycode/exercise/peopleage1.txt"
Arrays=[]
for i in range(0,1000):
height = random.randint(0,230)
if height < 50:
height = height +50
gender = getRandomGender()
if (height <100 and gender =="M"):
height= height+100
if (height <100 and gender =="F"):
height = height+50
Arrays.append(str(i) +" "+gender+" "+str(height))

rdd = sc.parallelize(Arrays)
rdd.saveAsTextFile(outputFile)
 
 

 

 
统计人口与身高的代码:
from pyspark import SparkContext

sc = SparkContext('local','CountPeopleData')
#读入数据
RDD = sc.textFile("file:///usr/local/spark/mycode/exercise/people.txt")
#按空格切割,并且通过filter过滤,得到全是M的新的RDD,然后wordcount
totalMen = RDD.flatMap(lambda line : line.split(" ")).filter(lambda a : a == "M").map(lambda a : (a,1)).reduceByKey(lambda a,b : (a+b))
totalWomen = RDD.flatMap(lambda line: line.split(" ")).filter( lambda a: a =="F").map(lambda a: (a,1)).reduceByKey(lambda a,b :(a+b))
 
#保留性别与身高部分
totaPeopleHeightinfo = RDD.map(lambda line : (line.split(" ")[1] + " "+line.split(" ")[2]))
 
#过滤,留下全是M 的数据,然后从小到大排序,取第一个,为最小
totalMenLowHieghtinfo = totaPeopleHeightinfo.filter(lambda a : a[0] == "M").map(lambda a: (a,1)).sortByKey().first()
#过滤,留下全是F的数据,然后设置ascending=False,大到小排序,取第一个,为最大
totalMenBestHieghtinfo = totaPeopleHeightinfo.filter(lambda a : a[0] == "M").map(lambda a : (a,1)).sortByKey(ascending=False).first()
#过滤,留下全是M 的数据,然后从小到大排序,取第一个,为最小
totalWoMenLowHieghtinfo = totaPeopleHeightinfo.filter(lambda a : a[0] == "F").map(lambda a: (a,1)).sortByKey().first()
#过滤,留下全是F的数据,然后设置ascending=False,大到小排序,取第一个,为最大
totalWoMenBestHieghtinfo = totaPeopleHeightinfo.filter(lambda a : a[0] == "F").map(lambda a : (a,1)).sortByKey(ascending=False).first()

print("男生总数:",totalMen.collect())
print("女生总数:",totalWomen.collect())
print("男生最低身高为:",totalMenLowHieghtinfo)
print("男生最高身高为:",totalMenBestHieghtinfo)
print("女生最低身高为:",totalWoMenLowHieghtinfo)
print("女生最高身高为:",totalWoMenBestHieghtinfo)

 

 
 

转载于:https://www.cnblogs.com/SoftwareBuilding/p/9467161.html

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值