我想忽略產生錯誤的路徑:
'路徑不存在'
當我用 pyspark 讀取鑲木地板檔案時。例如,我有一個路徑串列:
list_paths = ['path1','path2','path3']
并閱讀如下檔案:
dataframe = spark.read.parquet(*list_paths)
但路徑path2不存在。一般來說,我不知道哪個路徑不存在,所以我想path2自動忽略。我怎樣才能做到這一點并只獲得一個資料幀?
uj5u.com熱心網友回復:
也許你可以
existing_paths = [path for path in list_paths if os.path.exists(path)]
dataframe = spark.read.parquet(*existing_paths)
uj5u.com熱心網友回復:
您可以使用Hadoop FS API在將檔案傳遞給之前檢查檔案是否存在spark.read:
conf = sc._jsc.hadoopConfiguration()
Path = sc._gateway.jvm.org.apache.hadoop.fs.Path
filtered_paths = [p for p in list_paths if Path(p).getFileSystem(conf).exists(Path(p))]
dataframe = spark.read.parquet(*filtered_paths)
scSparkContext在哪里。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/383925.html
