检索笔记本的当前用户名Retrieve the current username for the notebook

备注

Azure Databricks 不支持这些 API 调用,可以随时更改或删除它们。These API calls are not supported by Azure Databricks and can change or be removed at any time.

问题Problem

正在运行笔记本,但不知道当前用户名。You are running a notebook, but do not know the name of the current user.

解决方案Solution

运行交互式笔记本时,可以使用 dbutils.notebook.getContext.tags 获取当前用户名。You can use dbutils.notebook.getContext.tags to obtain the current username when running an interactive notebook.

dbutils.notebook.getContext.tags("user")
dbutils.notebook.entry_point.getDbutils().notebook().getContext().tags().apply('user')

将笔记本作为作业运行时,不能直接使用 dbutils.notebook.getContext.tagsWhen running a notebook as a job, you cannot use dbutils.notebook.getContext.tags directly. 应改为使用笔记本小组件,将用户名作为作业参数显式传递,并从单元格访问小组件的值。Instead, you should use a notebook widget, pass the username explicitly as a job parameter, and access the widget’s value from a cell.

dbutils.widgets.get("job_user")