解释器执行钩子(实验性)

概述

Apache Zeppelin 允许用户在段落代码执行前后指定由解释器执行的额外代码。 如果您需要在特定时间为笔记本中的所有段落运行相同的代码集,这主要是有用的。 目前,此功能仅适用于 spark 和 pyspark 解释器。 要指定您的钩子代码,您可以使用 z.registerHook()。 例如,在一个段落中输入以下内容:

%pyspark
z.registerHook("post_exec", "print 'This code should be executed before the parapgraph code!'")
z.registerHook("pre_exec", "print 'This code should be executed after the paragraph code!'")

这些调用将在您下次运行段落时生效。

在另一个段落中,输入

%pyspark
print "This code should be entered into the paragraph by the user!"

输出应为:

This code should be executed before the paragraph code!
This code should be entered into the paragraph by the user!
This code should be executed after the paragraph code!

如果你需要知道钩子代码,请使用 z.getHook()

%pyspark
print z.getHook("post_exec")

print 'This code should be executed after the paragraph code!'

任何对z.registerHook()的调用都会自动覆盖之前注册的内容。 要完全取消注册一个钩子事件,请使用z.unregisterHook(eventCode)。 目前只有"post_exec""pre_exec"是Zeppelin钩子注册系统有效的事件代码。

最后,钩子注册表在同一个组中的其他解释器之间内部共享。这将允许一个解释器 REPL 的钩子代码由另一个解释器设置,如下所示:

%spark
z.unregisterHook("post_exec", "pyspark")

API 对于 spark (scala) 和 pyspark (python) 的实现是相同的。

注意事项

调用z.registerHook("pre_exec", ...)时应小心。如果您指定的钩子代码中存在错误,这将导致解释器REPL无法执行任何通过预执行阶段的代码,使得直接调用z.unregisterHook()无法生效。当前的解决方法包括从同一解释器组中的不同解释器REPL调用z.unregisterHook()(见上文)或在UI中手动重启解释器组。