SparkCore案例
PySpark實現(xiàn)SouGou統(tǒng)計分析
jieba分詞:
pip install jieba 從哪里下載pypi
三種分詞模式
精確模式,試圖將句子最精確地切開,適合文本分析;默認的方式
全模式,把句子中所有的可以成詞的詞語都掃描出來, 速度非??欤遣荒芙鉀Q歧義;
搜索引擎模式,在精確模式的基礎上,對長詞再次切分,提高召回率,適合用于搜索引擎分詞。
# -*- coding: utf-8 -*- # Program function:測試結巴分詞 import jieba import re # jieba.cut # 方法接受四個輸入參數(shù): # 需要分詞的字符串; # cut_all 參數(shù)用來控制是否采用全模式; # HMM 參數(shù)用來控制是否使用 HMM 模型; # use_paddle 參數(shù)用來控制是否使用paddle模式下的分詞模式,paddle模式采用延遲加載方式,通過enable_paddle接口安裝paddlepaddle-tiny,并且import相關代碼; str = "我來到北京清華大學" print(list(jieba.cut(str))) # ['我', '來到', '北京', '清華大學'],默認的是精確模式 print(list(jieba.cut(str, cut_all=True))) # ['我', '來到', '北京', '清華', '清華大學', '華大', '大學'] 完全模式 # 準備的測試數(shù)據 str1 = "00:00:00 2982199073774412 [360安全衛(wèi)士] 8 3 download.it.com.cn/softweb/software/firewall/antivirus/20067/17938.html" print(re.split("\s+", str1)[2]) # [360安全衛(wèi)士] print(re.sub("\[|\]", "", re.split("\s+", str1)[2])) #360安全衛(wèi)士 print(list(jieba.cut(re.sub("\[|\]", "", re.split("\s+", str1)[2])))) # [360安全衛(wèi)士] --->['360', '安全衛(wèi)士']
![]()
數(shù)據認知:數(shù)據集來自于搜狗實驗室,日志數(shù)據
日志庫設計為包括約1個月(2008年6月)Sogou搜索引擎部分網頁查詢需求及用戶點擊情況的網頁查詢日志數(shù)據集合。
![]()
![]()
需求
1-首先需要將數(shù)據讀取處理,形成結構化字段進行相關的分析
2-如何對搜索詞進行分詞,使用jieba或hanlp
jieba是中文分詞最好用的工具
![]()
步驟
1-讀取數(shù)據
2-完成需求1:搜狗關鍵詞統(tǒng)計
3-完成需求2:用戶搜索點擊統(tǒng)計
4-完成需求3:搜索時間段統(tǒng)計
5-停止sparkcontext
代碼
# -*- coding: utf-8 -*- # Program function:搜狗分詞之后的統(tǒng)計 ''' * 1-讀取數(shù)據 * 2-完成需求1:搜狗關鍵詞統(tǒng)計 * 3-完成需求2:用戶搜索點擊統(tǒng)計 * 4-完成需求3:搜索時間段統(tǒng)計 * 5-停止sparkcontext ''' from pyspark import SparkConf, SparkContext import re import jieba if __name__ == '__main__': # 準備環(huán)境變量 conf = SparkConf().setAppName("sougou").setMaster("local[*]") sc = SparkContext.getOrCreate(conf=conf) sc.setLogLevel("WARN") # TODO*1 - 讀取數(shù)據 sougouFileRDD = sc.textFile("/export/data/pyspark_workspace/PySpark-SparkCore_3.1.2/data/sougou/SogouQ.reduced") # print("sougou count is:", sougouFileRDD.count())#sougou count is: 1724264 # 00:00:00 2982199073774412 [360安全衛(wèi)士] 8 3 download.it.com.cn/softweb/software/firewall/antivirus/20067/17938.html resultRDD=sougouFileRDD \ .filter(lambda line:(len(line.strip())>0) and (len(re.split("\s+",line.strip()))==6))\ .map(lambda line:( re.split("\s+", line)[0], re.split("\s+", line)[1], re.sub("\[|\]", "", re.split("\s+", line)[2]), re.split("\s+", line)[3], re.split("\s+", line)[4], re.split("\s+", line)[5] )) # print(resultRDD.take(2)) #('00:00:00', '2982199073774412', '360安全衛(wèi)士', '8', '3', 'download.it.com.cn/softweb/software/firewall/antivirus/20067/17938.html') #('00:00:00', '07594220010824798', '哄搶救災物資', '1', '1', 'news.21cn.com/social/daqian/2008/05/29/4777194_1.shtml') # TODO*2 - 完成需求1:搜狗關鍵詞統(tǒng)計 print("=============完成需求1:搜狗關鍵詞統(tǒng)計==================") recordRDD = resultRDD.flatMap(lambda record: jieba.cut(record[2])) # print(recordRDD.take(5)) sougouResult1=recordRDD\ .map(lambda word:(word,1))\ .reduceByKey(lambda x,y:x+y)\ .sortBy(lambda x:x[1],False) # print(sougouResult1.take(5)) # TODO*3 - 完成需求2:用戶搜索點擊統(tǒng)計 print("=============完成需求2:用戶搜索點擊統(tǒng)計==================") # 根據用戶id和搜索的內容作為分組字段進行統(tǒng)計 sougouClick = resultRDD.map(lambda record: (record[1], record[2])) sougouResult2=sougouClick\ .map(lambda tuple:(tuple,1))\ .reduceByKey(lambda x,y:x+y) #key,value # 打印一下最大的次數(shù)和最小的次數(shù)和平均次數(shù) print("max count is:",sougouResult2.map(lambda x: x[1]).max()) print("min count is:",sougouResult2.map(lambda x: x[1]).min()) print("mean count is:",sougouResult2.map(lambda x: x[1]).mean()) # 如果對所有的結果排序 # print(sougouResult2.sortBy(lambda x: x[1], False).take(5)) # TODO*4 - 完成需求3:搜索時間段統(tǒng)計 print("=============完成需求3:搜索時間段-小時-統(tǒng)計==================") #00:00:00 hourRDD = resultRDD.map(lambda x: str(x[0])[0:2]) sougouResult3=hourRDD\ .map(lambda word:(word,1))\ .reduceByKey(lambda x,y:x+y)\ .sortBy(lambda x:x[1],False) print("搜索時間段-小時-統(tǒng)計",sougouResult3.take(5)) # TODO*5 - 停止sparkcontext sc.stop()
總結
- 重點關注在如何對數(shù)據進行清洗,如何按照需求進行統(tǒng)計
- 1-rdd的創(chuàng)建的兩種方法,必須練習
- 2-rdd的練習將基礎的案例先掌握。map。flatMap。reduceByKey
- 3-sougou的案例需要聯(lián)系2-3遍
- 練習流程:
- 首先先要將代碼跑起來
- 然后在理解代碼,這一段代碼做什么用的
- 在敲代碼,需要寫注釋之后敲代碼
AI副業(yè)實戰(zhàn)手冊:http://www.yibencezi.com/notes/253200?affiliate_id=1317(目前40+工具及實戰(zhàn)案例,持續(xù)更新,實戰(zhàn)類小冊排名第一,做三個月掙不到錢找我退款,交個朋友的產品)
后記
??博客主頁:https://manor.blog.csdn.net文章來源:http://www.zghlxwxcb.cn/news/detail-713135.html
??歡迎點贊 ?? 收藏 ?留言 ?? 如有錯誤敬請指正!
??本文由 Maynor 原創(chuàng),首發(fā)于 CSDN博客??
??感覺這輩子,最深情綿長的注視,都給了手機?
??專欄持續(xù)更新,歡迎訂閱:https://blog.csdn.net/xianyu120/category_12453356.html文章來源地址http://www.zghlxwxcb.cn/news/detail-713135.html
到了這里,關于Python大數(shù)據之PySpark(七)SparkCore案例的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!