【发布时间】:2020-07-28 14:40:05
【问题描述】:
编辑 [1]:修改示例数据以准确显示情况。
编辑 [2]:添加(测试)代码以显示其应如何工作.
我有一个如下所示的 (PySpark) 数据框:
示例数据(显示为 2 列):
ID Type Other ID Type Other
-------------- --------------
101 A 1 105 A 1
101 A 2 105 A 2
101 B 1 105 B 1
102 A 1 105 A 4
102 A 2 106 A 1
103 A 1 106 A 2
103 A 2 201 B 1
103 B 1 201 A 1
104 A 1 202 B 1
104 A 2 203 B 1
104 A 3 203 B 2
我已经按 ID 分区了。
如何对这些数据进行分区,以使 type 也在多个分区中平均分布?
所以,如果我只是按 ID 分区,并且我正在制作 3 个分区,我可以得到这个:
分区:
1 2 3
--- --- ---
102 A * 101 A * 201 B *
104 A * 103 A * 202 B *
106 A * 105 A * 203 B *
101 B * 201 A *
103 B *
105 B *
但是,处理 B 比处理 A 在计算上更昂贵,所以我在只有类型 B 的分区 3 上遇到了瓶颈。
理想情况下,我想像这样对数据进行分区:
1 2 3
--- --- ---
101 A * 103 A * 105 A *
102 A * 104 A * 106 A *
201 B * 202 B * 203 B *
101 B * 103 B * 105 B *
201 A *
我该怎么做?
以下代码是我想要做的一个例子:
import random
import pyspark.sql.functions as f
from pyspark import Row
from pyspark.sql import DataFrame
from test_common.test_base import PySparkTest
RANDOM = random.Random()
def spread_values_OVER_partitions(df_input: DataFrame, concentrate_col_name: str, spread_col_name: str) -> DataFrame:
"""This method SHOULD partition a dataframe so that the first column is partitioned normally, but the "spread_col_name" column is spread over all partitions.
Args:
df_input: The dataframe to partitions
concentrate_col_name: The column (name) on which you should (normally) partition.
spread_col_name: The column (name) over which values should be SPREAD over the partitions.
Returns:
The repartitioned dataframe.
"""
# THIS DOES NOT WORK!
return df_input.repartition(3, concentrate_col_name, spread_col_name)
class PartitionSpreadTest(PySparkTest):
def test_spread_partitioning(self):
"""Test how to spread a certain columns values *OVER* partitions, instead of concentrating them."""
test_data_tuple = [(id, 'A', other) for id in range(101, 106) for other in range(1, RANDOM.randint(3, 4))]
test_data_tuple.extend([(id, 'B', other) for id in [104] + list(range(201, 204)) for other in range(1, RANDOM.randint(4, 5))])
test_data_dict = [{'id': r[0],
'type': r[1],
'other': r[2],
}
for r in test_data_tuple]
df_test = self.spark.createDataFrame(Row(**x) for x in test_data_dict)
num_part = 3
df_test.groupby('id', 'type').agg(f.count('id')).orderBy('id', 'type').show(100, False)
# This DOES NOT WORK!
df_repartitioned = spread_values_OVER_partitions(df_test, concentrate_col_name='id', spread_col_name='type')
partition_cols = ['id', 'type']
print(f"Num partitions: [{num_part:3}]: \n")
# print partitions
(df_repartitioned.select(
*partition_cols,
f.spark_partition_id().alias('part_id'))
.distinct()
.groupBy(*partition_cols)
.agg(f.collect_list('part_id').alias('part_ids'))
.withColumn('num_parts', f.size('part_ids'))
.orderBy('part_ids', *partition_cols).select(
*partition_cols,
'part_ids',
'num_parts')
.show(1000, False))
但是,上面的代码输出如下:
+---+----+--------+---------+
|id |type|part_ids|num_parts|
+---+----+--------+---------+
|101|A |[0] |1 |
|104|A |[0] |1 |
|105|A |[0] |1 |
|202|B |[0] |1 |
|203|B |[0] |1 |
|104|B |[1] |1 |
|201|B |[1] |1 |
|102|A |[2] |1 |
|103|A |[2] |1 |
+---+----+--------+---------+
在这种情况下,
- 分区
[1]仅包含类型B - 分区
[2]仅包含A型
这与我想要的相反。
【问题讨论】:
-
我开始怀疑我是否需要在 ('type', 'id') 上设置一个窗口函数,为每个组合分配不同的分区 id ("row_number)。???? 我希望有一个内置的或更优雅的解决方案。
标签: apache-spark pyspark partitioning