包单元Package cells

若要在 Spark 中以及跨笔记本会话中可靠地使用笔记本中定义的自定义 Scala 类和对象,应在包单元中定义类。To use custom Scala classes and objects defined within notebooks reliably in Spark and across notebook sessions, you should define classes in package cells. “包单元”是在运行时编译的单元。A package cell is a cell that is compiled when it is run. 包单元对于笔记本的其余部分不可见。A package cell has no visibility with respect to the rest of the notebook. 可以将其视为单独的 Scala 文件。You can think of it as a separate Scala file. 只有 classobject 定义可以放入包单元。Only class and object definitions can go in a package cell. 不能放入任何值、变量或函数定义。You cannot have any values, variables, or function definitions.

以下笔记本显示了如果不使用包单元会发生什么情况,并提供了一些示例、注意事项和最佳做法。The following notebook shows what can happen if you do not use package cells and provides some examples, caveats, and best practices.

包单元笔记本Package Cells notebook

获取笔记本Get notebook