I have a set of files. The file path is saved in the file. Let's say "all_files.txt". Using the apache spark, I need to perform an operation on all files and collect the results.
The steps I want to take are as follows:
- Create an RDD by reading "all_files.txt"
- For each line in "all_files.txt" (each line is a path to some file) read the contents of each file in one RDD
- Then do the operation with all the contents
This is the code I wrote for it:
def return_contents_from_file (file_name): return spark.read.text(file_name).rdd.map(lambda r: r[0]) def run_spark(): file_name = 'path_to_file' spark = SparkSession \ .builder \ .appName("PythonWordCount") \ .getOrCreate() counts = spark.read.text(file_name).rdd.map(lambda r: r[0]) \
This causes an error:
line 323, in get_return_value py4j.protocol.Py4JError: an error occurred while calling o25. getnewargs . Trace: py4j.Py4JException: The getnewargs ([]) method does not exist when py4j.reflection.ReflectionEngine.getMethod (ReflectionEngine.javahaps18) in py4j.reflection.ReflectionEngine.getMethod (ReflectionEngine.java:326) on py4jvoke (Gateway.java:272) at py4j.commands.AbstractCommand.invokeMethod (AbstractCommand.java:132) at py4j.commands.CallCommand.execute (CallCommand.java:79) at py4j.GatewayConnection.run (GatewayConnection.java:14) in java.lang.Thread.run (Thread.java:745)
Can someone please tell me what I am doing wrong and how I should move on. Thanks in advance.
source share