Skip to main content

Get task context in a UDF

Use the TaskContext PySpark API to get context information while running a Batch Unity Catalog Python UDF or PySpark UDF.

For example, context information such as the user's identity and cluster tags can verify a user's identity to access external services.

Requirements

Use TaskContext to get context information

Select a tab to see TaskContext examples for PySpark UDFs or Batch Unity Catalog Python UDFs .

The following PySpark UDF example prints the user's context:

Python
@udf
def log_context():
import json
from pyspark.taskcontext import TaskContext
tc = TaskContext.get()

# Returns current user executing the UDF
session_user = tc.getLocalProperty("user")

# Returns cluster tags
tags = dict(item.values() for item in json.loads(tc.getLocalProperty("spark.databricks.clusterUsageTags.clusterAllTags ") or "[]"))

# Returns current version details
current_version = {
"dbr_version": tc.getLocalProperty("spark.databricks.clusterUsageTags.sparkVersion"),
"dbsql_version": tc.getLocalProperty("spark.databricks.clusterUsageTags.dbsqlVersion")
}

return {
"user": session_user,
"job_group_id": job_group_id,
"tags": tags,
"current_version": current_version
}

TaskContext properties

The TaskContext.getLocalProperty() method has the following property keys:

Property Key

Description

Example Usage

user

The user currently executing the UDF

tc.getLocalProperty("user")

-> "alice"

spark.jobGroup.id

The Spark job group ID associated with the current UDF

tc.getLocalProperty("spark.jobGroup.id")

-> "jobGroup-92318"

spark.databricks.clusterUsageTags.clusterAllTags

Cluster metadata tags as key-value pairs formatted as a string representation of a JSON dictionary

tc.getLocalProperty("spark.databricks.clusterUsageTags.clusterAllTags")

-> [{"Department": "Finance"}]

spark.databricks.clusterUsageTags.region

The region where the workspace resides

tc.getLocalProperty("spark.databricks.clusterUsageTags.region")

-> "us-west-2"

accountId

Databricks account ID for the running context

tc.getLocalProperty("accountId")

-> "1234567890123456"

orgId

Workspace ID (not available on DBSQL)

tc.getLocalProperty("orgId")

-> "987654321"

spark.databricks.clusterUsageTags.sparkVersion

Databricks Runtime version for the cluster (on non-DBSQL environments)

tc.getLocalProperty("spark.databricks.clusterUsageTags.sparkVersion")

-> "16.3"

spark.databricks.clusterUsageTags.dbsqlVersion

DBSQL version (on DBSQL environments)

tc.getLocalProperty("spark.databricks.clusterUsageTags.dbsqlVersion")

-> "2024.35"