Notebooks in EMR Serverless Spark provide an interactive environment for writing and running Apache Spark code. This topic explains how to create, edit, run, publish, import, and export a notebook, and how to share code between notebooks using the %run magic command.
Prerequisites
Before you begin, make sure you have:
A workspace. For more information, see Create a workspace.
A notebook session created and started. For more information, see Manage notebook sessions.
Create a notebook
Log on to the E-MapReduce (EMR) console.
In the navigation pane on the left, choose EMR Serverless > Spark.
On the Spark page, find your workspace and click its name.
In the navigation pane on the left, click Development.
On the Development tab, click the
icon.Enter a name, set Type to Interactive Development > Notebook, and click OK.
Edit and run a notebook
In the upper-right corner of the configuration tab of the notebook, select a notebook session from the Notebook Sessions drop-down list. A single notebook session can serve multiple notebooks simultaneously, so you do not need a dedicated session per notebook. To create a session on the spot, click Create Notebook Session from the drop-down list.
Enter Python statements in a cell.
Run the notebook using one of the following options:
To run all cells, click Execute All Cells.
To run a single cell, click the
icon in front of that cell.
Publish a notebook
Confirm the job runs as expected.
In the upper-right corner of the configuration tab of the notebook, click Publish.
In the Publish dialog box, configure the Remarks parameter and click OK.
Export a notebook
On the Development tab, hover over the notebook name and choose
> Export.
Import a notebook
Only files of the Notebook type can be imported.
On the Development tab, hover over the target folder and click the
icon.
In the dialog box, click the dotted-border area to select a local file, or drag the file into the area.
Click OK.
Share code between notebooks
Use the %run magic command to execute code from another notebook. This is useful for sharing functions and variables across multiple notebooks.
All %run paths must start with /dev, followed by the folder path to the target notebook.
Example: Share a function defined in notebook_a with notebook_b.
In
notebook_a, define a function and a variable.# notebook_a.ipynb def greet(name): return f"Hello, {name}!" message = "Welcome to our Python session."In
notebook_b, runnotebook_ausing the%runcommand. Replacepath/to/with the actual folder path.%run /dev/path/to/notebook_aIn a new cell in
notebook_b, use the function and variable fromnotebook_a.print(greet("EMR Serverless Spark")) print(message)The output is similar to:
