Run EMR PySpark with the Python3 kernel
Updated at:
In JupyterHub on E-MapReduce, you can run Python tasks directly, or use the PySpark and SQL magic commands within the Python3 kernel. This topic shows you how to run tasks in the Python3 kernel and describes the parameters for the PySpark and SQL magic commands.
Prerequisites
Download the sample notebook file spark_magic_example.ipynb to your local directory.
Procedure
- Access the JupyterHub web UI. For more information, see Manage JupyterHub.
- On the JupyterHub web UI, under Notebook, click Python3.
- Click the icon to upload the sample notebook file.
In the spark_magic_example.ipynb notebook, you can view the provided PySpark and SQL magic commands.
- PySpark command parameters
set_spark_optsSets the Spark runtime parameters. You can configure any parameter supported by the Spark version in your environment. It accepts a dictionary. For example:conf = { 'spark.executor.instances': '3', 'spark.executor.memory': '2g', } %set_spark_opts $confget_spark_optsDisplays the Spark runtime parameters. For example:%get_spark_optsget_spark_sessionStarts a Spark job on the E-MapReduce cluster based on the specified runtime parameters and returns the Spark session. This parameter accepts a string that specifies the Spark job name. For example:spark, sc = %get_spark_session test_applicationImportant Each notebook supports only one Spark session. If you try to create another one, the existing session is returned.stop_spark_sessionTerminates the Spark job.
- SQL command parameters
- In a
%%sqlcell, you can write multiple SQL statements separated by semicolons (;). For example:%%sql create table if not exists test_table_1 (a string, b int); insert into test_table_1 values("abc", 1), ("def", 2); select a, sum(b) from test_table_1 group by a - In a
%%sqlcell, you can use the-oparameter to assign the result to a specified variable as a pandas DataFrame. If you omit this parameter, the cell directly displays the result of the last SQL statement. For example:%%sql -o df # Assigns the result to the df variable. select a, sum(b) from test_table group by a - In a
%%sqlcell, you can use variables from other cells. For example:%%sql select * from test_table where a = '{x}' # The variable x is defined in another cell. This technique is often used with schedulers like Airflow.
- In a
- PySpark command parameters
- Click the icon at the top of the notebook to run the current cell.
Is this page helpful?