說明工作
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
查看批次工作的詳細資料。
深入探索
如需包含這個程式碼範例的詳細說明文件,請參閱下列內容:
程式碼範例
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。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 viewing details about a Batch job.\u003c/p\u003e\n"],["\u003cp\u003eEach code example demonstrates how to retrieve information about a specific job using the Batch API, including its status.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves setting up Application Default Credentials for authentication and initializing a Batch service client.\u003c/p\u003e\n"],["\u003cp\u003eThe code samples utilize specific API functions, such as \u003ccode\u003eGetJob\u003c/code\u003e, to fetch the job details from a designated project, region, and job name.\u003c/p\u003e\n"],["\u003cp\u003eThe documentation also offers links to detailed API references for each language, as well as a link to the Google Cloud sample browser.\u003c/p\u003e\n"]]],[],null,["# Describe a job\n\nView details about a Batch job.\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 std::string const& job_id) {\n auto const name = \"projects/\" + project_id + \"/locations/\" + location_id +\n \"/jobs/\" + job_id;\n // Initialize a client and issue the request.\n auto client = google::cloud::batch_v1::BatchServiceClient(\n google::cloud::batch_v1::MakeBatchServiceConnection());\n auto response = client.GetJob(name);\n if (!response) throw std::move(response).status();\n std::cout \u003c\u003c \"GetJob() succeeded with \" \u003c\u003c response-\u003eDebugString() \u003c\u003c \"\\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 )\n\n // Retrieves the information about the specified job, most importantly its status\n func getJob(w io.Writer, projectID, region, jobName string) (*batchpb.Job, error) {\n \t// projectID := \"your_project_id\"\n \t// region := \"us-central1\"\n \t// jobName := \"some-job\"\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 nil, fmt.Errorf(\"NewClient: %w\", err)\n \t}\n \tdefer batchClient.Close()\n\n \treq := &batchpb.GetJobRequest{\n \t\tName: fmt.Sprintf(\"projects/%s/locations/%s/jobs/%s\", projectID, region, jobName),\n \t}\n\n \tresponse, err := batchClient.GetJob(ctx, req)\n \tif err != nil {\n \t\treturn nil, fmt.Errorf(\"unable to get job: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Job info: %v\\n\", response)\n\n \treturn response, 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 com.google.cloud.batch.v1.https://cloud.google.com/java/docs/reference/google-cloud-batch/latest/com.google.cloud.batch.v1.JobName.html;\n import java.io.IOException;\n\n public class GetJob {\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 hosts the job.\n String region = \"europe-central2\";\n\n // The name of the job you want to retrieve information about.\n String jobName = \"JOB_NAME\";\n\n getJob(projectId, region, jobName);\n }\n\n // Retrieve information about a Batch Job.\n public static void getJob(String projectId, String region, String jobName) 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 https://cloud.google.com/java/docs/reference/google-cloud-batch/latest/com.google.cloud.batch.v1.Job.html job =\n batchServiceClient.getJob(\n https://cloud.google.com/java/docs/reference/google-cloud-batch/latest/com.google.cloud.batch.v1.JobName.html.newBuilder()\n .setProject(projectId)\n .setLocation(region)\n .setJob(jobName)\n .build());\n\n System.out.printf(\"Retrieved the job: %s \", job.https://cloud.google.com/java/docs/reference/google-cloud-batch/latest/com.google.cloud.batch.v1.Job.html#com_google_cloud_batch_v1_Job_getName__());\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 * The name of the job you want to retrieve information about.\n */\n // const jobName = 'YOUR_JOB_NAME';\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 callGetJob() {\n // Construct request\n const request = {\n name: `projects/${projectId}/locations/${region}/jobs/${jobName}`,\n };\n\n // Run request\n const response = await batchClient.getJob(request);\n console.log(response);\n }\n\n await callGetJob();\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\n from google.cloud import batch_v1\n\n\n def get_job(project_id: str, region: str, job_name: str) -\u003e batch_v1.Job:\n \"\"\"\n Retrieve information about a Batch Job.\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 hosts the job.\n job_name: the name of the job you want to retrieve information about.\n\n Returns:\n A Job object representing the specified job.\n \"\"\"\n client = batch_v1.BatchServiceClient()\n\n return client.get_job(\n name=f\"projects/{project_id}/locations/{region}/jobs/{job_name}\"\n )\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)."]]