在Azure Machine Learning中使用Synapse Spark Pool作为计算目标
概述
本文将详细介绍如何在Azure Machine Learning项目中利用Azure Synapse Analytics的Spark池作为计算资源。通过两种主要方式实现:
- 使用ScriptRunConfig提交实验运行到已连接的Synapse Spark集群
- 在Azure Machine Learning管道中使用SynapseSparkStep编排数据处理流程
准备工作
在开始之前,需要完成以下基础配置:
- 创建Azure Synapse工作区:这是运行Spark作业的基础环境
- 在Synapse工作区中创建Spark池:为作业提供计算资源
- 确保拥有足够权限:需要是Synapse工作区资源的"所有者"才能执行后续的链接操作
环境配置
链接Synapse工作区到AML
首先需要将Synapse工作区与Azure Machine Learning工作区建立关联:
synapse_link_config = SynapseWorkspaceLinkedServiceConfiguration(
subscription_id="<订阅ID>",
resource_group="<资源组名称>",
name="<Synapse工作区名称>"
)
linked_service = LinkedService.register(
workspace=ws,
name="<链接服务名称>",
linked_service_config=synapse_link_config)
重要提示:链接完成后,系统会为每个链接服务生成一个MSI(系统分配的身份主体ID),需要在Synapse Studio门户中为该MSI授予"Synapse Apache Spark管理员"角色。
附加Synapse Spark池作为AML计算目标
attach_config = SynapseCompute.attach_configuration(
linked_service,
type="SynapseSpark",
pool_name="<Spark池名称>")
synapse_compute = ComputeTarget.attach(
workspace=ws,
name="<计算目标名称>",
attach_configuration=attach_config)
synapse_compute.wait_for_completion()
数据准备
上传示例数据
# 使用默认的blob存储
def_blob_store = Datastore(ws, "workspaceblobstore")
# 上传本地文件作为数据源
file_name = "Titanic.csv"
def_blob_store.upload_files(files=["./{}".format(file_name)], overwrite=False)
创建数据集
支持两种类型的数据集输入:
- 表格数据集:
titanic_tabular_dataset = Dataset.Tabular.from_delimited_files(
path=[(def_blob_store, file_name)])
input1 = titanic_tabular_dataset.as_named_input("tabular_input")
- 文件数据集:
titanic_file_dataset = Dataset.File.from_files(
path=[(def_blob_store, file_name)])
input2 = titanic_file_dataset.as_named_input("file_input").as_hdfs()
输出配置
output = HDFSOutputDatasetConfig(
destination=(def_blob_store,"test")
).register_on_complete(name="registered_dataset")
使用ScriptRunConfig提交实验
准备数据处理脚本
创建dataprep.py
脚本,该脚本能够:
- 使用数据集SDK读取表格数据集
- 使用HDFS路径读取文件数据集
- 将处理后的数据写入输出目录
配置运行环境
conda_dep = CondaDependencies()
conda_dep.add_pip_package("azureml-core==1.20.0")
配置Spark运行参数
run_config = RunConfiguration(framework="pyspark")
run_config.target = synapse_compute_name
# 配置Spark资源
run_config.spark.configuration["spark.driver.memory"] = "1g"
run_config.spark.configuration["spark.driver.cores"] = 2
run_config.spark.configuration["spark.executor.memory"] = "1g"
run_config.spark.configuration["spark.executor.cores"] = 1
run_config.spark.configuration["spark.executor.instances"] = 1
run_config.environment.python.conda_dependencies = conda_dep
提交实验运行
script_run_config = ScriptRunConfig(
source_directory = './code',
script= 'dataprep.py',
arguments = ["--tabular_input", input1,
"--file_input", input2,
"--output_dir", output],
run_config = run_config)
exp = Experiment(workspace=ws, name="synapse-spark")
run = exp.submit(config=script_run_config)
在AML管道中使用SynapseSparkStep
创建CPU计算集群
cpu_cluster_name = "cpucluster"
compute_config = AmlCompute.provisioning_configuration(
vm_size='STANDARD_D2_V2',
max_nodes=1)
cpu_cluster = ComputeTarget.create(ws, cpu_cluster_name, compute_config)
cpu_cluster.wait_for_completion(show_output=True)
创建管道步骤
- SynapseSparkStep - 在Synapse Spark池上执行数据处理:
step_1 = SynapseSparkStep(
name = 'synapse-spark',
file = 'dataprep.py',
source_directory="./code",
inputs=[step1_input1, step1_input2],
outputs=[step1_output],
arguments = ["--tabular_input", step1_input1,
"--file_input", step1_input2,
"--output_dir", step1_output],
compute_target = synapse_compute_name,
driver_memory = "7g",
driver_cores = 4,
executor_memory = "7g",
executor_cores = 2,
num_executors = 1,
environment = env)
- PythonScriptStep - 在CPU集群上执行训练:
step_2 = PythonScriptStep(
script_name="train.py",
arguments=[step2_input],
inputs=[step2_input],
compute_target=cpu_cluster_name,
source_directory="./code",
allow_reuse=False)
提交管道运行
pipeline = Pipeline(workspace=ws, steps=[step_1, step_2])
pipeline_run = pipeline.submit('synapse-pipeline', regenerate_outputs=True)
最佳实践
- 资源分配:根据数据规模合理配置Spark资源,避免资源浪费或不足
- 权限管理:确保MSI具有足够的权限访问Synapse资源
- 环境隔离:为不同的任务创建独立的环境,避免依赖冲突
- 监控优化:利用Azure提供的监控工具跟踪作业执行情况,持续优化性能
通过本文介绍的方法,您可以充分利用Azure Synapse Spark池的强大计算能力,与Azure Machine Learning无缝集成,构建高效的数据处理和机器学习工作流。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考