查看作业和任务

使用集合让一切井井有条 根据您的偏好保存内容并对其进行分类。

本页面介绍如何查看批量作业及其任务。

准备工作

  • 如果您之前未使用过 Batch,请查看开始使用 Batch,然后完成项目和用户需满足的前提条件以启用 Batch。
  • 如需获取查看作业和任务所需的权限,请让管理员授予您项目的 Batch Job Viewer (roles/batch.jobsViewer) 或 Batch Job Editor (roles/batch.jobsEditor) IAM 角色。 如需详细了解如何授予角色,请参阅管理访问权限

列出和描述作业

列出作业以查看项目中的作业。描述作业以查看其配置详情。

列出作业

您可以使用 Google Cloud 控制台、gcloud CLI、Batch API、Go、Java、Node.js 或 Python 列出作业。

控制台

要列出项目的作业,请在 Google Cloud 控制台中转到作业列表页面。

转到作业列表

gcloud

如需使用 gcloud CLI 列出项目的作业,请使用 gcloud batch jobs list 命令

gcloud batch jobs list

API

如需使用 Batch API 列出特定位置的项目的作业,请使用 jobs.list 方法

GET https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs/

请替换以下内容:

  • PROJECT_ID:项目的项目 ID
  • LOCATION:作业的位置
  • JOB_NAME:作业的名称。

Go

Go

如需了解详情,请参阅 Batch Go API 参考文档

import (
	"context"
	"fmt"
	"io"

	batch "cloud.google.com/go/batch/apiv1"
	"cloud.google.com/go/batch/apiv1/batchpb"
	"google.golang.org/api/iterator"
)

// Lists all jobs in the given project and region
func listJobs(w io.Writer, projectID, region string) error {
	// projectID := "your_project_id"
	// region := "us-central1"

	ctx := context.Background()
	batchClient, err := batch.NewClient(ctx)
	if err != nil {
		return fmt.Errorf("NewClient: %v", err)
	}
	defer batchClient.Close()

	req := &batchpb.ListJobsRequest{
		Parent: fmt.Sprintf("projects/%s/locations/%s", projectID, region),
	}

	var jobs []*batchpb.Job
	it := batchClient.ListJobs(ctx, req)

	for {
		job, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			return fmt.Errorf("unable to list jobs: %v", err)
		}
		jobs = append(jobs, job)
	}

	fmt.Fprintf(w, "Jobs: %v\n", jobs)

	return nil
}

Java

Java

如需了解详情,请参阅 Batch Java API 参考文档


import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.Job;
import java.io.IOException;

public class ListJobs {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";

    // Name of the region hosting the jobs.
    String region = "europe-central2";

    listJobs(projectId, region);
  }

  // Get a list of all jobs defined in given region.
  public static void listJobs(String projectId, String region) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the `batchServiceClient.close()` method on the client to safely
    // clean up any remaining background resources.
    try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {

      // Construct the parent path of the job.
      String parent = String.format("projects/%s/locations/%s", projectId, region);

      for (Job job : batchServiceClient.listJobs(parent).iterateAll()) {
        System.out.println(job.getName());
      }
      System.out.println("Listed all batch jobs.");
    }
  }
}

Node.js

Node.js

如需了解详情,请参阅 Batch Node.js API 参考文档

/**
 * TODO(developer): Uncomment and replace these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
/**
 * The region that hosts the job.
 */
// const region = 'us-central-1';

// Imports the Batch library
const batchLib = require('@google-cloud/batch');

// Instantiates a client
const batchClient = new batchLib.v1.BatchServiceClient();

async function callListJobs() {
  // Construct request
  const request = {
    parent: `projects/${projectId}/locations/${region}`,
  };

  // Run request
  const iterable = await batchClient.listJobsAsync(request);
  for await (const response of iterable) {
    console.log(response);
  }
}

callListJobs();

Python

Python

如需了解详情,请参阅 Batch Python API 参考文档

from typing import Iterable

from google.cloud import batch_v1

def list_jobs(project_id: str, region: str) -> Iterable[batch_v1.Job]:
    """
    Get a list of all jobs defined in given region.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        region: name of the region hosting the jobs.

    Returns:
        An iterable collection of Job object.
    """
    client = batch_v1.BatchServiceClient()

    return client.list_jobs(parent=f"projects/{project_id}/locations/{region}")

描述作业

您可以使用 Google Cloud 控制台、gcloud CLI、Batch API、Go、Java、Node.js 或 Python 描述作业。

控制台

  1. 要列出项目的作业,请在 Google Cloud 控制台中转到作业列表页面。

    转到作业列表

  2. 如需描述作业,请在作业名称列中点击作业名称。 系统会打开作业详情页面。

gcloud

如需使用 gcloud CLI 描述作业,请使用 gcloud batch jobs describe 命令

gcloud batch jobs describe JOB_NAME --location LOCATION

请替换以下内容:

  • JOB_NAME:作业的名称。
  • LOCATION:作业的位置

API

如需使用 Batch API 描述作业,请使用 jobs.get 方法

GET https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs/JOB_NAME

请替换以下内容:

  • PROJECT_ID:项目的项目 ID
  • LOCATION:作业的位置
  • JOB_NAME:作业的名称。

Go

Go

如需了解详情,请参阅 Batch Go API 参考文档

import (
	"context"
	"fmt"
	"io"

	batch "cloud.google.com/go/batch/apiv1"
	"cloud.google.com/go/batch/apiv1/batchpb"
)

// Retrieves the information about the specified job, most importantly its status
func getJob(w io.Writer, projectID, region, jobName string) (*batchpb.Job, error) {
	// projectID := "your_project_id"
	// region := "us-central1"
	// jobName := "some-job"

	ctx := context.Background()
	batchClient, err := batch.NewClient(ctx)
	if err != nil {
		return nil, fmt.Errorf("NewClient: %v", err)
	}
	defer batchClient.Close()

	req := &batchpb.GetJobRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/jobs/%s", projectID, region, jobName),
	}

	response, err := batchClient.GetJob(ctx, req)
	if err != nil {
		return nil, fmt.Errorf("unable to get job: %v", err)
	}

	fmt.Fprintf(w, "Job info: %v\n", response)

	return response, nil
}

Java

Java

如需了解详情,请参阅 Batch Java API 参考文档


import com.google.cloud.batch.v1.BatchServiceClient;
import com.google.cloud.batch.v1.Job;
import com.google.cloud.batch.v1.JobName;
import java.io.IOException;

public class GetJob {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Cloud project you want to use.
    String projectId = "YOUR_PROJECT_ID";

    // Name of the region hosts the job.
    String region = "europe-central2";

    // The name of the job you want to retrieve information about.
    String jobName = "JOB_NAME";

    getJob(projectId, region, jobName);
  }

  // Retrieve information about a Batch Job.
  public static void getJob(String projectId, String region, String jobName) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the `batchServiceClient.close()` method on the client to safely
    // clean up any remaining background resources.
    try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {

      Job job =
          batchServiceClient.getJob(
              JobName.newBuilder()
                  .setProject(projectId)
                  .setLocation(region)
                  .setJob(jobName)
                  .build());

      System.out.printf("Retrieved the job: %s ", job.getName());
    }
  }
}

Node.js

Node.js

如需了解详情,请参阅 Batch Node.js API 参考文档

/**
 * TODO(developer): Uncomment and replace these variables before running the sample.
 */
// const projectId = 'YOUR_PROJECT_ID';
/**
 * The region that hosts the job.
 */
// const region = 'us-central-1';
/**
 * The name of the job you want to retrieve information about.
 */
// const jobName = 'YOUR_JOB_NAME';

// Imports the Batch library
const batchLib = require('@google-cloud/batch');

// Instantiates a client
const batchClient = new batchLib.v1.BatchServiceClient();

async function callGetJob() {
  // Construct request
  const request = {
    name: `projects/${projectId}/locations/${region}/jobs/${jobName}`,
  };

  // Run request
  const response = await batchClient.getJob(request);
  console.log(response);
}

callGetJob();

Python

Python

如需了解详情,请参阅 Batch Python API 参考文档


from google.cloud import batch_v1

def get_job(project_id: str, region: str, job_name: str) -> batch_v1.Job:
    """
    Retrieve information about a Batch Job.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        region: name of the region hosts the job.
        job_name: the name of the job you want to retrieve information about.

    Returns:
        A Job object representing the specified job.
    """
    client = batch_v1.BatchServiceClient()

    return client.get_job(name=f"projects/{project_id}/locations/{region}/jobs/{job_name}")

列出和描述任务

列出任务以查看作业或任务的任务组中的任务。描述任务以查看其配置详情。 您可以使用 Google Cloud 控制台、gcloud CLI 或 Batch API 列出和描述作业的任务。

控制台

如需使用 Google Cloud 控制台查看作业的任务详情,请描述作业以打开作业详情页面,然后查看任务详情部分。

gcloud

列出任务

如需使用 gcloud CLI 列出作业任务组的任务,请使用 gcloud batch tasks list 命令

gcloud batch tasks list --location LOCATION \
  --job JOB_NAME

请替换以下内容:

  • LOCATION:作业的位置
  • JOB_NAME:作业的名称。

描述任务

如需使用 gcloud CLI 描述任务,请使用 gcloud batch tasks describe 命令

gcloud batch tasks describe TASK_INDEX \
  --location LOCATION \
  --job JOB_NAME \
  --task_group TASK_GROUP_NAME

请替换以下内容:

  • TASK_INDEX:您要描述的任务的索引。任务组中任务的索引从 0 开始,针对每个任务递增 1。例如,包含四个任务的任务组的索引为 0123
  • TASK_GROUP_NAME:包含您要描述的任务的任务组的名称。请将此项设置为 group0
  • JOB_NAME:作业的名称。
  • LOCATION:作业的位置

API

列出任务

如需使用 Batch API 列出作业任务组的任务,请使用 tasks.list 方法

GET https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs/JOB_NAME/taskGroups/TASK_GROUP_NAME/tasks

请替换以下内容:

  • PROJECT_ID:项目的项目 ID
  • LOCATION:作业的位置
  • JOB_NAME:作业的名称。
  • TASK_GROUP_NAME:您要描述的任务组的名称。将其设置为 group0

描述任务

如需使用 Batch API 描述任务,请使用 tasks.get 方法

GET https://batch.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/jobs/JOB_NAME/taskGroups/TASK_GROUP_NAME/tasks/TASK_INDEX

请替换以下内容:

  • PROJECT_ID:项目的项目 ID
  • LOCATION:作业的位置
  • JOB_NAME:作业的名称。
  • TASK_GROUP_NAME:包含您要描述的任务的任务组的名称。请将此项设置为 group0
  • TASK_INDEX:您要描述的任务的索引。任务组中任务的索引从 0 开始,针对每个任务递增 1。例如,包含四个任务的任务组的索引为 0123

后续步骤