A scenario for a data engineer · Junior and up
The cleaning step takes most of the run
Tensor · Data science
Thursday, 14:00
A Databricks job spends forty minutes of every hour in one step: a Python function, run row by row as a Spark UDF, that trims names and parses dates.
What do you do?
Tap the one you would do. The answer opens below.
Show the best callThe best call
Replace the function with built-in functions such as trim and to_date.
The work runs inside the engine, with no trip to Python for each row. The step drops to a few minutes.
Why
Built-in functions run inside the query engine and are optimized with the rest of the query. A Python UDF sends rows to a Python process and back. Use a UDF only when no built-in function does the job, and prefer a pandas UDF then.
Read it in the Databricks docs (opens in a new tab)The other two
- Works, at a price. Rewrite it as a pandas UDF, which handles rows in batches.
- Several times faster than row by row. Still slower than built-in functions, and still code you maintain.
- A poor call. Add workers until the step is fast enough.
- It gets faster, and each row costs as much as before. You are paying more machines to do slow work.
The words in this scenario
- UDF.
- A user-defined function: your own code, called from a query for the rows it works on. In the docs (opens in a new tab)
Go deeper on brickster.ai
- Apache Spark: what changed recentlyNews, releases, videos and questions on it, gathered daily.
- Drill Spark in PracticeShort questions with the reason after each answer.
More scenarios
- The nightly job no longer fits in the night · data engineer
- Twenty dashboards, one heavy query · data engineer
- The MERGE takes two hours · data engineer
- All 47 scenarios
Had one like this at work? Send it to us: the best tickets come from real incidents.
Checked against the Databricks documentation on . brickster.ai is independent and is not affiliated with Databricks.