【发布时间】:2019-09-07 20:51:44
【问题描述】:
我正在 pyspark 中编写一个 grep 工具,它在命令行上获取一个单词并搜索一个文本文件并返回包含命令行中给出的单词的任何行。我的搜索返回不是搜索词的行
#!/usr/bin/python
import sys
from pyspark import SparkContext
def search_word(word):
if (word) != -1:
print ('%s\t%s' % ( word, word.strip() ))
# assign search word given on command line
if len(sys.argv) > 1:
word = sys.argv[1]
sc = SparkContext()
textRDD = sc.textFile("input.txt")
textRDD = textRDD.map(lambda word: word.replace(',',' ').replace('.',' '). lower())
textRDD = textRDD.flatMap(lambda word: word.split())
textRDD = textRDD.filter(lambda word: search_word(word))
firstten = textRDD.take(10)
print(firstten)
命令行示例:spark-submit 自己
示例文本文件:
Ere quitting, for the nonce, the Sperm Whale's head, I would have
you, as a sensible physiologist, simply--particularly remark its front
aspect, in all its compacted collectedness. I would have you investigate
it now with the sole view of forming to yourself some unexaggerated,
intelligent estimate of whatever battering-ram power may be lodged
there. Here is a vital point; for you must either satisfactorily settle
this matter with yourself, or for ever remain an infidel as to one of
the most appalling, but not the less true events, perhaps anywhere to be found in all recorded history.
预期结果:
yourself -- it now with the sole view of forming to yourself some unexaggerated
上面的代码返回这个:
produce produce
our our
new new
ebooks ebooks
【问题讨论】:
-
请不要在没有解释原因的情况下对我的帖子投反对票。我只是要求解释我哪里出错了。
-
使用 df,没有 flatMap 和带有 like 的 df。见stackoverflow.com/questions/41889974/…
-
还需要帮助吗?
-
是的,我无法让它工作
-
好的,我稍后会发送答案