1

我只想计算火花(pyspark)中的单词,但我可以映射字母或整个字符串。

我试过了:(整个字符串)

v1='Hi hi hi bye bye bye word count' 
v1_temp=sc.parallelize([v1]) 
v1_map = v1_temp.flatMap(lambda x: x.split('\t'))
v1_counts = v1_map.map(lambda x: (x, 1))
v1_counts.collect()  

或(只是字母)

v1='Hi hi hi bye bye bye word count'
v1_temp=sc.parallelize(v1)
v1_map = v1_temp.flatMap(lambda x: x.split('\t'))
v1_counts = v1_map.map(lambda x: (x, 1))
v1_counts.collect()
4

3 回答 3

4

当你这样做时,sc.parallelize(sequence)你正在创建一个将并行操作的 RDD。在第一种情况下,您的序列是一个包含单个元素(整个句子)的列表。在第二种情况下,您的序列是一个字符串,在 python 中类似于字符列表。

如果你想并行计算单词,你可以这样做:

from operator import add

s = 'Hi hi hi bye bye bye word count' 
seq = s.split()   # ['Hi', 'hi', 'hi', 'bye', 'bye', 'bye', 'word', 'count']
sc.parallelize(seq)\
  .map(lambda word: (word, 1))\
  .reduceByKey(add)\
  .collect()

会给你:

[('count', 1), ('word', 1), ('bye', 3), ('hi', 2), ('Hi', 1)]
于 2015-01-16T15:55:00.093 回答
3

如果您只想计算字母数字单词,这可能是一个解决方案:

import time, re
from pyspark import SparkContext, SparkConf

def linesToWordsFunc(line):
    wordsList = line.split()
    wordsList = [re.sub(r'\W+', '', word) for word in wordsList]
    filtered = filter(lambda word: re.match(r'\w+', word), wordsList)
    return filtered

def wordsToPairsFunc(word):
    return (word, 1)

def reduceToCount(a, b):
    return (a + b)

def main():
    conf = SparkConf().setAppName("Words count").setMaster("local")
    sc = SparkContext(conf=conf)
    rdd = sc.textFile("your_file.txt")

    words = rdd.flatMap(linesToWordsFunc)
    pairs = words.map(wordsToPairsFunc)
    counts = pairs.reduceByKey(reduceToCount)

    # Get the first top 100 words
    output = counts.takeOrdered(100, lambda (k, v): -v)

    for(word, count) in output:
        print word + ': ' + str(count)

    sc.stop()

if __name__ == "__main__":
    main()
于 2015-09-29T13:28:32.113 回答
2

网上的wordcount有很多版本,以下是其中的几个;

#to count the words in a file hdfs:/// of file:/// or localfile "./samplefile.txt"
rdd=sc.textFile(filename)

#or you can initialize with your list
v1='Hi hi hi bye bye bye word count' 
rdd=sc.parallelize([v1])


wordcounts=rdd.flatMap(lambda l: l.split(' ')) \
        .map(lambda w:(w,1)) \
        .reduceByKey(lambda a,b:a+b) \
        .map(lambda (a,b):(b,a)) \
        .sortByKey(ascending=False)

output = wordcounts.collect()

for (count,word) in output:
    print("%s: %i" % (word,count))
于 2017-04-18T19:01:42.367 回答