Python pySpark中是否有可能在两个单独的单词中搜索字符串?

Python pySpark中是否有可能在两个单独的单词中搜索字符串?,python,apache-spark,pyspark,Python,Apache Spark,Pyspark,我正在寻找一种在PythonSpark中搜索两个单词分开的字符串的方法。例如:IPhone x或Samsun s10 例如,我想给出一个文本文件和一个复合字符串(iphonex),然后得到结果 我在互联网上找到的只是一个字数 在spark 2.0中,如果您是gunna,请从文件中读取,例如.csv文件: df = spark.read.format("csv").option("header", "true").load("pathtoyourcsvfile.csv") 然后,您可以使用以下正

我正在寻找一种在PythonSpark中搜索两个单词分开的字符串的方法。例如:IPhone x或Samsun s10

例如,我想给出一个文本文件和一个复合字符串(iphonex),然后得到结果

我在互联网上找到的只是一个字数

在spark 2.0中,如果您是gunna,请从文件中读取,例如.csv文件:

df = spark.read.format("csv").option("header", "true").load("pathtoyourcsvfile.csv")
然后,您可以使用以下正则表达式对其进行过滤:

pattern = "\s+(word1|word2)\s+"
filtered = df.filter(df['<thedesiredcolumnhere>'].rlike(pattern))
pattern=“\s+(word1 | word2)\s+”
filtered=df.filter(df['].rlike(模式))

您可以尝试编写自己的UDF,并结合使用以分割单词,您还可以向词典中添加新词,以帮助库分割新词,例如“Iphone x”

例如:

>>> from wordsegment import clean
>>> clean('She said, "Python rocks!"')
'shesaidpythonrocks'
>>> segment('She said, "Python rocks!"')
['she', 'said', 'python', 'rocks']
如果您不想使用library,也可以看到以下答案:

# give a file
rdd = sc.textFile("/root/PycharmProjects/Spark/file") 

# give a composite string
string_ = "Iphone x" 

# filer by line containing the string
new_rdd = rdd.filter(lambda line: string_ in line) 

# collect these lines
rt = str(new_rdd.collect()) 

# apply regex to find all words and count 
count = re.findall(string_, rt) them

你是在问是否有可能判断一列中每个值中是否存在由两个单词组成的字符串?我想用pyspark搜索文本文件中“IPhone X”的计数