# Spark repartition vs. coalesce

visibility 998 event 2021-03-07 access_time 2 years ago

Spark provides two functions to repartition data: repartition and coalesce. These two functions are created for different use cases.

As the word coalesce suggests, function coalesce is used to merge thing together or to come together and form a g group or a single unit.

The syntax is like the following:

`def coalesce(numPartitions)`

Parameter numPartitions is used to specify the new partition number. It returns a DataFrame with exactly numPartitions partitions. This function is commonly used to reduce partitions number of a dataframe.

The following example reduces the partitions number of DataFrame to 100.

`df = df.coalesce(100)`

Function repartition can also be used to repartition a dataframe with more options available. The following is the function syntax:

`DataFrame.repartition(numPartitions, *cols)`

The following example repartitions the dataframe to 100 partitions.

`df = df.repartition(100)`

Default Spark hash partitioning function will be used to repartition the dataframe.

This example repartitions dataframe by multiple columns (Year, Month and Day):

`df = df.repartition("Year", "Month", "Day")`

Another example is to specify both parameters:

`df = df.repartition(100, "Name")`

The above example will repartition the dataframe to 100 partitions with column "Name" used as hash partitioning key.

From Spark 2.4.0, a new function named repartitionByRange is added. The syntax is like the following:

`DataFrame.repartitionByRange(numPartitions, *cols)`

This function is very similar as repartition function. However the returned dataframe is range partitioned instead of hash partitioned.

For example, the following code snippet repartition the dataframe to 100 ranges based on column Population:

`df = df.repartitionByRange(100, "Population")`

This function uses Spark range partitioner which partitions sortable records by range into roughly equal ranges.  Data sampling will be used to create ranges, which can result inconsistent results from different runs. Refer to RangePartitioner (Spark 2.3.0 JavaDoc) (apache.org) for more details about this range partitioner.

### Key differences

• When use coalesce function, data reshuffling doesn't happen as it creates a narrow dependency. Each current partition will be remapped to a new partition when action occurs.
• repartition function can also be used to change partition number of a dataframe. It can be used to increase or decrease partition numbers, which involves data shuffling. It can also be used to partition data by certain columns in the dataframe.
• repartitionByRange function can be used to repartition using range partitioner to create partitions that are roughly equal.

If the purpose is to reduce partition size to a smaller number without involving partitioning by dataframe column(s), I recommend using coalesce function to get potential better performance.

More from Kontext
{"ft_lang_label":"__label__en","ft_lang_prob":0.7824046,"math_prob":0.8305735,"size":2998,"snap":"2023-40-2023-50","text_gpt3_token_len":625,"char_repetition_ratio":0.23346694,"word_repetition_ratio":0.02112676,"special_character_ratio":0.18912609,"punctuation_ratio":0.11729623,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98498356,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-28T10:38:43Z\",\"WARC-Record-ID\":\"<urn:uuid:3e58a9b9-a876-400f-a630-feae11ae1040>\",\"Content-Length\":\"49636\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:17ece463-9d41-4081-9802-077fc56979c1>\",\"WARC-Concurrent-To\":\"<urn:uuid:78be80b8-ccf6-4fd3-82f6-2da6a3192d75>\",\"WARC-IP-Address\":\"20.11.96.38\",\"WARC-Target-URI\":\"https://kontext.tech/article/657/differences-between-repartition-and-coalesce-functions-in-spark\",\"WARC-Payload-Digest\":\"sha1:BL6ESHBDHLWM64QSNEICWCBKVHLGRXF2\",\"WARC-Block-Digest\":\"sha1:JSO4MVSRRGPACLIVDQV6BHZRNNJU5O2W\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510387.77_warc_CC-MAIN-20230928095004-20230928125004-00347.warc.gz\"}"}