Python 为什么在Pyspark数据帧上切片、拆分和存储数据很耗时?
我是Pyspark的新手,一直在处理大量文件。我有大约2.5 TB的数据,我正在从每个文件中提取一些元数据,并将其存储在数据框中。下表给出了此元数据的示例Python 为什么在Pyspark数据帧上切片、拆分和存储数据很耗时?,python,apache-spark,pyspark,apache-spark-sql,databricks,Python,Apache Spark,Pyspark,Apache Spark Sql,Databricks,我是Pyspark的新手,一直在处理大量文件。我有大约2.5 TB的数据,我正在从每个文件中提取一些元数据,并将其存储在数据框中。下表给出了此元数据的示例 |-------|----------|----------|--------------|--------------| | ID | FileType | Metadata | AssociatedID | Group ID | |-------|----------|----------|--------------|--
|-------|----------|----------|--------------|--------------|
| ID | FileType | Metadata | AssociatedID | Group ID |
|-------|----------|----------|--------------|--------------|
| 1 | Type 1 | xyz | 3 | A |
|-------|----------|----------|--------------|--------------|
| 2 | Type 1 | fgh | 4 | A |
|-------|----------|----------|--------------|--------------|
| 3 | Type 2 | | | A |
|-------|----------|----------|--------------|--------------|
| 4 | Type 2 | | | A |
|-------|----------|----------|--------------|--------------|
| 5 | Type 4 | | | A |
|-------|----------|----------|--------------|--------------|
提取此元数据后,我希望将其存储在CSV中。我想要存储它的方式是基于下面的逻辑
|-------|----------|----------|--------------|--------------|
| ID | FileType | Metadata | AssociatedID | Group ID |
|-------|----------|----------|--------------|--------------|
| 1 | Type 1 | xyz | 3 | A |
|-------|----------|----------|--------------|--------------|
| 3 | Type 2 | | | A |
|-------|----------|----------|--------------|--------------|
| 5 | Type 4 | | | A |
|-------|----------|----------|--------------|--------------|
执行此操作的代码如下所示:
uniqueMetadata= sdf_.select("Metadata").distinct().collect()
for meta in uniqueMetadata:
# Get the rows that have metadata 'xyz'
metadata_df = sdf_.filter(col("Metadata") == meta)
#Get All Group ID's of those groups that have a metadata xyz
groupIDs= metadata_df .select("GroupID").distinct().collect()
groupIDs= [v["GroupID"] for v in groupIDs] #Convert to List
for groupID in groupIDs: #Iterate Group IDS
#Get all records for particular Group ID
groupID_df = sdf_.filter(col("GroupID") == groupID)
#Get all Associated ID's of metadata xyz in that group
uniqueAssociatedIDs= groupID_df .select("AssociatedID").distinct().collect()
uniqueAssociatedIDs= [v["AssociatedID"] for v in uniqueAssociatedIDs] #Convert to List
#Get All Rows with matching Associated IDs
matchingAssociatedDF = groupID_df .filter(col("ID").isin(uniqueAssociatedIDs))
metadata_df = metadata_df .union(matchingAssociatedDF )
#Get All Other Rows in the same group that are not FileType1/Type2
other_df = groupID_df .filter((~col("ID").isin(uniqueAssociatedIDs)) & (~col("FileType").isin(["Type 1", "Type 2"])))
metadata_df = metadata_df .union(other_df)
df_path = output_path + "/" + "/ByMetadata/" + metadata + "/" + shortuuid.uuid() + "/"
#Save in Location
metadata_df .coalesce(8).write.format("com.databricks.spark.csv").option("header", "true").save(df_path)
这段代码的问题是计算和存储时间太长。对于200个组和大约200个独特的元数据,需要48个多小时,这并不好,因为我必须批量处理大约100000个组
pyspark中是否有更好的解决方案来优化此功能?我可以尝试什么样的优化?Pyspark不是为这种类型的查询和切片而制作的吗