DataWorks Notebook User Guide
Configure PySpark in DataWorks Notebook, including custom Spark configurations, OSS storage access, third-party Python packages, JAR resources, and Livy parameters.
Use custom configurations
If you are prompted to upgrade the dataworks-magic version after running %%maxcompute_spark, upgrade before using the following features. Otherwise, the features may not take effect.
Set SparkConf at startup. For example, set the maximum Livy idle time (
configspecifies Livy-related parameters) and enable the schema-level syntax switch (spark_confspecifies Spark-related parameters). Other parameters can be configured in the same way.%%maxcompute_spark { "config": { # cpu and memory defalut "cpu": 1, "memory": "4096M", "livy.server.max.idle.time": "10m" }, "quota": "XXX", "spark_conf": { "spark.sql.catalog.odps.enableNamespaceSchema": "true" } }To modify the configuration, re-run the code above. Note that restarting the Spark session discards the previous session state.
Use OSS storage
Configure OSS access parameters:
oss_endpointmust use an internal endpoint, for example:oss-cn-shanghai-internal.aliyuncs.comDo not set
spark.hadoop.fs.oss.implorspark.hadoop.fs.AbstractFileSystem.oss.implin Notebook. Otherwise, OSS access may fail.
%%maxcompute_spark
{
"spark_conf": {
"spark.hadoop.odps.cupid.trusted.services.access.list": "<bucket-name>.<oss-endpoint>",
"spark.hadoop.fs.oss.accessKeyId": "***",
"spark.hadoop.fs.oss.accessKeySecret": "***",
"spark.hadoop.fs.oss.endpoint": "<oss-endpoint>"
}
}Use third-party Python packages
Package dependencies: We recommend that you use pyodps-pack and specify Python 3.11 as the Python version. Livy Spark Session uses Python 3.11 by default, and version consistency avoids compatibility issues. For more information, see PySpark Python Versions and Dependencies.
# install pyodps pip install pyodps # prepare requirements.txt and pack pyodps-pack -r requirements.txt --python-version=3.11 -o <package-name>Upload the package to MaxCompute project:
-- Run in odpscmd: ADD archive /path/to/<package-name> -f;Configure Spark to use the package:
NoteSeparate multiple
cupid.resourcesentries with commas (,). For more information about this parameter, see General configurations.Separate multiple
PYTHONPATHentries with colons (:). Thepackagessubdirectory in the example is included becausepyodps-packautomatically creates this subdirectory. If you use a different packaging method, verify the directory structure before specifyingPYTHONPATH. For more information aboutPYTHONPATH, see Reference user-defined Python packages.
%%maxcompute_spark { "spark_conf": { "spark.hadoop.odps.cupid.resources": "<your_project>.<package-name>", "spark.executorEnv.PYTHONPATH": "./<your_project>.<package-name>/packages", "spark.yarn.appMasterEnv.PYTHONPATH": "./<your_project>.<package-name>/packages" } }
Use JAR or other resource packages
Upload the resource (using tar.gz as an example):
add archive /path/to/<package-name> -f;Configure Spark to load the resource. Separate multiple resources with commas (,):
%%maxcompute_spark { "spark_conf": { "spark.hadoop.odps.cupid.resources": "<your_project>.<package-name>" } }
You can combine this with Python package configurations in the spark_conf parameter.
Advanced configurations
Specify the Spark version
Supported versions:
spark-3.1.1-odps0.47.0spark-3.4.2-odps0.48.0(default)spark-3.5.2-odps0.49.0
Configuration:
%%maxcompute_spark { "spark_conf": { "spark.hadoop.odps.spark.version": "spark-3.4.2-odps0.48.0" } }
Disable the default Python 3.11 environment
To use a custom Python environment:
%%maxcompute_spark
{
"spark_conf": {
"spark.hadoop.odps.spark.alinux3.enabled": "false"
}
}Use Matplotlib for plotting
Package and upload Matplotlib first. For more information, see Use third-party Python packages. Then create plots in Notebook:
%%spark
import matplotlib.pyplot as plt
import io, base64, json
import matplotlib
# optional
matplotlib.use('Agg')
# A figure must be created here first (critical step).
fig = plt.figure()
# Example plot
x = [1, 2, 3, 4]
y = [20, 22, 19, 23]
plt.plot(x, y, marker='o', linestyle='--', color='b')
plt.title("Temperature Change Over Time")
plt.xlabel("Time")
plt.ylabel("Temperature")
# Render image to Notebook (Livy magic command)
%matplot pltLivy configurations
The following table lists the configurable Livy parameters:
Parameter | Default value | Description |
| true | Specifies whether to enable access control. Enabled by default. We recommend that you do not disable this parameter. |
| 3d | The maximum time that the Livy server can stay alive. Default value: 3 days. Supported units: s, m, h, and d. |
| 1h | The maximum idle time of the Livy server. Default value: 1 hour. The Livy server automatically shuts down if no Spark session is active within this period. |
| 12h | The maximum idle time of a Spark session. Default value: 12 hours. A Spark session automatically shuts down if no Spark task runs within this period. |
| 12h | The maximum retention time of an expired Spark session. Default value: 12 hours. An expired session is permanently destroyed if it is not restarted within this period. |
| 50 | The maximum number of sessions that can exist simultaneously on a single Livy server. Default value: 50. |