Apache spark 火花计数包含特定单词的行数

Apache spark 火花计数包含特定单词的行数,apache-spark,pyspark,Apache Spark,Pyspark,我有一个日志文件,其中有几行包含“error”一词。如何计算apache spark中包含此术语的行的总数 到目前为止,我正在使用这种方法 from pyspark import SparkConf, SparkContext conf = SparkConf().setMaster("local").setAppName("WordCount") sc = SparkContext(conf = conf) input = sc.textFile("errors.txt") words =

我有一个日志文件,其中有几行包含“error”一词。如何计算apache spark中包含此术语的行的总数

到目前为止,我正在使用这种方法

from pyspark import SparkConf, SparkContext

conf = SparkConf().setMaster("local").setAppName("WordCount")
sc = SparkContext(conf = conf)

input = sc.textFile("errors.txt")
words = input.flatMap(lambda x: x for x if "errors" in input)
wordCounts = input.countByValue()

for word, count in wordCounts.items():
    print str(count)
但是这种方法不起作用。有人能告诉我怎么计算吗

编辑:scala中的等效项是

lines = spark.textFile("hdfs://...")
errors = lines.filter(_.startsWith("ERROR"))
errors.persist()

这一行的python等价物是什么。

input.filter(lambda行:“error”在第行)。count()应该可以工作。

input.filter(lambda行:“error”在第行)。count()应该可以工作。

请使用以下代码片段:

from pyspark import SparkConf, SparkContext

conf = SparkConf().setMaster("local").setAppName("errors")
sc = SparkContext(conf = conf)

lines = sc.textFile("errors.txt")
rdd = lines.filter(lambda x: "error" in x)
print rdd.count

请使用以下代码段:

from pyspark import SparkConf, SparkContext

conf = SparkConf().setMaster("local").setAppName("errors")
sc = SparkContext(conf = conf)

lines = sc.textFile("errors.txt")
rdd = lines.filter(lambda x: "error" in x)
print rdd.count

谢谢你的解决方案。我能用另一种方法解决它

input = sc.textFile("errors.txt")
wordCounts = input.countByValue()

for word, count in wordCounts.items():
    if "error" in word:
        print count

谢谢你的解决方案。我能用另一种方法解决它

input = sc.textFile("errors.txt")
wordCounts = input.countByValue()

for word, count in wordCounts.items():
    if "error" in word:
        print count

rdd.count
应该有效
rdd.count
应该有效