利用python来操作spark的词频统计,现将过程分享如下:

1.新建项目:(这里是在已有的项目中创建的,可单独创建wordcount项目)

①新建txt文件: wordcount.txt (文件内容: 跟词频统计(一)中文件一致)

②创建py文件: word.py

from pyspark import SparkContext
from pyspark import SparkConf conf = SparkConf().setAppName('word').setMaster('local')
sc = SparkContext(conf=conf)
wordcount = sc.textFile(r'E:\Hbase\api\wordcount')
counts = wordcount.flatMap(lambda x: x.split(" "))\
.map(lambda word: (word, 1)) \
.reduceByKey(lambda a, b: a + b).collect()
print(counts)

打印结果:

[('development', 1), ('producing', 1), ('among', 1), ('Source,', 1), ('for', 1), ('quality', 1), ('to', 1), ('influencers', 1), ('advances', 1), ('collaborative', 1), ('model', 1), ('in', 1), ('the', 2), ('of', 1), ('has', 1), ('successful', 1), ('Software', 1), ("Foundation's", 1), ('most', 1), ('long', 1), ('that', 1), ('uded', 1), ('as', 1), ('Open', 1), ('The', 1), ('commitment', 1), ('software', 1), ('consistently', 1), ('a', 1), ('development.', 1), ('high', 1), ('future', 1), ('Apache', 1), ('served', 1), ('open', 1), ('https://s.apache.org/PIRA', 1)]

2.如果词频统计的数据量较小,可以如下:

from pyspark import SparkContext
from pyspark import SparkConf conf = SparkConf().setAppName('word').setMaster('local')
sc = SparkContext(conf=conf)
data = [r"uded among the most successful influencers in Open Source, The Apache Software Foundation's\
commitment to collaborative development has long served as a model for producing consistently\
high quality software that advances the future of open development. https://s.apache.org/PIRA\
"]
datardd = sc.parallelize(data) result = datardd.flatMap(lambda x: x.split(' ')).map(lambda x: (x,1)).reduceByKey(lambda a,b: a+b).collect()
print(result)

打印结果:

[('', 18), ('development', 1), ('producing', 1), ('among', 1), ('Source,', 1), ('for', 1), ('quality', 1), ('to', 1), ('influencers', 1), ('served', 1), ('collaborative', 1), ('in', 1), ('the', 2), ('Open', 1), ('of', 1), ('has', 1), ('long', 1), ('https://s.apache.org/PIRA\\\n', 1), ('successful', 1), ('Software', 1), ('most', 1), ('consistently\\\n', 1), ('a', 1), ("Foundation's\\\n", 1), ('uded', 1), ('as', 1), ('advances', 1), ('The', 1), ('commitment', 1), ('software', 1), ('that', 1), ('development.', 1), ('high', 1), ('future', 1), ('Apache', 1), ('model', 1), ('open', 1)]
18/07/27 17:14:34 INFO SparkContext: Invoking stop() from shutdown hook
result = datardd.flatMap(lambda x: x.split(' ')).map(lambda x: (x,1)).reduceByKey(lambda a,b: a+b).collect()
print(result)

总结:

①在window上利用python操作spark词频统计前提: 本机要有spark的系统环境配置 和java的环境配置,配置步骤类似于python,必须确保安装无误才能运行结果.

②注意本机的python 跟spark的版本的兼容性,本机是python3.6 /spark1.6,很明显两者不兼容,需要重新安装3.5版本的python, linux上python跟spark也是同理.

③实际工作过程中需要注意:collect()的数据收集,在大数据处理过程中都是p量级的海量数据,如果不加思索直接collect()会直接导致内存崩溃.

​ 针对③的情况,建议操作有:

from pyspark import SparkContext
from pyspark import SparkConf conf = SparkConf().setAppName('word').setMaster('local')
sc = SparkContext(conf=conf)
data = [r"uded among the most successful influencers in Open Source, The Apache Software Foundation's\
commitment to collaborative development has long served as a model for producing consistently\
high quality software that advances the future of open development. https://s.apache.org/PIRA\
"]
datardd = sc.parallelize(data) # result = datardd.flatMap(lambda x: x.split(' ')).map(lambda x: (x,1)).reduceByKey(lambda a,b: a+b).collect()
# print(result)
result = datardd.flatMap(lambda x: x.split(' ')).map(lambda x: (x,1)).reduceByKey(lambda a,b: a+b)
def f(x):
print(x) result2 = result.foreach(f)
print(result2)
解释:它是通过foreach()遍历循环将数据结果挨个挨个打印到后台,避免撑爆内存的风险!

spark ---词频统计(二)的更多相关文章

  1. Python3.7 练习题(二) 使用Python进行文本词频统计

    # 使用Python进行词频统计 mytext = """Background Industrial Light & Magic (ILM) was starte ...

  2. spark core (二)

    一.Spark-Shell交互式工具 1.Spark-Shell交互式工具 Spark-Shell提供了一种学习API的简单方式, 以及一个能够交互式分析数据的强大工具. 在Scala语言环境下或Py ...

  3. Spark 系列(二)—— Spark开发环境搭建

    一.安装Spark 1.1 下载并解压 官方下载地址:http://spark.apache.org/downloads.html ,选择 Spark 版本和对应的 Hadoop 版本后再下载: 解压 ...

  4. C语言实现词频统计——第二版

    原需求 1.读取文件,文件内包可含英文字符,及常见标点,空格级换行符. 2.统计英文单词在本文件的出现次数 3.将统计结果排序 4.显示排序结果 新需求: 1.小文件输入. 为表明程序能跑 2.支持命 ...

  5. 软件工程第一次个人项目——词频统计by11061153柴泽华

    一.预计工程设计时间 明确要求: 15min: 查阅资料: 1h: 学习C++基础知识与特性: 4-5h: 主函数编写及输入输出部分: 0.5h: 文件的遍历: 1h: 编写两种模式的词频统计函数: ...

  6. python瓦登尔湖词频统计

    #瓦登尔湖词频统计: import string path = 'D:/python3/Walden.txt' with open(path,'r',encoding= 'utf-8') as tex ...

  7. Hadoop上的中文分词与词频统计实践 (有待学习 http://www.cnblogs.com/jiejue/archive/2012/12/16/2820788.html)

    解决问题的方案 Hadoop上的中文分词与词频统计实践 首先来推荐相关材料:http://xiaoxia.org/2011/12/18/map-reduce-program-of-rmm-word-c ...

  8. 【原创】大数据基础之词频统计Word Count

    对文件进行词频统计,是一个大数据领域的hello word级别的应用,来看下实现有多简单: 1 Linux单机处理 egrep -o "\b[[:alpha:]]+\b" test ...

  9. Python——字符串、文件操作,英文词频统计预处理

    一.字符串操作: 解析身份证号:生日.性别.出生地等. 凯撒密码编码与解码 网址观察与批量生成 2.凯撒密码编码与解码 凯撒加密法的替换方法是通过排列明文和密文字母表,密文字母表示通过将明文字母表向左 ...

随机推荐

  1. SQL计算上下两行某列的差

    SELECT * FROM #TempHuDong SELECT * FROM #TempHuDong SELECT TOP 1 ABS(a.num -b.num) '差' FROM (select ...

  2. Surrounded Regions [未完成]

    思路完全一样 AC的代码: class Solution { private: struct Point { int x, y; Point(int _x, int _y):x(_x), y(_y) ...

  3. Spark资源调度分配内幕天机彻底解密:Driver在Cluster模式下的启动、两种不同的资源调度方式源码彻底解析、资源调度内幕总结

    本课主题 Master 资源调度的源码鉴赏 资源调度管理 任务调度与资源是通过 DAGScheduler.TaskScheduler.SchedulerBackend 等进行的作业调度 资源调度是指应 ...

  4. selenium 滑动解锁(drag_and_drop_by_offset)

    from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from ...

  5. Shell,Bash,等脚本学习(有区别)

    二元比较操作符,比较变量或者比较数字.注意数字与字符串的区别.   整数比较   -eq        等于,如:if [ "$a" -eq "$b" ] -n ...

  6. ZOJ-3286 Very Simple Counting---因子数打表

    题目链接: https://cn.vjudge.net/problem/ZOJ-3286 题目大意: f(n)为n的因子个数 求出有多少个f(i)使得f(i) == f(n) && i ...

  7. O(1) 和 O(n) 的区别

    举个简单的例子,要从0加到n,我们会这么写:int sum = 0;for(int i = 0; i<=n; ++i){   sum += i;}一共算了n次加法,那么就说这个时间复杂度是O(n ...

  8. CALayer & bitmap Content

    Working with High-Resolution Images Layers do not have any inherent knowledge of the resolution of t ...

  9. Android 进价5_自定义广播 通过广播更新ListView的适配器 下载管理

    1.在处理下载管理时,服务在后台运行,下载完成后要更新listview列表的按钮,将“下载”改成“打开”这样一个功能. 在Activity里面写一个静态内部类,继承广播.其中属性text_button ...

  10. vue入门学习示例

    鄙人一直是用angular框架的,所以顺便比较了一下. <!DOCTYPE html> <html lang="en"> <head> < ...