使用RDD和DataFrames获得不同的结果

Fisseha Berhane

我正在使用Spark RDD和DataFrame创建文本文件的字数统计,但答案略有不同。我正在使用的数据在这里可用为什么答案不同?

from pyspark.sql.functions import col, trim, lower, regexp_replace, explode, split, length
 from pyspark.sql import Row

 from nltk.corpus import stopwords
 stopwords = stopwords.words('english')

数据框

def clean1(line):
   return trim(lower(regexp_replace(line, '[^a-zA-Z0-9\s]','')))

 df1 = (spark.read.text("Quran.txt")
   .select(clean1('value').alias('line'))
   .select(explode(split('line', ' ')).alias('word'))
   .filter(length(col("word")) > 0)
   .filter(~col('word').isin(stopwords))
   .groupBy('word').count()
   .orderBy('count', ascending = False)
  )

  df1.show(10)

在此处输入图片说明

RDD

 import re

 def cleanline(line):
    return re.sub('[^a-zA-Z0-9\s]', '', line).lower().strip()

df2 = (sc.textFile('Quran.txt')
   .map(lambda x: cleanline(x))
   .flatMap(lambda x: x.split(' '))
   .filter(lambda x: len(x) > 0)
   .filter(lambda x: x not in stopwords)
   .map(lambda x: (x, 1))
   .reduceByKey(lambda a, b: a + b)
   .sortBy(lambda x: -x[1])
   .map(lambda x: Row(word = x[0], count = x[1]))
   .toDF()
   .select(['word','count'])
  )

  df2.show(10)

在此处输入图片说明

更新

我发现,如果我将DataFrame部分中的正则表达式从“ [^ a-zA-Z0-9 \ s]”更改为“ [^ a-zA-Z0-9]”,答案将相同。这两个正则表达式模式不一样吗?

拉梅什·马哈然(Ramesh Maharjan)

如果看一下函数的定义 trim

'ltrim': 'Trim the spaces from left end for the specified string value.',
'rtrim': 'Trim the spaces from right end for the specified string value.',
'trim': 'Trim the spaces from both ends for the specified string column.',

表示修剪只删除空格,不删除制表符(\ t)但是下面的某些行中有一些选项卡这些选项卡并未按trim功能删除

God could destroy him if He chose, v. 19 (488)

那就是god上面一行中没有制表符的原因,因此不计在内。strip()函数取出所有的空间,包括在前面的标签。

其他计数也是如此。

所以定义udf,其中函数strip()Python函数可以用来为您的解决方案。

from pyspark.sql import functions as F
from pyspark.sql import types as T

def stripUdf(x):
    return x.strip()

callStripUdf = F.udf(stripUdf, T.StringType())

def clean1(line):
    return callStripUdf(F.trim(F.lower(F.regexp_replace(line, '[^a-zA-Z0-9\s]',''))))

现在,正如您提到的那样,从更改[^a-zA-Z0-9\s][^a-zA-Z0-9 ]解决了该问题,这是因为\ s表示包括制表符(\ t)在内的所有空格,因此应用更改将制表符替换字符trim

我希望答案是有帮助的

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用%dopar%和%do%获得不同的结果

使用ggplot和基本绘图功能获得不同的结果

使用Sparklyr和Dplyr时获得不同的结果

在Windows和Linux中使用R获得不同的结果

使用多线程在不同的运行中获得不同的结果

在Java和golang中使用AES时获得不同的结果(密文)

模板专业化-使用clang和gcc可获得不同的结果

使用randomForest()和插入符号的randomForest获得不同的结果(方法=“ rf”)

使用mongo和mongoClient从同一查询中获得不同的投影结果

在 T-SQL 和 LINQ (EF Core) 中使用相同的查询获得不同的结果

使用“ LEFT OUTER JOIN”和“ IN”获得不同的结果,我的逻辑哪里出问题了?

Flutter - 如何使用相同的未来获得不同的结果?

在 mysql 中使用更多条件获得不同的结果

使用多线程生产者-消费者在运行和删除模式下获得不同的结果

如何迭代对象以获得不同的结果?

改进聚合查询以获得不同的结果

反射-从HashMap获得不同的结果-LinkedHashMap

Keras与定种子获得不同的结果

Java AES加密获得不同的结果

每次从BeautifulSoup获得不同的结果

使用相同命令在不同文件夹中获得不同结果

使用不同库中的相同功能获得不同结果

为什么SVM使用不同的功能获得不同的结果?

如何以相同的方法使用不同的按钮以获得不同的结果?

Python Scrapy:使用不同的方法获取页面可获得不同的结果

Sklearn 在不同系统(机器)上使用相同的 random_state 获得不同的结果

MySQL获得不同时间段和组的结果

本机浏览器和Node.js中的JavaScript函数声明获得不同的结果

Charset.defaultCharset()在JDK1.7和JDK 1.6下获得不同的结果