What's the most efficient way to accumulate dataframes in pyspark?

时间:2016-02-12 22:12:52

标签: python apache-spark dataframe pyspark

I have a dataframe (or could be any RDD) containing several millions row in a well-known schema like this:

i = np.array([1,2])
facets = [np.array([1,2]),np.array([3,4])]
remove_from_array(facets, i)

print facets # [array([3, 4])]

I need to load a dozen other datasets from disk that contains different features for the same number of keys. Some datasets are up to a dozen or so columns wide. Imagine:

Key | FeatureA | FeatureB
--------------------------
U1  |        0 |         1
U2  |        1 |         1

It feels like a fold or an accumulation where I just want to iterate all the datasets and get back something like this:

Key | FeatureC | FeatureD |  FeatureE
-------------------------------------
U1  |        0 |        0 |         1

Key | FeatureF
--------------
U2  |        1

I've tried loading each dataframe then joining but that takes forever once I get past a handful of datasets. Am I missing a common pattern or efficient way of accomplishing this task?

1 个答案:

答案 0 :(得分:1)

假设每个DataFrame中每个键最多有一行,并且所有键都是基本类型,您可以尝试使用聚合进行联合。让我们从一些导入和示例数据开始:

from itertools import chain
from functools import reduce
from pyspark.sql.types import StructType
from pyspark.sql.functions import col, lit, max
from pyspark.sql import DataFrame

df1 = sc.parallelize([
    ("U1", 0, 1), ("U2", 1, 1)
]).toDF(["Key", "FeatureA", "FeatureB"])

df2 = sc.parallelize([
  ("U1", 0, 0, 1)
]).toDF(["Key", "FeatureC", "FeatureD", "FeatureE"])

df3 = sc.parallelize([("U2", 1)]).toDF(["Key", "FeatureF"])

dfs = [df1, df2, df3]

接下来我们可以提取常见架构:

output_schema = StructType(
  [df1.schema.fields[0]] + list(chain(*[df.schema.fields[1:] for df in dfs]))
)

并转换所有DataFrames

transformed_dfs = [df.select(*[
  lit(None).cast(c.dataType).alias(c.name) if c.name not in df.columns 
  else col(c.name)
  for c in output_schema.fields
]) for df in dfs]

最后是一个联合和虚拟聚合:

combined = reduce(DataFrame.unionAll, transformed_dfs)
exprs = [max(c).alias(c) for c in combined.columns[1:]]
result = combined.repartition(col("Key")).groupBy(col("Key")).agg(*exprs)

如果每个键有多个行但单个列仍然是原子的,您可以尝试将max / collect_list替换为collect_set,然后explode