利用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. POI读取xls和xlsx

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import  ...

  2. TCP_Wrappers 简介

    TCP_Wrappers     简介 TCP_Wrappers是一个工作在第四层(传输层)的的安全工具,对有状态连接的特定服务进行安全检测并实现访问控制,凡是包含有libwrap.so库文件的的程序 ...

  3. 【Leetcode】【Medium】Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. DFS BFS代码

    #define maxnum 30 #include<bits_stdc++.h> int visited[maxnum]={0}; using namespace std; typede ...

  5. June 28th 2017 Week 26th Wednesday

    Anger begins with folly, and ends in repentance. 愤怒以愚蠢开始,以后悔告终. Learn to control your temper, don't ...

  6. dba_tables、all_tables、user_tables

    本文摘抄自:http://blog.csdn.net/daxiang12092205/article/details/42921063 dba_tables : 系统里所有的表的信息,需要DBA权限才 ...

  7. 一对一关联关系基于主键映射的异常 IdentifierGenerationException

    具体异常:org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one pro ...

  8. The Binder Architecture

    The Binder Architecture is a declarative architecture for iOS development inspired by MVVM and VIPER ...

  9. Gluon 实现 dropout 丢弃法

    多层感知机中: hi 以 p 的概率被丢弃,以 1-p 的概率被拉伸,除以  1 - p import mxnet as mx import sys import os import time imp ...

  10. [改错_19/04/01] 学习Java.IO 对象数据流时出现 Exception in thread "main" java.io.EOFException ...at cn.sxt.test.Test_DataStream.main(Test_DataStream.java:31) 错误 .

    过程描述:编译可以通过,就是每次运行时出现如下的图片,百思不得其解. 错误原因: byte[] datas=baos.toByteArray(); 放在了oos.writeInt(14);oos.fl ...