ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 解释员执行Hook(实验) 原文链接 : [http://zeppelin.apache.org/docs/0.7.2/manual/interpreterexechooks.html](http://zeppelin.apache.org/docs/0.7.2/manual/interpreterexechooks.html) 译文链接 : [http://www.apache.wiki/pages/viewpage.action?pageId=10030757](http://www.apache.wiki/pages/viewpage.action?pageId=10030757) 贡献者 : [片刻](/display/~jiangzhonglian) [ApacheCN](/display/~apachecn) [Apache中文网](/display/~apachechina) ## 概述 Apache Zeppelin允许用户在前后段代码执行时指定由解释器执行的附加代码。如果您需要在特定时间为笔记本中的所有段落运行相同的代码集,这主要是有用的。目前,此功能仅适用于spark和pyspark解释器。要指定你的Hook代码,你可以使用`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 Hook注册表系统的有效的事件代码。 最后,hook注册表由同一组中的其他解释器内部共享。这将允许一个解释器REPL的钩子代码由另一个解释器设置,如下所示: ``` %spark z.unregisterHook("post_exec", "pyspark")  ``` 该API对于spark(scala)和pyspark(python)实现是相同的。 ### 注意事项 打电话`z.registerHook("pre_exec", ...)`应小心。如果您指定的钩子代码有错误,这将导致解释器REPL无法执行任何代码通过预执行阶段,使得直接调用不可能`z.unregisterHook()`生效。当前的解决方法包括`z.unregisterHook()`通过同一解释器组(见上文)中的其他解释器REPL进行调用,或者在UI中手动重新启动解释器组。