在高并发集群上的 Databricks 中获取笔记本内的用户名?

Sid*_*ain 9 databricks

在尝试在高并发集群上获取用户数据时,我遇到了这个问题。我正在使用下面的命令来获取用户详细信息

dbutils.notebook.entry_point.getDbutils().notebook().getContext().tags().apply('用户')

以下是运行的错误日志。任何帮助将非常感激。

Py4JError: An error occurred while calling o475.tags. Trace:
py4j.security.Py4JSecurityException: Method public scala.collection.immutable.Map com.databricks.backend.common.rpc.CommandContext.tags() is not whitelisted on class class com.databricks.backend.common.rpc.CommandContext
    at py4j.security.WhitelistingPy4JSecurityManager.checkCall(WhitelistingPy4JSecurityManager.java:409)
    at py4j.Gateway.invoke(Gateway.java:294)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:251)
    at java.lang.Thread.run(Thread.java:748)
Run Code Online (Sandbox Code Playgroud)

小智 18

您可以使用 dbutils 命令检索信息:

dbutils.notebook.entry_point.getDbutils().notebook().getContext().userName().get()
Run Code Online (Sandbox Code Playgroud)


Ark*_*ady 6

我一直在用这个:

user_id = spark.sql('select current_user() as user').collect()[0]['user']
Run Code Online (Sandbox Code Playgroud)

current_user()是Databricks 中记录的 SQL 函数


小智 -1

这是一个很糟糕的解决方法,但我还没有找到更好的方法。

import uuid
import shutil

# Create a unique temporary table location
tmpTable = f'/tmp/identifier/{uuid.uuid4()}'

# Write a single line to a delta format table.
spark.range(1).write.format('delta').save(tmpTable)

# Extract the username from the delta history 
username = spark.sql(f'DESCRIBE HISTORY delta.`{tmpTable}`').select('userName').collect()[0]['userName']

# Delete the temporary table
shutil.rmtree('/dbfs'+tmpTable)
Run Code Online (Sandbox Code Playgroud)