Spark Core实验三则(Ubuntu+pycharm+spark)

实验一:分析tomcat的访问日志,求访问量最高的两个网页

题目:

Tomcat的访问日志文件为localhost_access_log.2017-07-30,格式如下:

求出访问量最高的两个网页。要求输出以下格式:

网页名称、访问量

代码:

from pyspark import SparkConf, SparkContext
# 192.168.88.1 - - [30/Jul/2017:12:53:43 +0800] "GET /MyDemoWeb/ HTTP/1.1" 200 259
# 求出访问量最高的两个网页。要求输出以下格式:
# 网页名称、访问量
conf = SparkConf().setAppName("WordCount").setMaster("local")
sc = SparkContext(conf=conf)

inputFile = '/home/swy/input/localhost_access_log.2017-07-30.txt'
textFile = sc.textFile(inputFile)

wordCount = textFile.map(lambda line: line.split("MyDemoWeb/")[1])\
    .map(lambda line: line.split(" HTTP")[0]).map(lambda word: (word, 1))\
    .reduceByKey(lambda x, y: x + y).sortBy(lambda x: x[1], False).take(2)

print('访问量最高的两个网页是:\n{}'.format(wordCount))

运行结果:

实验二:使用Spark程序进行数据的清洗

题目:

       数据源userclicklogProblem文件记录了用户点击的日志记录,但日志中存在不合规范的数据。请用Spark程序进程数据清洗,完成以下操作:

  1. 过滤不满足6个字段的数据
  2. 过滤URL为空的数据

代码:

from pyspark import SparkConf, SparkContext

# 1,201.105.101.102,http://mystore.jsp/?productid=1,2017020020,1,1
# 2,201.105.101.103,http://mystore.jsp/?productid=2,2017020022,1,1
# 3,201.105.101.105,http://mystore.jsp/?productid=3,2017020023,1,2
# 4,201.105.101.107,http://mystore.jsp/?productid=1,2017020025,1,1
# 1,201.105.101.102,http://mystore.jsp/?productid=4,2017020021,3,1
# 1,201.105.101.102,http://mystore.jsp/?productid=1,2017020029,2,1
# 1,201.105.101.102, ,2017020029,2,1
# 1,201.105.101.102, ,2017020029,2
# (1)过滤不满足6个字段的数据
# (2)过滤URL为空的数据

conf = SparkConf().setAppName("WordCount").setMaster("local")
sc = SparkContext(conf=conf)

inputFile = '/home/swy/input/userclicklogProblem.txt'
textFile = sc.textFile(inputFile)

# wordCount = textFile.map(lambda line: line.split(","))\
#     .filter(lambda x: len(x) == 6).filter(lambda x: x[2] != " ")
# print('过滤后的数据如下:\n{}'.format(wordCount))

wordCount = textFile.map(lambda line: line.split(","))\
    .filter(lambda x: len(x) == 6).filter(lambda x:x[2]!=" ")
print("过滤后的数据如下:")
wordCount.foreach(print)

运行结果:

实验三:人口身高数据分析

题目:

       假设我们需要对某个省的人口 (1 亿) 性别还有身高进行统计,需要计算出男女人数,男性中的最高和最低身高,以及女性中的最高和最低身高。本案例中用到的源文件有以下格式,三列分别是 ID,性别,身高 (cm)。

代码:

from pyspark import SparkConf, SparkContext

# 假设我们需要对某个省的人口 (1 亿) 性别还有身高进行统计,需要计算出男女人数,男性中的最高和最低身高,
# 以及女性中的最高和最低身高。本案例中用到的源文件有以下格式,三列分别是 ID,性别,身高 (cm)。
# 1 M 103
# 2 F 215
# 3 M 169
# 4 F 190
# 5 F 132
conf = SparkConf().setAppName("WordCount").setMaster("local")
sc = SparkContext(conf=conf)

inputFile = '/home/swy/input/sample_people_info.txt'
textFile = sc.textFile(inputFile)
# 男人数
wordCount1 = textFile.map(lambda line: line.split(" ")).filter(lambda x: x[1] == 'M').count()
# 女人数
wordCount2 = textFile.map(lambda line: line.split(" ")).filter(lambda x: x[1] == 'F').count()
# 男人中最高身高
wordCount3 = \
textFile.map(lambda line: line.split(" ")).filter(lambda x: x[1] == 'M').sortBy(lambda x: x[2], False).take(1)[0][2]
# 男人中最矮身高
wordCount4 = \
textFile.map(lambda line: line.split(" ")).filter(lambda x: x[1] == 'M').sortBy(lambda x: x[2], True).take(1)[0][2]
# 女人中最高身高
wordCount5 = \
textFile.map(lambda line: line.split(" ")).filter(lambda x: x[1] == 'F').sortBy(lambda x: x[2], False).take(1)[0][2]
# 女人中最矮身高
wordCount6 = \
textFile.map(lambda line: line.split(" ")).filter(lambda x: x[1] == 'F').sortBy(lambda x: x[2], True).take(1)[0][2]
print('男人总数为:{}人'.format(wordCount1))
print('男人中最高身高为:{}cm'.format(wordCount3))
print('男人中最矮身高为:{}cm'.format(wordCount4))
print('女人总数为:{}人'.format(wordCount2))
print('女人中最高身高为:{}cm'.format(wordCount5))
print('女人中最矮身高为:{}cm'.format(wordCount6))

 运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惊弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值