PySpark学习笔记3-案例练习

搜索引擎日志分析

需求1 用户搜索关键词分析

需求2 用户和关键词组合分析

需求3 热门搜索时间段分析

首先对于数据可以用jieba库进行分词处理

python 复制代码
from pyspark import SparkConf,SparkContext
from defs import context_jieba,filter_words,append_words
from oprator import add
from pyspark.storagelevel import storagelevel

if __name__ == '__main__':
conf = SparkConf().setAppName('sougou').setMaster("local[*]")
#读取文件
sc = SparkContext('../sougou.txt')
split_rdd = file_rdd.map(lambda line:line.split('\t'))
#送入缓存
split_rdd.persist(StorageLevel.DISK_ONLY)
#需求1
context_rdd = split_rdd.map(lambda x : x[2])
# 对取出来的第二列搜索内容做分词操作
word_rdd = context_rdd.flatMap(context_jieba)
filter_rdd = word_rdd.filter(filter_words)
final_words_rdd = filter_rdd.amp(append_words)
#统计关键词的个数
result1 = final_words_rdd.reduceByKey(lambda a,b: a + b).sortBy(lambda x : x[1],ascending=False,numPartitions=1).take(5)
print('需求一结果',result1)
#需求2
user_content_rdd = split_rdd.map(lambda x:(x[1],x[2]))
user_word_rdd = uder_content_tdd.flatMap(extract_user_and_word)
user_word_with_one_rddd = user_word_rdd.map(lambda x:(x:1))
result2 = user_word_with_one_rddd.reduceByKey(lambda a,b:a+b).sortBy(lambda x:x[1],ascending=False,numpPartitions=1).take(5)
print(result2)
#需求3
time_rdd = split_rdd.map(lambda x:x[0])
hour_with_one_rdd  = time_rdd.map(lambda x:(x.split(":")[0],1))
result3 = hpour_with_one_rdd.reduceByKey(add).sortBy(lambda x:x[1],ascending=False,numPartitions=1).collect()
print(result3)

将代码提交到集群中运行

powershell 复制代码
/spark/bin/spark-submit --master yarn --py-files def.py /root/main.py
相关推荐
电子云与长程纠缠6 小时前
Godot学习05 - 播放与分离FBX动画
学习·游戏引擎·godot
蒸蒸yyyyzwd6 小时前
day3学习笔记
笔记·学习
red_redemption7 小时前
自由学习记录(143)
学习
楼田莉子8 小时前
MySQL数据库:MySQL的数据类型
数据库·学习·mysql
小陈phd9 小时前
系统架构师学习笔记(三)——计算机体系结构之存储系统
笔记·学习·系统架构
Rsun0455110 小时前
AI智能体学习路线
人工智能·学习
feasibility.10 小时前
SSH Agent Forwarding 与 tmux 排障笔记
linux·运维·服务器·经验分享·笔记·ssh
charlie11451419110 小时前
通用GUI编程技术——Win32 原生编程实战(十六)——Visual Studio 资源编辑器使用指南
开发语言·c++·ide·学习·gui·visual studio·win32
知识分享小能手10 小时前
MongoDB入门学习教程,从入门到精通,MongoDB查询(4)
数据库·学习·mongodb
weixin_4093831210 小时前
godot等轴视角tilemaplayer的学习 isocheric的素材xy大小怎么调
学习·游戏引擎·godot