本文介绍了如何在pyspark中加载databricks软件包dbutils的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在pyspark中运行以下代码。

I was trying to run the below code in pyspark.

dbutils.widgets.text('config', '', 'config')

这让我犯了一个错误:

 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 NameError: name 'dbutils' is not defined

所以,有什么办法可以通过包含databricks包在pyspark中运行它,就像导入一样?

so, Is there any way I can run it in pyspark by including the databricks package ,like an import ?

感谢您的帮助

推荐答案

,如

取决于您直接在databricks服务器上执行代码的位置(例如,使用databricks笔记本调用您的项目egg文件),还是从IDE使用databricks-connect来执行代码,应按以下方式初始化dbutils。 (其中spark是您的SparkSession)

depending on where you are executing your code directly on databricks server (eg. using databricks notebook to invoke your project egg file) or from your IDE using databricks-connect you should initialize dbutils as below. (where spark is your SparkSession)

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)

这篇关于如何在pyspark中加载databricks软件包dbutils的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 08:53