Код: Выделить всё
py4j.protocol.Py4JJavaError: An error occurred while calling o48.saveAsTextFile.
org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory file:/home/pyspark_python/wordcount/output_new already exists
Проверил, что выходной каталог не содержит никаких данных (ls показывает, что он пуст).
Удалил и воссоздал каталог с помощью rm -r и mkdir -p.
Убедился, что другие задания Spark не выполняются (ps aux | grep spark).
Несмотря на это , ошибка сохраняется, когда я перезапустите скрипт.
Вот код, который я использую:
Код: Выделить всё
from pyspark import SparkConf, SparkContext
import os
def main(input_file, output_dir):
# Configuration Spark
conf = SparkConf().setAppName("WordCountTask").setMaster("local[*]")
sc = SparkContext(conf=conf)
# Lecture du fichier d'entrée
text_file = sc.textFile(input_file)
# Comptage des mots
counts = (
text_file.flatMap(lambda line: line.split(" "))
.map(lambda word: (word, 1))
.reduceByKey(lambda a, b: a + b)
)
# Sauvegarde des résultats
if not os.path.exists(output_dir):
os.makedirs(output_dir)
counts.saveAsTextFile(output_dir)
print(f"Résultats sauvegardés dans le répertoire : {output_dir}")
if __name__ == "__main__":
# Définir les chemins d'entrée et de sortie
input_file = r"/home/othniel/pyspark_python/wordcount/input/loremipsum.txt"
output_dir = "/home/othniel/pyspark_python/wordcount/output_new"
# Exécution de la tâche WordCount
main(input_file, output_dir)
Спасибо за помощь!
Подробнее здесь: https://stackoverflow.com/questions/793 ... ory-during