Get Current User in Databricks

There is a function starting from DBR 7.3 rated as a dynamic view function to do just that - current_user. SQL users are lucky as they can just call it from their script i.e. select current_user as user. Unforutnately, it’s not available from Python/Scala, but one can wrap it in a function like this:

def get_current_user_name():
  return (spark
          .createDataFrame([("",)], "user string")
          .withColumn("user", f.expr("current_user"))

Also, unfortunately, this function will only work if spark config has the following setting on the cluster:

spark.databricks.userInfoFunctions.enabled true

Have a question⁉ Contact me.