在notebook2中使用notebook1的输出
我在 notebook2
中定义了一个类,这个类需要从 notebook1
中获取一些参数。为此,我使用了 dbutils.notebook.run(path, 60, {"CATALOG": f"{CATALOG}"})
这个命令来调用 notebook2
。需要注意的是,使用 %run
的时候,我无法通过小部件传递参数。
现在,我在传递参数方面没有问题,但我在我的类中定义的函数想在 notebook1
中使用,但我却做不到。根据我的理解,dbutils.notebook.run
会在 notebook2
中启动一个新的 Spark 会话,这就是我收到错误提示 name 'gpg_encryption' is not defined
的原因。
这是我正在做的事情:
1. 运行 notebook1
dbutils.notebook.run(path, 60, {"CATALOG": f"{CATALOG}", "STORAGEACCOUNT_NAME": f"{STORAGEACCOUNT_NAME}"})
2.
上面的代码触发了 notebook2
,并且在里面定义了类。
class gpg_encryption(gnupg.GPG):
"""
Expands the GnuPG library:
- Importing of BASE64 encoded keys.
- Decrypting csv files to pandas dataframes.
- Using simplified key-management.
On databricks clusters.
"""
asc_key_path = f'/Volumes/{CATALOG}/bronze/private'
def __init__(self):
self.gpg = gnupg.GPG()
self.gpg.encoding = 'utf-8'
self.create_key_path_dir()
def create_key_path_dir(self):
"""
Creates a folder to store keyfiles.
"""
if not os.path.exists(self.asc_key_path):
spark.sql(f"CREATE EXTERNAL VOLUME lakehouse_dev.bronze.private LOCATION 'abfss://bronze@{storage_account}.dfs.core.windows.net/private'")
3. 在类定义后,在同一个笔记本(notebook2
)中,运行这个命令以获取类的输出到 notebook1
dbutils.notebook.exit(gpg_encryption)
4. 最后一步
在 notebook1
中运行
gpg_handler = gpg_encryption()
但我遇到了错误
name 'gpg_encryption' is not defined
1 个回答
我刚刚扩展了我的 init
函数,具体如下:
def __init__(self, storage_account, catalog):
self.gpg = gnupg.GPG()
self.gpg.encoding = 'utf-8'
self.storage_account = storage_account
self.catalog = catalog
self.asc_key_path = f'/Volumes/{self.catalog}/bronze/private'
self.create_key_path_dir()
然后我使用 %run
来运行这个笔记本,接着在调用这个类的时候传入参数 gpg_handler = gpg_encryption(catalog = CATALOG, storage_account=STORAGEACCOUNT_NAME )
不过如果有人能解释一下,怎么按照我问题中提到的方式或者其他方式来实现,那就太好了。