number of lines with number of words less than 5

2019-09-26 08:15发布

问题:

Using pyspark, I would like to find number of lines that has number of words < 5

I wrote this code but I couldn't figure out what is wrong with it

from pyspark.sql import SparkSession, Row


spark = SparkSession.builder.master("spark://master:7077").appName('test').config(conf=SparkConf()).getOrCreate()
df = spark.read.text('text.txt')
rdd = df.rdd
print(df.count())
rdd1=rdd.filter(lambda line: len((line.split(" "))<5)).collect()
print(rdd1.count())

This is the a small part of the Error
-----------------------------------------------------------------------
Py4JJavaError                             Traceback (most recent call last)
    <ipython-input-48-27233afa0b82> in <module>()
          9 rdd = df.rdd
         10 print(df.count())
    ---> 11 rdd1=rdd.filter(lambda line: len((line.split(" "))<5)).collect()
         12 print(rdd1.count())
         13 
Py4JJavaError: An error occurred while calling z:org.apache.spark.api.python.PythonRDD.collectAndServe.
: org.apache.spark.SparkException: Job aborted due to stage failure: Task 0 in stage 144.0 failed 1 times, most recent failure: Lost task 0.0 in stage 144.0 (TID 144, localhost): org.apache.spark.api.python.PythonException: Traceback (most recent call last):
  File "/Users/ff/spark/python/lib/pyspark.zip/pyspark/sql/types.py", line 1497, in __getattr__
    idx = self.__fields__.index(item)
ValueError: 'split' is not in list

回答1:

I think you have some parentheses in the wrong place in this expression:

rdd1=rdd.filter(lambda line: len((line.split(" "))<5)).collect()

The way you have it, you're doing this:

len(... < 5)

Instead of this:

len(...) < 5


回答2:

I solved it. The problem was that I was trying to split a list. This is the new line

rdd=rdd.filter(lambda line: len(line[0].split(" "))<5).collect()