Skip to main content
PYSPARK โ€ข LESSON 42

Injecting Constant Literals with lit()

How do we append a constant environment tag to every record using the lit() function?

Intermediateโฑ 2 Minutesโญ 260 XP
๐Ÿค” THE QUESTION

How do we append a constant environment tag to every record using the lit() function?

๐Ÿ’ก WHAT IS IT?

lit() converts a primitive Python string, integer, or float into a Spark Column literal.

๐ŸŽฏ WHAT IS IT USED FOR?

Tagging pipeline source systems, environment identifiers, partition keys, and audit metadata.

๐Ÿ’ป EXAMPLE
from pyspark.sql.functions import lit

df = df.withColumn("environment", lit("PRODUCTION"))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Injecting Constant Literals with lit().

  • โœ“ Import lit function
  • โœ“ Create Column literal value
  • โœ“ Append constant environment tag