Azure Batch Java SDK
Azure Batch SDK for Java
用于在 Azure 中运行大规模并行和高性能计算 (HPC) 批处理作业的客户端库。
安装
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-compute-batch</artifactId>
<version>1.0.0-beta.5</version>
</dependency>前置条件
- Azure Batch 账户
- 已配置计算节点的池
- Azure 订阅
环境变量
AZURE_BATCH_ENDPOINT=https://<account>.<region>.batch.azure.com
AZURE_BATCH_ACCOUNT=<account-name>
AZURE_BATCH_ACCESS_KEY=<account-key>客户端创建
使用 Microsoft Entra ID(推荐)
import com.azure.compute.batch.BatchClient;
import com.azure.compute.batch.BatchClientBuilder;
import com.azure.identity.DefaultAzureCredentialBuilder;
BatchClient batchClient = new BatchClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint(System.getenv("AZURE_BATCH_ENDPOINT"))
.buildClient();
异步客户端
import com.azure.compute.batch.BatchAsyncClient;
BatchAsyncClient batchAsyncClient = new BatchClientBuilder()
.credential(new DefaultAzureCredentialBuilder().build())
.endpoint(System.getenv("AZURE_BATCH_ENDPOINT"))
.buildAsyncClient();
使用共享密钥凭据
import com.azure.core.credential.AzureNamedKeyCredential;
String accountName = System.getenv("AZURE_BATCH_ACCOUNT");
String accountKey = System.getenv("AZURE_BATCH_ACCESS_KEY");
AzureNamedKeyCredential sharedKeyCreds = new AzureNamedKeyCredential(accountName, accountKey);
BatchClient batchClient = new BatchClientBuilder()
.credential(sharedKeyCreds)
.endpoint(System.getenv("AZURE_BATCH_ENDPOINT"))
.buildClient();
核心概念
| 概念 | 描述 |
|---------|-------------|
| Pool (池) | 运行任务的计算节点集合 |
| Job (作业) | 任务的逻辑分组 |
| Task (任务) | 计算单元(命令/脚本) |
| Node (节点) | 执行任务的虚拟机 |
| Job Schedule (作业计划) | 定期创建作业 |
池操作
创建池
import com.azure.compute.batch.models.*;
batchClient.createPool(new BatchPoolCreateParameters("myPoolId", "STANDARD_DC2s_V2")
.setVirtualMachineConfiguration(
new VirtualMachineConfiguration(
new BatchVmImageReference()
.setPublisher("Canonical")
.setOffer("UbuntuServer")
.setSku("22_04-lts")
.setVersion("latest"),
"batch.node.ubuntu 22.04"))
.setTargetDedicatedNodes(2)
.setTargetLowPriorityNodes(0), null);
获取池
BatchPool pool = batchClient.getPool("myPoolId");
System.out.println("Pool state: " + pool.getState());
System.out.println("Current dedicated nodes: " + pool.getCurrentDedicatedNodes());列出池
import com.azure.core.http.rest.PagedIterable;
PagedIterable<BatchPool> pools = batchClient.listPools();
for (BatchPool pool : pools) {
System.out.println("Pool: " + pool.getId() + ", State: " + pool.getState());
}
调整池大小
import com.azure.core.util.polling.SyncPoller;
BatchPoolResizeParameters resizeParams = new BatchPoolResizeParameters()
.setTargetDedicatedNodes(4)
.setTargetLowPriority
Nodes(2);
SyncPoller<BatchPool, BatchPool> poller = batchClient.beginResizePool("myPoolId", resizeParams);
poller.waitForCompletion();
BatchPool resizedPool = poller.getFinalResult();
### 启用自动缩放 (AutoScale)BatchPoolEnableAutoScaleParameters autoScaleParams = new BatchPoolEnableAutoScaleParameters()
.setAutoScaleEvaluationInterval(Duration.ofMinutes(5))
.setAutoScaleFormula("$TargetDedicatedNodes = min(10, $PendingTasks.GetSample(TimeInterval_Minute * 5));");
batchClient.enablePoolAutoScale("myPoolId", autoScaleParams);
### 删除池SyncPoller<BatchPool, Void> deletePoller = batchClient.beginDeletePool("myPoolId");
deletePoller.waitForCompletion();
## 作业操作 (Job Operations)
创建作业
### 获取作业### 列出作业### 获取任务计数### 终止作业SyncPoller<BatchJob, BatchJob> poller = batchClient.beginTerminateJob("myJobId", options, null);
poller.waitForCompletion();
### 删除作业SyncPoller<BatchJob, Void> deletePoller = batchClient.beginDeleteJob("myJobId");
deletePoller.waitForCompletion();
## 任务操作 (Task Operations)
创建单个任务
### 创建带有退出条件的任务### 创建任务集合(最多 100 个)### 创建大量任务(无限制)### 获取任务### 列出任务### 获取任务输出BinaryData stdout = batchClient.getTaskFile("myJobId", "task1", "stdout.txt");
System.out.println(new String(stdout.toBytes(), StandardCharsets.UTF_8));
### 终止任务batchClient.terminateTask("myJobId", "task1", null, null);
## 节点操作
列出节点
### 重启节点### 获取远程登录设置## 作业计划操作
创建作业计划
### 获取作业计划## 错误处理try {
batchClient.getPool("nonexistent-pool");
} catch (BatchErrorException e) {
BatchError error = e.getValue();
System.err.println("Error code: " + error.getCode());
System.err.println("Message: " + error.getMessage().getValue());
if ("PoolNotFound".equals(error.getCode())) {
System.err.println("The specified pool does not exist.");
}
}
``
最佳实践
1. 使用 Entra ID — 身份验证首选 Entra ID 而非共享密钥
2. 使用管理 SDK 操作池 — azure-resourcemanager-batch 支持托管身份createTaskCollection
3. 批量创建任务 — 创建多个任务时请使用 或 createTasksgetJobTaskCounts
4. 正确处理 LRO — 池的缩放和删除操作是长时间运行的操作 (LRO)
5. 监控任务数量 — 使用 跟踪进度maxWallClockTime
6. 设置约束 — 配置 和 maxTaskRetryCount`
7. 使用低优先级节点 — 为容错工作负载节省成本
8. 启用自动缩放 — 根据工作负载动态调整池大小
参考链接
| 资源 | URL |
|----------|-----|
| Maven 包 |
| Maven | https://central.sonatype.com/artifact/com.azure/azure-compute-batch |
| GitHub | https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/batch/azure-compute-batch |
| API 文档 | https://learn.microsoft.com/java/api/com.azure.compute.batch |
| 产品文档 | https://learn.microsoft.com/azure/batch/ |
| REST API | https://learn.microsoft.com/rest/api/batchservice/ |
| 示例 | https://github.com/azure/azure-batch-samples |
使用场景
本技能适用于执行概览中所描述的工作流或操作。局限性
- 仅在任务与上述范围明确匹配时使用本技能。
- 不要将输出结果视为针对特定环境的验证、测试或专家评审的替代方案。
- 如果缺少必要的输入、权限、安全边界或成功标准,请停止操作并寻求澄清。