Introduction

In this article, we will explore the different Data flow partition types in Azure Data Factory. Each partitioning type provides specific instructions to Spark on how to organize the data after each processing in the cluster. This is a crucial step in developing any data transformation as it allows us to optimize the performance of our Data flow pipeline.

Challenges

In our previous article, we learned how to view the processing time by stages and access the partition chart by enabling the verbose logging in the Data flow activity. Now, we need to review our data transformation to reduce the transformation time. In each Data flow components, we can change the partition type. Before we dive into the tutorial, we need to understand the 5 partition types:

  1. Round Robin: Evenly distribute the data across the specified number of partitions. Spark will re-partition the data if the number of partition changes. This is often used when we do not have a good column to distribute the data.
  2. Hash: We define the columns to be used by the hashing function. A hash value is generated based on the unique column values. Each hash value is mapped to exactly 1 partition but one or more hash value can be placed in a partition. The benefit of this method is to group all the columns with the same values together for downstream processing (like aggregation).
  3. Dynamic Range: We define the columns to be used to arrange the data. Spark will determine the range of values that can fit within each partition.
  4. Fixed Range: We define the specific conditions for partitioning the data. Spark will use this as part of the partition function.
  5. Key: The number of partitions is generated based on the columns defined. Each unique combination of values will result in one partition. This should only be used if the combinations are limited and are small.

Tutorial

1. Let's review the data flow we are using for this tutorial:

2. Both 'invoices' and 'invoiceLines' sources are partitioned using 'Use current partitioning'. When using with option with sources, the data is partitioned evenly across all the partitions. A new partition is created when the data is about 128 MB. Generally, this is the recommended setting for most sources.

As you can see, in our example, mapping data flow created only 1 partition because we don't have a lot of data,

3. Next, we will examine different partition optimization in the 'combineInvoiceWithLines' join element. First up is 'Round Robin':

​​​​​​​

4. The next option we have is 'Hash'. This is a popular option, especially if we have multiple columns:

5. Dynamic Range is very similar to Hash partition option but Spark will try to distribute the data evenly using the column values:

6. Fix Range requires us to define the rule for each partition:

7. The last option is Key. In this option, we do not pre-define the number of partitions. Instead, this is created based on the unique key values.

Summary

In conclusion, Mapping data flow provides five partition options for tuning. Some of the options are easier to use and more suitable for everyday use cases. The goal of repartitioning should be to reduce data movement in downstream processes. As a result, our overall stages' process time must be reduced.

Finally, here is a table summarizing the partition options and my opinion on when we should use it.

Partition type Data distribution Number of partitions When to use
Round Robin Evenly distributed User defined Default, when no good column for distribution.
Hash Based on values in hash columns User defined Multi-value aggregation with a well distributed datasets
Dynamic Range Based on values in the range columns, Spark will try to balance the underutilized partitions User defined Single or Multi-value aggregation. The dataset might not well distributed.
Fixed Range Based on the defined conditions User defined Full control over how the data is distributed. Requires good knowledge of the dataset.
Key Based on unique values in the key columns Spark generated When each unique value needs to be in their own partition. Only should be used when the unique values are known and small.

Happy Learning!

References