Let’s Get Certified
Module 08 / 13 · DataFrame and Dataset APIs

Choose UDFs, shared variables and join strategies

Decide what executes per row, what is distributed once and where keyed state persists.

Choose UDFs, shared variables and join strategies

Work through the decision

A job ships a Python dictionary to every task and updates a driver variable in a Python UDF, expecting a global count. At the same time it joins a small station dimension to a much larger events table. Which operations should the engineer separate?

Check your reasoning

Use built-in column functions when they express the rule. A scalar Python UDF can compute a value from the input but its task-local variables do not form reliable distributed state. Use an explicit grouped aggregation for business counts. A broadcast variable distributes a small read-only object. An accumulator is useful for diagnostic totals with execution and retry semantics that must be understood.

Watch the source lesson at 1:34:56

Compare the choices

Scalar UDF
Define a typed user function for logic unavailable in built-in expressions. Account for serialization, null handling and optimizer visibility.
Stateful streaming operator
Use supported keyed state APIs or built-in streaming operations when a result depends on records from earlier batches. Persist query progress through checkpoints.
Broadcast variable
Distribute a small, read-only object to workers for reuse within tasks. Treat the broadcast contents as immutable on the workers.
Accumulator
Collect supported add-only diagnostic updates from workers. Avoid using task-side counters as the authoritative business ledger across recomputations.
Broadcast join
Broadcast a sufficiently small eligible relation for a join and inspect the physical plan. Confirm the build side fits each executor and semantics permit the strategy.

Test the decision

A broadcast join gives workers a small relation so eligible joins can avoid repartitioning the large side. It is a query-planning strategy separate from a broadcast variable passed to Python code. A keyed streaming state store tracks state across batches for compatible stateful operators. Give that state a retention policy and a checkpoint.

Inspect a broadcast join

from pyspark.sql import functions as F
events = spark.range(100).selectExpr("id", "id % 2 AS route")
routes = spark.createDataFrame([(0, "red"), (1, "blue")], "route long, line string")
events.join(F.broadcast(routes), "route").explain("formatted")
Check the result

Look for BroadcastExchange on the lookup and BroadcastHashJoin for the equality match. Change the data or remove the hint and compare the actual plan. An available join strategy and a safe build-side size are both necessary.

Practice the guide skills

  1. Decide whether a per-row transformation needs a UDF, and contrast its lifecycle with a state store that persists streaming keys. Watch 1:34:56 · Continue at 1:36:40 · Continue at 1:37:01
  2. Choose a read-only broadcast lookup or a task-updated accumulator for each of two driver-side requirements. Watch 1:38:00 · Continue at 1:38:20
  3. Broadcast the bounded side of an equality join and look for the physical join operator in the plan. Watch 1:32:14 · Continue at 1:32:30

Official objectives for this lesson

Related lesson · Compute and cluster types

Knowledge check

Choose UDFs, shared variables and join strategies quiz

Answer 3 questions. Check each answer for an explanation.