列出作业
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
列出特定位置的 Batch 作业。注意:作业的位置可能与作业的运行位置不同。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content provides code samples in C++, Go, Java, Node.js, and Python for listing Batch jobs within a specified location.\u003c/p\u003e\n"],["\u003cp\u003eThe location of a Batch job may be different from the location where it executes.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample requires setting up Application Default Credentials for authentication with Batch.\u003c/p\u003e\n"],["\u003cp\u003eEach language section includes a link to its respective Batch API reference documentation for more detailed information.\u003c/p\u003e\n"],["\u003cp\u003eThe content also includes a link to explore other code samples for other Google Cloud products using the Google Cloud sample browser.\u003c/p\u003e\n"]]],[],null,["# List jobs\n\nList your Batch jobs from a specific location. Note: a job's location might differ from where that job runs.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [View jobs and tasks](/batch/docs/view-jobs-tasks)\n\nCode sample\n-----------\n\n### C++\n\n\nFor more information, see the\n[Batch C++ API\nreference documentation](/cpp/docs/reference/batch/latest).\n\n\nTo authenticate to Batch, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n #include \"google/cloud/batch/v1/batch_client.h\"\n\n [](std::string const& project_id, std::string const& location_id) {\n auto const parent = \"projects/\" + project_id + \"/locations/\" + location_id;\n // Initialize a client and issue the request.\n auto client = google::cloud::batch_v1::BatchServiceClient(\n google::cloud::batch_v1::MakeBatchServiceConnection());\n int i = 0;\n for (auto job : client.ListJobs(parent)) {\n if (!job) throw std::move(job).status();\n std::cout \u003c\u003c \"Job[\" \u003c\u003c i++ \u003c\u003c \"] \" \u003c\u003c job-\u003eDebugString() \u003c\u003c \"\\n\";\n }\n }\n\n### Go\n\n\nFor more information, see the\n[Batch Go API\nreference documentation](/go/docs/reference/cloud.google.com/go/batch/latest/apiv1).\n\n\nTo authenticate to Batch, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import (\n \t\"context\"\n \t\"fmt\"\n \t\"io\"\n\n \tbatch \"cloud.google.com/go/batch/apiv1\"\n \t\"cloud.google.com/go/batch/apiv1/batchpb\"\n \t\"google.golang.org/api/iterator\"\n )\n\n // Lists all jobs in the given project and region\n func listJobs(w io.Writer, projectID, region string) error {\n \t// projectID := \"your_project_id\"\n \t// region := \"us-central1\"\n\n \tctx := context.Background()\n \tbatchClient, err := batch.https://cloud.google.com/go/docs/reference/cloud.google.com/go/batch/latest/apiv1.html#cloud_google_com_go_batch_apiv1_Client_NewClient(ctx)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"NewClient: %w\", err)\n \t}\n \tdefer batchClient.Close()\n\n \treq := &batchpb.ListJobsRequest{\n \t\tParent: fmt.Sprintf(\"projects/%s/locations/%s\", projectID, region),\n \t}\n\n \tvar jobs []*batchpb.Job\n \tit := batchClient.ListJobs(ctx, req)\n\n \tfor {\n \t\tjob, err := it.Next()\n \t\tif err == iterator.Done {\n \t\t\tbreak\n \t\t}\n \t\tif err != nil {\n \t\t\treturn fmt.Errorf(\"unable to list jobs: %w\", err)\n \t\t}\n \t\tjobs = append(jobs, job)\n \t}\n\n \tfmt.Fprintf(w, \"Jobs: %v\\n\", jobs)\n\n \treturn nil\n }\n\n### Java\n\n\nFor more information, see the\n[Batch Java API\nreference documentation](/java/docs/reference/google-cloud-batch/latest/overview).\n\n\nTo authenticate to Batch, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.cloud.batch.v1.https://cloud.google.com/java/docs/reference/google-cloud-batch/latest/com.google.cloud.batch.v1.BatchServiceClient.html;\n import com.google.cloud.batch.v1.https://cloud.google.com/java/docs/reference/google-cloud-batch/latest/com.google.cloud.batch.v1.Job.html;\n import java.io.IOException;\n\n public class ListJobs {\n\n public static void main(String[] args) throws IOException {\n // TODO(developer): Replace these variables before running the sample.\n // Project ID or project number of the Cloud project you want to use.\n String projectId = \"YOUR_PROJECT_ID\";\n\n // Name of the region hosting the jobs.\n String region = \"europe-central2\";\n\n listJobs(projectId, region);\n }\n\n // Get a list of all jobs defined in given region.\n public static void listJobs(String projectId, String region) throws IOException {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests. After completing all of your requests, call\n // the `batchServiceClient.close()` method on the client to safely\n // clean up any remaining background resources.\n try (https://cloud.google.com/java/docs/reference/google-cloud-batch/latest/com.google.cloud.batch.v1.BatchServiceClient.html batchServiceClient = https://cloud.google.com/java/docs/reference/google-cloud-batch/latest/com.google.cloud.batch.v1.BatchServiceClient.html.create()) {\n\n // Construct the parent path of the job.\n String parent = String.format(\"projects/%s/locations/%s\", projectId, region);\n\n for (https://cloud.google.com/java/docs/reference/google-cloud-batch/latest/com.google.cloud.batch.v1.Job.html job : batchServiceClient.listJobs(parent).iterateAll()) {\n System.out.println(job.getName());\n }\n System.out.println(\"Listed all batch jobs.\");\n }\n }\n }\n\n### Node.js\n\n\nFor more information, see the\n[Batch Node.js API\nreference documentation](/nodejs/docs/reference/batch/latest).\n\n\nTo authenticate to Batch, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n /**\n * TODO(developer): Uncomment and replace these variables before running the sample.\n */\n // const projectId = 'YOUR_PROJECT_ID';\n /**\n * The region that hosts the job.\n */\n // const region = 'us-central-1';\n\n // Imports the Batch library\n const batchLib = require('https://cloud.google.com/nodejs/docs/reference/batch/latest/overview.html');\n\n // Instantiates a client\n const batchClient = new batchLib.v1.https://cloud.google.com/nodejs/docs/reference/batch/latest/overview.html();\n\n async function callListJobs() {\n // Construct request\n const request = {\n parent: `projects/${projectId}/locations/${region}`,\n };\n\n // Run request\n const iterable = await batchClient.listJobsAsync(request);\n for await (const response of iterable) {\n console.log(response);\n }\n }\n\n await callListJobs();\n\n### Python\n\n\nFor more information, see the\n[Batch Python API\nreference documentation](/python/docs/reference/batch/latest).\n\n\nTo authenticate to Batch, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from __future__ import annotations\n\n from collections.abc import Iterable\n\n from google.cloud import batch_v1\n\n\n def list_jobs(project_id: str, region: str) -\u003e Iterable[batch_v1.Job]:\n \"\"\"\n Get a list of all jobs defined in given region.\n\n Args:\n project_id: project ID or project number of the Cloud project you want to use.\n region: name of the region hosting the jobs.\n\n Returns:\n An iterable collection of Job object.\n \"\"\"\n client = batch_v1.BatchServiceClient()\n\n return client.list_jobs(parent=f\"projects/{project_id}/locations/{region}\")\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=batch)."]]