Develop with notebooks

更新时间:
复制 MD 格式

This topic describes how to develop with notebooks in EMR Serverless Spark. You will learn how to create, edit, run, publish, import, and export notebooks, and share code between them.

Prerequisites

You have created a workspace and a notebook session. For more information, see Create a workspace and Manage notebook sessions.

Create a notebook

  1. Go to the development page.

    1. Log on to the E-MapReduce (EMR) console.

    2. In the left-side navigation pane, choose EMR Serverless > Spark.

    3. On the Spark page, click the name of your target workspace.

    4. On the EMR Serverless Spark page, click Data Development in the left-side navigation pane.

  2. Create a notebook.

    1. On the Development tab, click the image icon.

    2. Enter a name, select Interactive Development for Type, and click OK.

Edit and run a notebook

  1. In the upper-right corner, select a running notebook session.

    You can also select Create Notebook Session from the drop-down list to create a new one.

    Note

    Multiple notebooks can share the same notebook session instance, allowing you to access and use the same session resources from multiple notebooks simultaneously.

  2. Enter Python statements in a cell of the new notebook.

  3. Click Run All Cells or the image icon beside a cell to run the notebook.

Publish a notebook

  1. After the run completes, click Publish in the upper-right corner.

  2. In the Publish dialog box, enter the required details and click OK to save the notebook as a new version.

Export a notebook

On the Development tab in Data Development, hover over a Notebook and select image > Export.

Import a notebook

Note

Currently, only notebook files can be imported.

  1. On the Development tab in Data Development, hover over a folder, and then click the image icon.

    image

  2. In the dialog box, click the upload area to select a local file, or drag a file into the area, and then click OK.

Call another notebook

You can use the %run magic command to run code from another notebook. This is useful when you need to share functions or variables across multiple notebooks.

For example, assume you have two notebook files: notebook_a.ipynb and notebook_b.ipynb. The notebook_a.ipynb file defines functions and variables that you want to use in notebook_b.ipynb.

  1. In notebook_a.ipynb, define a simple function named greet and a variable named message.

    # notebook_a.ipynb
    def greet(name):
        return f"Hello, {name}!"
    
    message = "Welcome to our Python session."
    
  2. In notebook_b.ipynb, use the %run command to call notebook_a.ipynb.

    %run /dev/path/to/notebook_a
    
    Note

    The file path must start with /dev. Replace path/to/ with the actual folder path. In this example, notebook_a is in the test folder.

  3. In a new cell, use the function and variable defined in notebook_a.ipynb.

    print(greet("EMR Serverless Spark"))
    print(message)

    The output is similar to the following:

    image