使用筆記本Use notebooks

筆記本是) (命令的可執行資料格集合。A notebook is a collection of runnable cells (commands). 當您使用筆記本時,主要是開發和執行儲存格。When you use a notebook, you are primarily developing and running cells.

UI 動作支援所有筆記本工作,但您也可以使用鍵盤快速鍵來執行許多工。All notebook tasks are supported by UI actions, but you can also perform many tasks using keyboard shortcuts. 按一下  鍵盤圖示 圖示或選取 [ ? > 快速鍵],切換快捷方式顯示。Toggle the shortcut display by clicking the Keyboard Icon icon or selecting ? > Shortcuts.

鍵盤快速鍵Keyboard shortcuts

開發筆記本 Develop notebooks

本節說明如何開發筆記本儲存格,並在筆記本周圍流覽。This section describes how to develop notebook cells and navigate around a notebook.

本節內容:In this section:

關於筆記本About notebooks

筆記本中的工具列可讓您管理筆記本,並在筆記本內執行動作:A notebook has a toolbar that lets you manage the notebook and perform actions within the notebook:

筆記本工具列Notebook toolbar

以及您可以執行的一或多個資料格 (或命令) :and one or more cells (or commands) that you can run:

筆記本儲存格Notebook cells

在資料格的最右邊,資料格動作資料  格動作 包含三個功能表: 執行儀表板編輯At the far right of a cell, the cell actions Cell actions, contains three menus: Run, Dashboard, and Edit:

執行圖示 儀表板 編輯

還有兩個動作:隱藏and two actions: Hide 儲存格最小化 和刪除and Delete 刪除圖示..

新增儲存格Add a cell

若要加入儲存格,請將滑鼠游標移到頂端或底部的資料格上方,然後按一下 ![ 加入資料格] 圖示,或存取最右邊的筆記本儲存格功能表,按一下 ![ 向下] ,然後選取 [ 加入儲存格 ] 或下方的 [ 加入 儲存格]。To add a cell, mouse over a cell at the top or bottom and click the Add Cell icon, or access the notebook cell menu at the far right, click Down Caret, and select Add Cell Above or Add Cell Below.

刪除儲存格Delete a cell

移至 [資料格動作] 功能表Go to the cell actions menu 資料格動作 在最右邊,按一下at the far right and click 刪除圖示 (刪除) 。(Delete).

當您刪除資料格時,預設會顯示 [刪除確認] 對話方塊。When you delete a cell, by default a delete confirmation dialog displays. 若要停用未來確認對話方塊,請選取 [ 不要再顯示 ] 核取方塊,然後按一下 [ 確認]。To disable future confirmation dialogs, select the Do not show this again checkbox and click Confirm. 您也可以切換確認對話方塊設定,並在  帳戶圖示 > 使用者設定 > 筆記本設定 中開啟命令刪除確認選項。You can also toggle the confirmation dialog setting with the Turn on command delete confirmation option in Account Icon > User Settings > Notebook Settings.

若要還原已刪除的儲存格,請選取 [ 編輯] > 恢復刪除資料格 ],或使用 (Z) 鍵盤快速鍵。To restore deleted cells, either select Edit > Undo Delete Cells or use the (Z) keyboard shortcut.

剪下資料格Cut a cell

移至最右邊的資料格動作功能表資料  格動作 ,按一下 ![ 向下鍵] ,然後選取 [ 下資料格]。Go to the cell actions menu Cell actions at the far right, click Down Caret, and select Cut Cell.

您也可以使用 (X) 鍵盤快速鍵。You can also use the (X) keyboard shortcut.

若要還原已刪除的儲存格,請選取 [ 編輯] > 復原剪 下資料格],或使用 (Z) 鍵盤快速鍵To restore deleted cells, either select Edit > Undo Cut Cells or use the (Z) keyboard shortcut.

選取多個資料格或所有資料格Select multiple cells or all cells

您可以使用 + 上一個和下一個資料格的 向上向下 移動,來選取連續的筆記本儲存格。You can select adjacent notebook cells using Shift + Up or Down for the previous and next cell respectively. 您可以複製、剪下、刪除和貼上多重選取的資料格。Multi-selected cells can be copied, cut, deleted, and pasted.

若要選取所有資料格,請選取 [ 編輯] > 選取 [所有資料格 ],或使用命令模式快速鍵 Cmd + ATo select all cells, select Edit > Select All Cells or use the command mode shortcut Cmd+A.

預設語言Default language

每個資料格的預設語言會顯示在筆記本名稱旁邊的 () 連結中。The default language for each cell is shown in a () link next to the notebook name. 在下列筆記本中,預設語言為 SQL。In the following notebook, the default language is SQL.

筆記本預設語言Notebook default language

若要變更預設語言:To change the default language:

  1. 按一下 [ (] ) 連結。Click () link. [變更預設語言] 對話方塊隨即顯示。The Change Default Language dialog displays.

    筆記本預設語言Notebook default language

  2. 從 [ 預設語言 ] 下拉式清單中選取新的語言。Select the new language from the Default Language drop-down.

  3. 按一下 [變更]。Click Change.

  4. 為了確保現有的命令仍能繼續運作,之前的預設語言的命令會自動加上語言魔術命令的前置詞。To ensure that existing commands continue to work, commands of the previous default language are automatically prefixed with a language magic command.

混合語言 Mix languages

您可以 %<language> 在資料格的開頭指定語言魔術命令,以覆寫預設語言。You can override the default language by specifying the language magic command %<language> at the beginning of a cell. 支援的魔術命令為: %python%r%scala%sqlThe supported magic commands are: %python, %r, %scala, and %sql.

注意

當您叫用語言魔術命令時,會將命令分派至筆記本 執行內容 中的複寫。When you invoke a language magic command, the command is dispatched to the REPL in the execution context for the notebook. 以一種語言定義的變數 (,因此在另一種語言的複寫中,將無法使用該語言) 的複製。Variables defined in one language (and hence in the REPL for that language) are not available in the REPL of another language. Repl 只能透過外部資源(例如 DBFS 中的檔案或物件儲存體中的物件)共用狀態。REPLs can share state only through external resources such as files in DBFS or objects in object storage.

筆記本也支援一些輔助魔術命令:Notebooks also support a few auxiliary magic commands:

  • %sh:可讓您在筆記本中執行 shell 程式碼。%sh: Allows you to run shell code in your notebook. 若要在 shell 命令具有非零的結束狀態時將儲存格失效,請新增 -e 選項。To fail the cell if the shell command has a non-zero exit status, add the -e option. 此命令只會在 Apache Spark 驅動程式上執行,而不會在背景工作角色上執行。This command runs only on the Apache Spark driver, and not the workers. 若要在所有節點上執行 shell 命令,請使用 init 腳本To run a shell command on all nodes, use an init script.
  • %fs:可讓您使用 dbutils 檔案系統命令。%fs: Allows you to use dbutils filesystem commands. 請參閱 dbutilsSee dbutils.
  • %md:可讓您包含各種類型的檔,包括文字、影像,以及數學公式和方程式等。%md: Allows you to include various types of documentation, including text, images, and mathematical formulas and equations. 請參閱下一節。See the next section.

包含檔Include documentation

若要在筆記本中包含檔,您可以使用 %md 魔術命令來識別 Markdown 標記。To include documentation in a notebook you can use the %md magic command to identify Markdown markup. 包含的 Markdown 標記會轉譯為 HTML。The included Markdown markup is rendered into HTML. 例如,這個 Markdown 程式碼片段包含層級1標題的標記:For example, this Markdown snippet contains markup for a level-one heading:

%md # Hello This is a Title

它會轉譯為 HTML 標題:It is rendered as a HTML title:

筆記本 HTML 標題Notebook HTML title

可折疊的標題Collapsible headings

在包含 Markdown 標題的資料格之後出現的儲存格可以折迭到標題儲存格。Cells that appear after cells containing Markdown headings can be collapsed into the heading cell. 下圖顯示稱為「 標題 1 」的第一層標題,其中包含下列兩個數據格。The following image shows a level-one heading called Heading 1 with the following two cells collapsed into it.

折迭的資料格Collapsed cells

若要展開和折迭標題,請按一下 +-To expand and collapse headings, click the + and -.

另請參閱 隱藏和顯示資料格內容Also see Hide and show cell content.

您可以使用相對路徑連結至 Markdown 資料格中的其他筆記本或資料夾。You can link to other notebooks or folders in Markdown cells using relative paths. href 錨點標記的屬性指定為相對路徑,從開始, $ 然後遵循 Unix 檔案系統中的相同模式:Specify the href attribute of an anchor tag as the relative path, starting with a $ and then follow the same pattern as in Unix file systems:

%md
<a href="$./myNotebook">Link to notebook in same folder as current notebook</a>
<a href="$../myFolder">Link to folder in parent folder of current notebook</a>
<a href="$./myFolder2/myNotebook2">Link to nested notebook</a>

顯示影像Display images

若要顯示儲存在儲存檔 中的影像,請使用下列語法:To display images stored in the FileStore, use the syntax:

%md
![test](files/image.png)

例如,假設您在檔案系統中有 Databricks 標誌影像檔案:For example, suppose you have the Databricks logo image file in FileStore:

dbfs ls dbfs:/FileStore/
databricks-logo-mobile.png

當您在 Markdown 資料格中包含下列程式碼:When you include the following code in a Markdown cell:

Markdown 儲存格中的影像Image in Markdown cell

影像會轉譯在資料格中:the image is rendered in the cell:

轉譯的影像Rendered image

顯示數學方公式Display mathematical equations

筆記本支援 KaTeX 以顯示數學公式和方程式。Notebooks support KaTeX for displaying mathematical formulas and equations. 例如,For example,

%md
\\(c = \\pm\\sqrt{a^2 + b^2} \\)

\\(A{_i}{_j}=B{_i}{_j}\\)

$$c = \\pm\\sqrt{a^2 + b^2}$$

\\[A{_i}{_j}=B{_i}{_j}\\]

轉譯為:renders as:

轉譯的方程式1Rendered equation 1

and

%md
\\( f(\beta)= -Y_t^T X_t \beta + \sum log( 1+{e}^{X_t\bullet\beta}) + \frac{1}{2}\delta^t S_t^{-1}\delta\\)

where \\(\delta=(\beta - \mu_{t-1})\\)

轉譯為:renders as:

轉譯的方程式2Rendered equation 2

包含 HTMLInclude HTML

您可以使用函式在筆記本中包含 HTML displayHTMLYou can include HTML in a notebook by using the function displayHTML. 如需如何進行這項操作的範例,請參閱 筆記本中的 HTML、D3 和 SVGSee HTML, D3, and SVG in notebooks for an example of how to do this.

注意

displayHTMLIframe 是從網域提供的 databricksusercontent.com ,而 iframe 沙箱包含 allow-same-origin 屬性。The displayHTML iframe is served from the domain databricksusercontent.com and the iframe sandbox includes the allow-same-origin attribute. databricksusercontent.com 必須可從瀏覽器存取。databricksusercontent.com must be accessible from your browser. 如果貴公司的網路目前封鎖了該網址,請要求 IT 將該網址列入允許清單中。If it is currently blocked by your corporate network, it will need to be whitelisted by IT.

命令批註Command comments

您可以使用命令批註來與共同作業者進行討論。You can have discussions with collaborators using command comments.

若要切換批註提要欄位,請按一下筆記本右上方的 [ 批註 ] 按鈕。To toggle the Comments sidebar, click the Comments button at the top right of a notebook.

切換筆記本批註Toggle notebook comments

將批註新增至命令:To add a comment to a command:

  1. 反白顯示命令文字,然後按一下批註反升:Highlight the command text and click the comment bubble:

    開啟註解Open comments

  2. 新增您的批註,然後按一下 [ 批註]。Add your comment and click Comment.

    新增 commentyAdd commenty

若要編輯、刪除或回復批註,請按一下批註並選擇動作。To edit, delete, or reply to a comment, click the comment and choose an action.

編輯批註Edit comment

變更儲存格顯示 Change cell display

筆記本有三個顯示選項:There are three display options for notebooks:

  • 標準 view:結果會立即顯示在程式碼資料格之後Standard view: results are displayed immediately after code cells
  • 僅限結果:只顯示結果Results only: only results are displayed
  • 並存:程式碼和結果資料格並排顯示,右邊有結果Side-by-side: code and results cells are displayed side by side, with results to the right

移至 [View] 功能表Go to the View menu 檢視功能表 以選取您的顯示選項。to select your display option.

並排顯示side-by-side view

顯示行和命令編號Show line and command numbers

若要顯示行號或命令編號,請移至 [View] 功能表  視圖功能表 ,然後選取 [ 顯示行號 ] 或 [ 顯示命令編號]。To show line numbers or command numbers, go to the View menu View Menu and select Show line numbers or Show command numbers. 一旦顯示之後,您就可以從相同的功能表再次隱藏它們。Once they’re displayed, you can hide them again from the same menu. 您也可以使用鍵盤快速鍵 Control + L 來啟用行號。You can also enable line numbers with the keyboard shortcut Control+L.

透過 [view] 功能表顯示行號或命令編號Show line or command numbers via the view menu

筆記本中啟用的行和命令編號Line and command numbers enabled in notebook

如果您啟用行號或命令編號,Databricks 會儲存您的喜好設定,並將其顯示在該瀏覽器的所有其他筆記本中。If you enable line or command numbers, Databricks saves your preference and shows them in all of your other notebooks for that browser.

儲存格上方的命令編號會連結至該特定命令。Command numbers above cells link to that specific command. 如果您按一下資料格的命令編號,則會將您的 URL 更新為錨定至該命令。If you click on the command number for a cell, it updates your URL to be anchored to that command. 如果您想要連結到筆記本中的特定命令,請以滑鼠右鍵按一下命令編號,然後選擇 [ 複製連結位址]。If you want to link to a specific command in your notebook, right-click the command number and choose copy link address.

尋找和取代文字Find and replace text

若要尋找並取代筆記本內的文字,請選取 [檔案] > [尋找和取代]。To find and replace text within a notebook, select File > Find and Replace.

尋找及取代文字Find and replace text

目前的相符專案會以橙色醒目提示,而所有其他的相符專案則會以黃色反白顯示。The current match is highlighted in orange and all other matches are highlighted in yellow.

相符文字Matching text

您可以按一下 [ 取代] 來取代個別的相符專案。You can replace matches on an individual basis by clicking Replace.

您可以按一下 [ 步] 和 [下一步] 按鈕,或按 shift + enterenter ,分別移至上一個和下一個相符專案,以在相符專案之間切換。You can switch between matches by clicking the Prev and Next buttons or pressing shift+enter and enter to go to the previous and next matches, respectively.

按一下 ![ 刪除] 圖示 或按下 esc 鍵,以關閉 [尋找和取代] 工具。Close the find and replace tool by clicking Delete Icon or by pressing esc.

自動完成Autocomplete

您可以使用 Azure Databricks 自動完成功能,在資料格內輸入時自動完成程式碼區段。You can use Azure Databricks autocomplete features to automatically complete code segments as you enter them in cells. 這可減少您必須記住的內容,並將您必須做的輸入量降至最低。This reduces what you have to remember and minimizes the amount of typing you have to do. Azure Databricks 在您的筆記本中支援兩種自動完成類型:本機和伺服器。Azure Databricks supports two types of autocomplete in your notebook: local and server.

本機自動完成會完成筆記本中的單字。Local autocomplete completes words that exist in the notebook. 伺服器自動完成功能更強大,因為它會存取已定義類型、類別和物件的叢集,以及 SQL database 和資料表名稱。Server autocomplete is more powerful because it accesses the cluster for defined types, classes, and objects, as well as SQL database and table names. 若要啟用伺服器自動完成功能,您必須將 附加筆記本附加至 叢集,並執行所有定義 completable 物件的 儲存格To activate server autocomplete, you must attach your attach a notebook to a cluster and run all cells that define completable objects.

重要

在命令執行期間,會封鎖 R 筆記本中的伺服器自動完成。Server autocomplete in R notebooks is blocked during command execution.

輸入 completable 物件之後,按 tab 鍵即可觸發自動完成。You trigger autocomplete by pressing Tab after entering a completable object. 例如,在您定義和執行包含和定義的資料格之後 MyClassinstance 的方法 instance 會 completable,而當您按 tab 鍵時,就會顯示有效完成的清單。For example, after you define and run the cells containing the definitions of MyClass and instance, the methods of instance are completable, and a list of valid completions displays when you press Tab.

觸發程式自動完成Trigger autocomplete

類型完成和 SQL database 和資料表名稱完成的運作方式相同。Type completion and SQL database and table name completion work in the same way.

類型完成 — —— — SQL 完成

格式化 SQL Format SQL

Azure Databricks 提供的工具可讓您快速且輕鬆地格式化筆記本儲存格中的 SQL 程式碼。Azure Databricks provides tools that allow you to format SQL code in notebook cells quickly and easily. 這些工具可減少您的程式碼格式化,並協助在您的筆記本中強制執行相同的編碼標準。These tools reduce the effort to keep your code formatted and help to enforce the same coding standards across your notebooks.

您可以透過下列方式觸發格式器:You can trigger the formatter in the following ways:

  • 單一資料格Single cells

    • 鍵盤快速鍵:按 Cmd + Shift + FKeyboard shortcut: Press Cmd+Shift+F.

    • 命令操作功能表:在 SQL 資料格的命令內容下拉式功能表中,選取 [ 設定 Sql 格式 ]。Command context menu: Select Format SQL in the command context drop-down menu of a SQL cell. 此專案只會在 SQL 筆記本儲存格中顯示,且 %sql 語言魔術為。This item is visible only in SQL notebook cells and those with a %sql language magic.

      從命令內容格式化 SQLFormatting SQL From Command Context

  • 多個資料格Multiple cells

    選取 多個 sql 資料格 ,然後選取 [編輯] > 將 Sql 資料格格式化Select multiple SQL cells and then select Edit > Format SQL Cells. 如果您選取多個語言的資料格,則只會格式化 SQL 資料格。If you select cells of more than one language, only SQL cells are formatted. 這包含使用的 %sqlThis includes those that use %sql.

    從 [編輯] 功能表格式化 SQLFormatting SQL From Edit Menu

以下是在格式化之後,上述範例中的第一個資料格:Here’s the first cell in the preceding example after formatting:

格式化 SQL 之後After Formatting SQL

查看目錄View table of contents

若要顯示自動產生的目錄,請按一下 [筆記本] 左上角的箭號, (側邊欄和最上層的資料格) 。To display an automatically generated table of contents, click the arrow at the upper left of the notebook (between the sidebar and the topmost cell). 目錄是從筆記本中使用的 Markdown 標題產生的。The table of contents is generated from the Markdown headings used in the notebook.

開啟 TOCOpen TOC

若要關閉目錄,請按一下向左箭號。To close the table of contents, click the left-facing arrow.

關閉 TOCClose TOC

執行筆記本 Run notebooks

本節說明如何執行一或多個筆記本儲存格。This section describes how to run one or more notebook cells.

本節內容:In this section:

規格需求Requirements

筆記本必須 附加 至叢集。The notebook must be attached to a cluster. 如果叢集未執行,則當您執行一或多個資料格時,就會啟動該群集。If the cluster is not running, the cluster is started when you run one or more cells.

執行儲存格Run a cell

在最右邊的儲存格動作功能表中  ,按一下 ![ 執行] 圖示 並選取 [ 執行資料格],或按 shift + enterIn the cell actions menu Cell actions at the far right, click Run Icon and select Run Cell, or press shift+enter.

重要

筆記本資料格的大小上限(內容和輸出)為16MB。The maximum size for a notebook cell, both contents and output, is 16MB.

例如,請嘗試執行此 Python 程式碼片段,以參考預先定義的 spark 變數For example, try running this Python code snippet that references the predefined spark variable.

spark

然後,執行一些真正的程式碼:and then, run some real code:

1+1 # => 2

注意

筆記本有許多預設設定:Notebooks have a number of default settings:

  • 當您執行資料格時,筆記本會自動 附加 到執行中的叢集,而不會提示。When you run a cell, the notebook automatically attaches to a running cluster without prompting.
  • 當您按下 shift + enter 時,如果看不到儲存格,筆記本會自動滾動至下一個儲存格。When you press shift+enter, the notebook auto-scrolls to the next cell if the cell is not visible.

若要變更這些設定,請選取  [帳戶] 圖示 > 使用者設定 > 筆記本設定 ,並設定個別的核取方塊。To change these settings, select Account Icon > User Settings > Notebook Settings and configure the respective checkboxes.

執行上述所有步驟或更新版本Run all above or below

若要在儲存格之前或之後執行所有資料格,請移至最右邊的儲存格動作功能表資料  格動作 ,按一下 ![ 執行] 功能表 ,然後選取 [全部 執行 ] 或 [ 全部執行]。To run all cells before or after a cell, go to the cell actions menu Cell actions at the far right, click Run Menu, and select Run All Above or Run All Below.

[執行以下所有 內容] 包含您所在的儲存格。Run All Below includes the cell you are in. 上述所有動作都 不會執行。Run All Above does not.

執行所有資料格 Run all cells

若要執行筆記本中的所有儲存格,請選取筆記本工具列中的 [ 全部執行 ]。To run all the cells in a notebook, select Run All in the notebook toolbar.

重要

如果 掛接和取消掛接的步驟位於相同的筆記本中,請不要執行 全部 執行。Do not do a Run All if steps for mount and unmount are in the same notebook. 這可能會導致競爭情形,而且可能會損毀掛接點。It could lead to a race condition and possibly corrupt the mount points.

每個資料格查看多個輸出View multiple outputs per cell

非 Python 筆記本中的 Python 筆記本和資料 %python 格支援每個資料格多個輸出。Python notebooks and %python cells in non-Python notebooks support multiple outputs per cell.

一個儲存格中的多個輸出Multiple outputs in one cell

這項功能需要 Databricks Runtime 7.1 或更高版本,而且可以藉由設定在 Databricks Runtime 7.1-7.3 中啟用 spark.databricks.workspace.multipleResults.enabled trueThis feature requires Databricks Runtime 7.1 or above and can be enabled in Databricks Runtime 7.1-7.3 by setting spark.databricks.workspace.multipleResults.enabled true. 預設會在 Databricks Runtime 7.4 和更新版本中啟用。It is enabled by default in Databricks Runtime 7.4 and above.

Python 和 Scala 錯誤醒目提示Python and Scala error highlighting

Python 和 Scala 筆記本支援錯誤反白顯示。Python and Scala notebooks support error highlighting. 也就是說,資料格會反白顯示擲回錯誤的程式程式碼。That is, the line of code that is throwing the error will be highlighted in the cell. 此外,如果錯誤輸出是 stacktrace,則擲回錯誤的資料格會在 stacktrace 中顯示為數據格的連結。Additionally, if the error output is a stacktrace, the cell in which the error is thrown is displayed in the stacktrace as a link to the cell. 您可以按一下這個連結,跳到違規的程式碼。You can click this link to jump to the offending code.

Python 錯誤醒目提示Python error highlighting

Scala 錯誤反白顯示Scala error highlighting

通知Notifications

通知會向您發出特定事件的警示,例如 執行所有資料格 期間正在執行的命令,以及哪些命令處於錯誤狀態。Notifications alert you to certain events, such as which command is currently running during Run all cells and which commands are in error state. 當您的筆記本顯示多個錯誤通知時,第一個會有一個可讓您清除所有通知的連結。When your notebook is showing multiple error notifications, the first one will have a link that allows you to clear all notifications.

筆記本通知Notebook notifications

預設會啟用筆記本通知。Notebook notifications are enabled by default. 您可以在 [帳戶] 圖示下停用它們,  > 使用者設定] > [筆記本設定]。You can disable them under Account Icon > User Settings > Notebook Settings.

Databricks AdvisorDatabricks Advisor

Databricks Advisor 會在每次執行時自動分析命令,並在筆記本中顯示適當的建議。Databricks Advisor automatically analyzes commands every time they are run and displays appropriate advice in the notebooks. 建議通知所提供的資訊可協助您改善工作負載的效能、降低成本,並避免常見的錯誤。The advice notices provide information that can assist you in improving the performance of workloads, reducing costs, and avoiding common mistakes.

查看建議View advice

具有燈泡圖示的藍色方塊表示有適用于命令的建議。A blue box with a lightbulb icon signals that advice is available for a command. 方塊會顯示建議的相異部分數目。The box displays the number of distinct pieces of advice.

Databricks 建議Databricks advice

按一下燈泡以展開方塊並查看建議。Click the lightbulb to expand the box and view the advice. 將會顯示一或多個建議的部分。One or more pieces of advice will become visible.

查看建議View advice

按一下 [ 深入瞭解 ] 連結,以查看提供建議相關詳細資訊的檔。Click the Learn more link to view documentation providing more information related to the advice.

按一下 [ 不要再顯示此 內容] 連結,以隱藏建議的部分。Click the Don’t show me this again link to hide the piece of advice. 這種類型的建議將不再顯示。The advice of this type will no longer be displayed. 此動作可以 在筆記本設定中反轉This action can be reversed in Notebook Settings.

再按一次燈泡,以折迭建議方塊。Click the lightbulb again to collapse the advice box.

建議設定Advice settings

選取 ![ 帳戶] 圖示 > 使用者設定] > 筆記本設定 ,或按一下 [展開的建議] 方塊中的齒輪圖示,以存取 [筆記本設定] 頁面。Access the Notebook Settings page by selecting Account Icon > User Settings > Notebook Settings or by clicking the gear icon in the expanded advice box.

筆記本設定Notebook settings

切換 [ 開啟 Databricks Advisor ] 選項以啟用或停用建議。Toggle the Turn on Databricks Advisor option to enable or disable advice.

如果目前隱藏一或多個建議類型,就會顯示 [ 重設隱藏的建議 ] 連結。The Reset hidden advice link is displayed if one or more types of advice is currently hidden. 按一下連結,讓該建議類型再次顯示。Click the link to make that advice type visible again.

從另一個筆記本執行筆記本 Run a notebook from another notebook

您可以使用魔術命令從另一個筆記本執行筆記本 %run <notebook>You can run a notebook from another notebook by using the %run <notebook> magic command. 這大致上等同于 :load 本機電腦上的 Scala 複寫或 import Python 中的語句的命令。This is roughly equivalent to a :load command in a Scala REPL on your local machine or an import statement in Python. 中定義的所有變數都 <notebook> 可在您目前的筆記本中使用。All variables defined in <notebook> become available in your current notebook.

%run 必須在 資料格中, 因為它會執行整個筆記本內嵌。%run must be in a cell by itself, because it runs the entire notebook inline.

注意

無法 使用 %run 來執行 Python 檔案,以及 import 在該檔案中定義的實體到筆記本中。You cannot use %run to run a Python file and import the entities defined in that file into a notebook. 若要從 Python 檔案匯入,您必須將檔案封裝到 Python 程式庫 、從該 python 程式庫建立 Azure Databricks 程式庫,然後將連結 庫安裝到 您用來執行筆記本的叢集中。To import from a Python file you must package the file into a Python library, create an Azure Databricks library from that Python library, and install the library into the cluster you use to run your notebook.

範例Example

假設您有 notebookAnotebookBSuppose you have notebookA and notebookB. notebookA 包含具有下列 Python 程式碼的儲存格:notebookA contains a cell that has the following Python code:

x = 5

即使您未 x 在中定義,您還是 notebookB 可以 xnotebookB 執行之後存取 %run notebookAEven though you did not define x in notebookB, you can access x in notebookB after you run %run notebookA.

%run /Users/path/to/notebookA

print(x) # => 5

若要指定相對路徑,請在其前面加上 ./../To specify a relative path, preface it with ./ or ../. 例如,如果 notebookAnotebookB 位於相同的目錄中,您也可以從相對路徑執行它們。For example, if notebookA and notebookB are in the same directory, you can alternatively run them from a relative path.

%run ./notebookA

print(x) # => 5
%run ../someDirectory/notebookA # up a directory and into another

print(x) # => 5

如需筆記本之間更複雜的互動,請參閱 筆記本工作流程For more complex interactions between notebooks, see Notebook workflows.

管理筆記本狀態 和結果 Manage notebook state and results

將筆記本附加至叢集並執行一或多個儲存格之後,您的筆記本會有狀態並顯示結果。After you attach a notebook to a cluster and run one or more cells, your notebook has state and displays results. 本節說明如何管理筆記本狀態和結果。This section describes how to manage notebook state and results.

本節內容:In this section:

清除筆記本狀態 和結果 Clear notebooks state and results

若要清除筆記本狀態和結果,請按一下筆記本工具列中的 [ 清除 ],然後選取動作:To clear the notebook state and results, click Clear in the notebook toolbar and select the action:

清除狀態和結果Clear state and results

下載結果Download results

預設會啟用下載結果。By default downloading results is enabled. 若要切換此設定,請參閱 管理從筆記本下載結果的能力To toggle this setting, see Manage the ability to download results from notebooks. 如果已停用下載結果,則  不會顯示 [下載結果] 按鈕。If downloading results is disabled, the Download Result button is not visible.

下載資料格結果Download a cell result

您可以將包含表格式輸出的資料格結果下載到您的本機電腦。You can download a cell result that contains tabular output to your local machine. 按一下Click the 下載結果 按鈕(位於資料格底部)。button at the bottom of a cell.

下載資料格結果Download cell results

名為的 CSV 檔案 export.csv 會下載至預設的下載目錄。A CSV file named export.csv is downloaded to your default download directory.

下載完整的結果Download full results

根據預設,Azure Databricks 會傳回1000個數據框架的資料列。By default Azure Databricks returns 1000 rows of a DataFrame. 當超過1000個數據列時,向下箭號When there are more than 1000 rows, a down arrow 向下箭號按鈕 新增至is added to the 下載結果 按鈕。button. 若要下載查詢的所有結果:To download all the results of a query:

  1. 按一下 [下載結果] 旁的向下箭號  ,然後選取 [ 下載完整結果]。Click the down arrow next to Download Result and select Download full results.

    下載完整的結果Download full results

  2. 選取 [ 重新執行並下載]。Select Re-execute and download.

    重新執行並下載結果Re-run and download results

    下載完整的結果之後,系統會將名為的 CSV 檔案 export.csv 下載至本機電腦,而且該 /databricks-results 資料夾會有一個包含完整查詢結果的已產生資料夾。After you download full results, a CSV file named export.csv is downloaded to your local machine and the /databricks-results folder has a generated folder containing full the query results.

    下載的結果Downloaded results

隱藏和顯示資料格內容 Hide and show cell content

資料格內容是由資料格的程式碼和執行儲存格的結果所組成。Cell content consists of cell code and the result of running the cell. 您可以使用 [資料格動作] 功能表來隱藏和顯示儲存格程式碼和結果You can hide and show the cell code and result using the cell actions menu 資料格動作 ,位於資料格的右上方。at the top right of the cell.

若要隱藏資料格程式碼:To hide cell code:

  • 按一下  插入 號,然後選取 [隱藏程式碼]Click Down Caret and select Hide Code

若要隱藏和顯示資料格結果,請執行下列其中一項:To hide and show the cell result, do any of the following:

  • 按一下  插入號 ,然後選取 [隱藏結果]Click Down Caret and select Hide Result
  • 選取Select 儲存格最小化
  • 輸入 Esc > Shift + oType Esc > Shift + o

若要顯示隱藏的儲存格程式碼或結果,請按一下 [ 顯示 連結]:To show hidden cell code or results, click the Show links:

顯示隱藏的程式碼和結果Show hidden code and results

另請參閱可折迭的 標題See also Collapsible headings.

筆記本隔離Notebook isolation

筆記本隔離是指筆記本間變數和類別的可見度。Notebook isolation refers to the visibility of variables and classes between notebooks. Azure Databricks 支援兩種類型的隔離:Azure Databricks supports two types of isolation:

  • 變數和類別隔離Variable and class isolation
  • Spark 會話隔離Spark session isolation

注意

由於所有附加至相同叢集的筆記本都在相同的叢集 Vm 上執行,即使啟用了 Spark 會話隔離,在叢集內也不會有任何保證的使用者隔離。Since all notebooks attached to the same cluster execute on the same cluster VMs, even with Spark session isolation enabled there is no guaranteed user isolation within a cluster.

變數和類別隔離Variable and class isolation

變數和類別僅適用于目前的筆記本。Variables and classes are available only in the current notebook. 例如,兩個附加至相同叢集的筆記本可以定義具有相同名稱的變數和類別,但這些物件是相異的。For example, two notebooks attached to the same cluster can define variables and classes with the same name, but these objects are distinct.

若要定義 所有附加至相同 叢集的筆記本都可以看見的類別,請在 封裝儲存格中定義類別。To define a class that is visible to all notebooks attached to the same cluster, define the class in a package cell. 然後,您可以使用完整名稱來存取類別,這與存取附加的 Scala 或 JAVA 程式庫中的類別相同。Then you can access the class by using its fully qualified name, which is the same as accessing a class in an attached Scala or Java library.

Spark 會話隔離 Spark session isolation

每個附加到執行 Apache Spark 2.0.0 和以上的叢集的筆記本都有一個名為的 預先定義變數 spark ,該變數表示 SparkSessionEvery notebook attached to a cluster running Apache Spark 2.0.0 and above has a pre-defined variable called spark that represents a SparkSession. SparkSession 是使用 Spark Api 以及設定執行時間設定的進入點。SparkSession is the entry point for using Spark APIs as well as setting runtime configurations.

Spark 會話隔離預設為啟用。Spark session isolation is enabled by default. 您也可以使用 全域 暫時視圖,在筆記本之間共用臨時視圖。You can also use global temporary views to share temporary views across notebooks. 請參閱 Create viewcreate viewSee Create View or CREATE VIEW. 若要停用 Spark 會話隔離,請 spark.databricks.session.share truespark設定中將設定為。To disable Spark session isolation, set spark.databricks.session.share to true in the Spark configuration.

重要

設定 spark.databricks.session.share true 會中斷串流筆記本資料格和串流作業所使用的監視。Setting spark.databricks.session.share true breaks the monitoring used by both streaming notebook cells and streaming jobs. 更明確地說:Specifically:

  • 不會顯示串流儲存格中的 圖形The graphs in streaming cells are not displayed.
  • 只要資料流程正在執行,工作就不會被封鎖 (它們只是「成功」完成,) 停止資料流程。Jobs do not block as long as a stream is running (they just finish “successfully”, stopping the stream).
  • 作業中的串流未受監視,無法進行終止。Streams in jobs are not monitored for termination. 相反地,您必須手動呼叫 awaitTermination()Instead you must manually call awaitTermination().
  • 呼叫串流資料框架上的 顯示函數 無法運作。Calling the display function on streaming DataFrames doesn’t work.

以其他語言觸發命令的資料格 (也就是,使用 %scala 、、和) 的儲存格, %python %r %sql 以及包含其他筆記本的資料格 (也就是使用) 的儲存格 %run 是目前筆記本的一部分。Cells that trigger commands in other languages (that is, cells using %scala, %python, %r, and %sql) and cells that include other notebooks (that is, cells using %run) are part of the current notebook. 因此,這些資料格與其他筆記本資料格位於相同的會話中。Thus, these cells are in the same session as other notebook cells. 相反地, 筆記本工作流程 會以隔離的方式執行筆記本 SparkSession ,這表示在這類筆記本中定義的暫時性視圖 不會顯示 在其他筆記本中。By contrast, a notebook workflow runs a notebook with an isolated SparkSession, which means temporary views defined in such a notebook are not visible in other notebooks.

版本控制Version control

Azure Databricks 具有適用于筆記本的基本版本控制。Azure Databricks has basic version control for notebooks. 您可以在修訂時執行下列動作:新增批註、還原和刪除修訂,以及清除修訂歷程記錄。You can perform the following actions on revisions: add comments, restore and delete revisions, and clear revision history.

若要存取筆記本修訂,請按一下筆記本工具列右上方的 修訂歷程記錄To access notebook revisions, click Revision History at the top right of the notebook toolbar.

本節內容:In this section:

新增意見Add a comment

將批註新增至最新的修訂:To add a comment to the latest revision:

  1. 按一下 [修訂]。Click the revision.

  2. 按一下 [ 立即儲存 ] 連結。Click the Save now link.

    儲存批註Save comment

  3. 在 [儲存筆記本修訂] 對話方塊中,輸入批註。In the Save Notebook Revision dialog, enter a comment.

  4. 按一下 [儲存]Click Save. 筆記本修訂會與輸入的批註一併儲存。The notebook revision is saved with the entered comment.

還原修訂Restore a revision

若要還原修訂:To restore a revision:

  1. 按一下 [修訂]。Click the revision.

  2. 按一下 [ 還原此修訂]。Click Restore this revision.

    還原修訂Restore revision

  3. 按一下 [確認] 。Click Confirm. 選取的修訂會成為筆記本的最新版本。The selected revision becomes the latest revision of the notebook.

刪除修訂Delete a revision

若要刪除筆記本的修訂專案:To delete a notebook’s revision entry:

  1. 按一下 [修訂]。Click the revision.

  2. 按一下垃圾桶圖示Click the trash icon 資源回收筒..

    刪除修訂Delete revision

  3. 按一下 [是,清除]Click Yes, erase. 選取的修訂會從筆記本的修訂歷程記錄中刪除。The selected revision is deleted from the notebook’s revision history.

清除修訂歷程記錄Clear a revision history

清除筆記本的修訂歷程記錄:To clear a notebook’s revision history:

  1. 選取 [檔案] > 清除修訂歷程記錄Select File > Clear Revision History.

  2. 按一下 [是,清除]Click Yes, clear. 筆記本修訂歷程記錄已清除。The notebook revision history is cleared.

    警告

    一旦清除,修訂歷程記錄就無法復原。Once cleared, the revision history is not recoverable.

Git 版本控制Git version control

Azure Databricks 也會與這些以 Git 為基礎的版本控制工具整合:Azure Databricks also integrates with these Git-based version control tools: