本文介绍了NameError:名称"dbutils"未在pyspark中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在databricks云中运行pyspark作业.作为这项工作的一部分,我需要将一些csv文件写入到databricks文件系统(dbfs)中,并且还需要使用某些dbutils本机命令,例如,
I am running a pyspark job in databricks cloud. I need to write some of the csv files to databricks filesystem (dbfs) as part of this job and also i need to use some of the dbutils native commands like,
#mount azure blob to dbfs location
dbutils.fs.mount (source="...",mount_point="/mnt/...",extra_configs="{key:value}")
将文件写入装载目录后,我还要尝试卸载.但是,当我直接在pyspark作业中使用dbutils时,它会失败
I am also trying to unmount once the files has been written to the mount directory. But, when i am using dbutils directly in the pyspark job it is failing with
NameError: name 'dbutils' is not defined
我应该导入任何软件包以在pyspark代码中使用dbutils吗?预先感谢.
Should i import any of the package to use dbutils in pyspark code ? Thanks in advance.
推荐答案
尝试使用此功能:
def get_dbutils(spark):
try:
from pyspark.dbutils import DBUtils
dbutils = DBUtils(spark)
except ImportError:
import IPython
dbutils = IPython.get_ipython().user_ns["dbutils"]
return dbutils
dbutils = get_dbutils(spark)
这篇关于NameError:名称"dbutils"未在pyspark中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!