【问题标题】:spark grep tool issuespark grep 工具问题
【发布时间】: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/…
  • 还需要帮助吗?
  • 是的,我无法让它工作
  • 好的,我稍后会发送答案

标签: python pyspark


【解决方案1】:

在数据和结果方面不太确定您的示例,但据我所知,我认为不需要 flatMap 或拆分。

这里是单一的 grep 值方法,只有几行代码:

import pyspark.sql.functions as f
df = spark.read.text("/FileStore/tables/sample_text.txt").toDF("text_string")
df.show(100, truncate=False)
grep_val = 'ZZZ'
df.where(df.text_string.contains(grep_val)).show(100, truncate=False)

返回:

+-------------------------+
|text_string              |
+-------------------------+
|Hi how are you today ZZZ |
|I am fine                |
|I am also tired          |
|You look good            |
|Can I stay with you?     |
|Bob will pop in later ZZZ|
|Oh really? Nice, cool    |
+-------------------------+

+-------------------------+
|text_string              |
+-------------------------+
|Hi how are you today ZZZ |
|Bob will pop in later ZZZ|
+-------------------------+

您最好遵循使用 grep_list、rlike 和 JOIN 的标准方法。请参阅PySpark: Search For substrings in text and subset dataframe,以更灵活的方式获取一般指导。

【讨论】:

  • 谢谢。这是一个很好的开始
猜你喜欢
  • 2014-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多