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 4 of 9
-
timestamp_seconds, timestamp_millis, timestamp_micros, unix_seconds, unix_millis, unix_micros in Spark Scala: Convert Between Epoch Numbers and Timestamps in a DataFrame
Six paired functions convert between Unix epoch numbers and timestamp columns at three precision levels: timestamp_seconds, timestamp_millis, and timestamp_micros go from a numeric column to a timestamp, and unix_seconds, unix_millis, and unix_micros go the other way. Reach for them when you're ingesting long epoch values from Kafka, log files, or APIs and need a real timestamp type to filter, format, or join on — or when you need to emit timestamps as numeric values for downstream systems.
-
unix_timestamp and from_unixtime in Spark Scala: Convert Between Strings and Unix Epoch Seconds in a DataFrame
unix_timestamp and from_unixtime are the bridge between human-readable timestamp strings and Unix epoch seconds. Reach for them when integrating with systems that store time as a long — Kafka payloads, log files, REST APIs — or when you need to do timestamp math that's easier in seconds than in calendar fields. to_unix_timestamp is the SQL-only sibling of unix_timestamp, called through expr().
-
to_date and to_timestamp in Spark Scala: Parse Strings into Date and Timestamp DataFrame Columns
to_date and to_timestamp parse string columns into proper date and timestamp types. They're the functions you reach for whenever raw data lands as text — CSV files, JSON payloads, or upstream queries — and you need to do anything date-related with it. Spark 3.4 also adds to_timestamp_ntz and to_timestamp_ltz for explicit time-zone semantics.
-
date_trunc and trunc in Spark Scala: Truncate Date and Timestamp Columns in a DataFrame
date_trunc and trunc round a date or timestamp column down to a coarser unit — the start of the hour, day, month, quarter, or year. They're the go-to functions for bucketing events into time windows for grouping, partitioning, or aligning data to a calendar boundary.
-
date_format in Spark Scala: Format Date and Timestamp Columns in a DataFrame
date_format converts a date, timestamp, or string column into a formatted string column using a pattern made of letters like yyyy, MM, dd, HH, and EEEE. Use it whenever you need a human-readable date label, a custom export format, or a partition-friendly column like 2026-01.
-
datediff and date_diff in Spark Scala: Days Between Two Dates in a DataFrame
datediff returns the number of days between two date columns. It's the go-to function for computing things like "days to ship", "days since signup", or "age in days". Spark 3.4 added a SQL-only alias date_diff that does the same thing — useful when you're writing a SQL expression but otherwise interchangeable.
-
date_add, date_sub, and add_months in Spark Scala: Shift Dates in a DataFrame
date_add, date_sub, and add_months shift a date column forward or backward by a fixed number of days or months. They're the bread-and-butter functions for computing things like "30 days from order date", "one month before renewal", or "next billing cycle". Spark 3.4 added a one-word alias, dateadd, that's only reachable through expr().
-
date_part, datepart, and extract in Spark Scala: Generic Date Part Extraction
date_part, datepart, and extract are generic ways to pull a single field out of a date, timestamp, or interval column. They cover the same ground as the dedicated functions like year, month, dayofmonth and hour, minute, second, but with one function call where the field name is a parameter — useful when the field you need is decided at runtime or driven by config.
-
Hour, Minute, Second Extraction in Spark Scala: hour, minute, second
Spark provides hour, minute, and second for pulling the time-of-day components out of a timestamp column. They're the time-side counterparts to year, month, and day and are useful for bucketing events by hour of day, filtering work hours, or building time-based features.
-
Year, Month, Day Extraction in Spark Scala: year, month, dayofmonth, dayofweek, dayofyear, weekofyear, quarter
Spark provides a small family of functions for pulling individual date parts — year, month, day, week, quarter — out of a date or timestamp column. They're the building blocks for grouping by month, filtering by quarter, or partitioning a table by year.