假設我提供了以下鑲木地板資料磁區:
.
└── data/
├── product=soda/
│ ├── <hash>_toto.parquet
│ ├── ...
│ └── <hash>.parquet
└── product=cake/
├── <hash>.parquet
└── ...
我想使用 PySpark 讀取資料,但要排除包含<hash>_toto.parquet.
我可以讀取整個磁區資料,但我不知道如何排除其中的一些。我想保留 Spark 實作的用于合并資料并product在此處使用以下代碼創建列的功能:
from pyspark.sql import SparkSession, SQLContext
# this code read all parquets, merge them, and create a column product
spark = SparkSession.builder \
.master("local") \
.appName("app") \
.config("spark.executor.memory", "5gb") \
.config("spark.cores.max", "6") \
.getOrCreate()
sc = spark.sparkContext
sqlContext = SQLContext(sc)
dataframe = sqlContext.read.parquet("./data")
uj5u.com熱心網友回復:
使用input_file_name函式過濾掉來自檔案名中包含'hash'的檔案的行:
from pyspark.sql import functions as F
df = sqlContext.read.parquet("./data")
df = df.filter(~F.input_file_name().rlike('hash'))
uj5u.com熱心網友回復:
您可以在資料框中添加一個布爾列toto,然后partitionBy('toto'). 然后你會得到子檔案夾 toto=True/ 和 toto=False/。
閱讀完整的檔案夾后,您可以簡單地運行 `.where('toto == False') 以防止 Spark 讀取這些鑲木地板。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/418182.html
標籤:
