Lesson reference: PySpark and Scala
Clean product names
The setup pads product names with spaces and builds a SKU reference from product_id. trim removes surrounding spaces; lower then normalizes case. Internal spaces remain, so Wireless keyboard stays two words. The original catalog is unchanged.
PySpark example
from pyspark.sql.functions import (
col, lit, concat, trim, lower, upper, regexp_extract,
format_string, to_date, to_timestamp, datediff
)
prepared_products = products.select(
"product_id",
concat(lit(" "), col("product_name"), lit(" ")).alias("raw_name"),
concat(lit("SKU-"), col("product_id").cast("string")).alias("reference")
)
result = prepared_products.select(
col("product_id"), lower(trim(col("raw_name"))).alias("clean_name")
).orderBy("product_id")
result.show()
Scala example
import org.apache.spark.sql.functions._
val prepared_products = products.select(
col("product_id"),
concat(lit(" "), col("product_name"), lit(" ")).alias("raw_name"),
concat(lit("SKU-"), col("product_id").cast("string")).alias("reference")
)
val result = prepared_products.select(
col("product_id"), lower(trim(col("raw_name"))).alias("clean_name")
).orderBy("product_id")
result.show()
Extract a SKU
regexp_extract uses a regular expression and a capture-group number. Group 1 is the SKU prefix; group 2 is the numeric suffix. The extracted value is text, not a number. An unmatched pattern returns an empty string.
PySpark example
from pyspark.sql.functions import (
col, lit, concat, trim, lower, upper, regexp_extract,
format_string, to_date, to_timestamp, datediff
)
prepared_products = products.select(
"product_id",
concat(lit(" "), col("product_name"), lit(" ")).alias("raw_name"),
concat(lit("SKU-"), col("product_id").cast("string")).alias("reference")
)
result = prepared_products.select(
col("product_id"),
regexp_extract(col("reference"), "([A-Z]+)-([0-9]+)", 1).alias("reference_part")
).orderBy("product_id")
result.show()
Scala example
import org.apache.spark.sql.functions._
val prepared_products = products.select(
col("product_id"),
concat(lit(" "), col("product_name"), lit(" ")).alias("raw_name"),
concat(lit("SKU-"), col("product_id").cast("string")).alias("reference")
)
val result = prepared_products.select(
col("product_id"),
regexp_extract(col("reference"), "([A-Z]+)-([0-9]+)", 1).alias("reference_part")
).orderBy("product_id")
result.show()
Match a name prefix
like uses SQL-style wildcards: % matches any sequence and _ matches one character. This example trims for comparison and selects names starting with L. The returned raw_name still has its original spaces. Matching is case-sensitive.
PySpark example
from pyspark.sql.functions import (
col, lit, concat, trim, lower, upper, regexp_extract,
format_string, to_date, to_timestamp, datediff
)
prepared_products = products.select(
"product_id",
concat(lit(" "), col("product_name"), lit(" ")).alias("raw_name"),
concat(lit("SKU-"), col("product_id").cast("string")).alias("reference")
)
result = prepared_products.filter(
trim(col("raw_name")).like("L%")
).select("product_id", "raw_name").orderBy("product_id")
result.show()
Scala example
import org.apache.spark.sql.functions._
val prepared_products = products.select(
col("product_id"),
concat(lit(" "), col("product_name"), lit(" ")).alias("raw_name"),
concat(lit("SKU-"), col("product_id").cast("string")).alias("reference")
)
val result = prepared_products.filter(
trim(col("raw_name")).like("L%")
).select("product_id", "raw_name").orderBy("product_id")
result.show()