【发布时间】:2018-11-29 00:04:07
【问题描述】:
在 PySpark 中,重新分区模块有一个可选的列参数,它当然会通过该键重新分区您的数据帧。
我的问题是 - 当没有密钥时,Spark 如何重新分区?我无法进一步挖掘源代码以找到它通过 Spark 本身的位置。
def repartition(self, numPartitions, *cols):
"""
Returns a new :class:`DataFrame` partitioned by the given partitioning expressions. The
resulting DataFrame is hash partitioned.
:param numPartitions:
can be an int to specify the target number of partitions or a Column.
If it is a Column, it will be used as the first partitioning column. If not specified,
the default number of partitions is used.
.. versionchanged:: 1.6
Added optional arguments to specify the partitioning columns. Also made numPartitions
optional if partitioning columns are specified.
>>> df.repartition(10).rdd.getNumPartitions()
10
>>> data = df.union(df).repartition("age")
>>> data.show()
+---+-----+
|age| name|
+---+-----+
| 5| Bob|
| 5| Bob|
| 2|Alice|
| 2|Alice|
+---+-----+
>>> data = data.repartition(7, "age")
>>> data.show()
+---+-----+
|age| name|
+---+-----+
| 2|Alice|
| 5| Bob|
| 2|Alice|
| 5| Bob|
+---+-----+
>>> data.rdd.getNumPartitions()
7
"""
if isinstance(numPartitions, int):
if len(cols) == 0:
return DataFrame(self._jdf.repartition(numPartitions), self.sql_ctx)
else:
return DataFrame(
self._jdf.repartition(numPartitions, self._jcols(*cols)), self.sql_ctx)
elif isinstance(numPartitions, (basestring, Column)):
cols = (numPartitions, ) + cols
return DataFrame(self._jdf.repartition(self._jcols(*cols)), self.sql_ctx)
else:
raise TypeError("numPartitions should be an int or Column")
例如:调用这些行完全没问题,但我不知道它实际上在做什么。它是整行的哈希吗?也许是数据框中的第一列?
df_2 = df_1\
.where(sf.col('some_column') == 1)\
.repartition(32)\
.alias('df_2')
【问题讨论】:
-
我的回答是否帮助您了解了没有密钥的重新分区是如何工作的?
标签: python apache-spark pyspark pyspark-sql