The Reference You Need
Spark Scala Examples
Simple spark scala examples to help you quickly complete your data etl pipelines. Save time digging through the spark scala function api and instead get right to the code you need...
Page 5 of 9
-
Current Date and Timestamp in Spark Scala: current_date, current_timestamp, localtimestamp, curdate, now, current_timezone
Spark provides a small family of functions for getting the current date, time, and session timezone — useful for tagging records with a load time, calculating ages, or filtering on "today". They all return the value at the start of query evaluation, so every call within a single query sees the same value.
-
elt in Spark Scala: Pick the N-th Value from a List of DataFrame Columns
elt returns the n-th value from a list of column expressions, where n is a 1-based index. It's handy when you have an integer column that points at one of several sibling columns (or literal values) and you want to materialize the chosen value into a single result column.
-
bin and conv in Spark Scala: Number Base Conversion in DataFrames
bin returns the binary string representation of a long integer. conv is the more general tool — it converts a number string from one base to another, covering decimal, hex, octal, binary, or anything in between. Use them when you're working with bit flags, color codes, or any data where the representation matters as much as the value.
-
Advanced Regex Functions in Spark Scala: regexp_count, regexp_extract_all, regexp_instr, regexp_like, regexp_substr
Beyond the familiar regexp_replace and regexp_extract, Spark 3.4 ships a set of regex helpers — regexp_count, regexp_extract_all, regexp_instr, regexp_like, and regexp_substr — that cover the common "count / extract-all / locate / test / grab-first" jobs that otherwise require chaining helpers or writing UDFs. They're SQL-only, so you reach them through expr() in Spark Scala.
-
url_encode, url_decode, and parse_url in Spark Scala: Work With URLs in a DataFrame
url_encode converts a string into application/x-www-form-urlencoded format so it can be safely used in a URL. url_decode reverses that transformation. parse_url extracts pieces of a URL — the host, path, query string, or a specific query parameter. All three are Spark SQL functions, so you call them through expr().
-
mask in Spark Scala: Mask Sensitive Data in a DataFrame Column
mask replaces characters in a string with masking characters — uppercase letters become X, lowercase letters become x, and digits become n by default. It's a quick way to redact sensitive data like emails, phone numbers, and identifiers without destroying the structure of the value.
-
btrim in Spark Scala: Trim Both Sides of a String in a DataFrame Column
btrim strips characters from both ends of a string. It's the SQL-standard equivalent of trim — useful when you're writing Spark SQL expressions or prefer the more explicit name.
-
decode and encode in Spark Scala: Convert Between Strings and Binary in DataFrames
encode converts a string column to binary using a specified character set. decode does the reverse — it converts binary data back to a string. Together they let you move between string and binary representations, which is useful when working with systems that expect raw bytes or when you need to control the character encoding explicitly.
-
Format String in Spark Scala: Printf-Style Column Formatting for DataFrames
The format_string function formats column values into a string using printf-style patterns. It's useful for building human-readable labels, combining columns into structured text, or formatting numbers inline without changing their type first.
-
find_in_set in Spark Scala: Search Comma-Delimited Strings in a DataFrame
find_in_set returns the 1-based position of a string within a comma-delimited list stored in another column. It returns 0 if the string isn't found and null if either input is null.