Descrivere un'attività
Mantieni tutto organizzato con le raccolte
Salva e classifica i contenuti in base alle tue preferenze.
Un job Batch è costituito da una o più attività. Questo esempio recupera il nome e le informazioni dettagliate sullo stato di una determinata attività.
Per saperne di più
Per la documentazione dettagliata che include questo esempio di codice, vedi quanto segue:
Esempio di codice
Salvo quando diversamente specificato, i contenuti di questa pagina sono concessi in base alla licenza Creative Commons Attribution 4.0, mentre gli esempi di codice sono concessi in base alla licenza Apache 2.0. Per ulteriori dettagli, consulta le norme del sito di Google Developers. Java è un marchio registrato di Oracle e/o delle sue consociate.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Difficile da capire","hardToUnderstand","thumb-down"],["Informazioni o codice di esempio errati","incorrectInformationOrSampleCode","thumb-down"],["Mancano le informazioni o gli esempi di cui ho bisogno","missingTheInformationSamplesINeed","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Altra","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis content provides code samples in C++, Go, Java, Node.js, and Python for retrieving the name and detailed status information of a specific task within a Batch job.\u003c/p\u003e\n"],["\u003cp\u003eEach code sample demonstrates how to use the respective language's Batch API to get task information by specifying the project ID, region, job name, task group, and task number.\u003c/p\u003e\n"],["\u003cp\u003eThe examples utilize Application Default Credentials for authentication, with links provided to setup authentication for a local environment, and each API call is linked to their respective API reference documentation.\u003c/p\u003e\n"],["\u003cp\u003eThe main functionality detailed in the content is \u003ccode\u003egetTask\u003c/code\u003e, to retrieve information about the specified job, most importantly its status.\u003c/p\u003e\n"],["\u003cp\u003eThis is a sample of code to be implemented in the specific environment that the user wishes to retrieve information from, as each sample code has variables that need to be replaced with the user's data.\u003c/p\u003e\n"]]],[],null,["# Describe a task\n\nA Batch job consists of one or several tasks. This sample retrieves the name and detailed status info for a given task.\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, std::string const& group_id,\n std::string const& task_number) {\n auto const name = \"projects/\" + project_id + \"/locations/\" + location_id +\n \"/jobs/\" + job_id + \"/taskGroups/\" + group_id +\n \"/tasks/\" + task_number;\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.GetTask(name);\n if (!response) throw std::move(response).status();\n std::cout \u003c\u003c \"GetTask() 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 getTask(w io.Writer, projectID, region, jobName, taskGroup string, taskNumber int32) error {\n \t// projectID := \"your_project_id\"\n \t// region := \"us-central1\"\n \t// jobName := \"some-job\"\n \t// taskGroup := \"group0\" // defaults to \"group0\" on job creation unless overridden\n \t// taskNumber := 0\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.GetTaskRequest{\n \t\tName: fmt.Sprintf(\"projects/%s/locations/%s/jobs/%s/taskGroups/%s/tasks/%d\",\n \t\t\tprojectID, region, jobName, taskGroup, taskNumber),\n \t}\n\n \tresponse, err := batchClient.GetTask(ctx, req)\n \tif err != nil {\n \t\treturn fmt.Errorf(\"unable to get task: %w\", err)\n \t}\n\n \tfmt.Fprintf(w, \"Task info: %v\\n\", response)\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.Task.html;\n import com.google.cloud.batch.v1.https://cloud.google.com/java/docs/reference/google-cloud-batch/latest/com.google.cloud.batch.v1.TaskName.html;\n import java.io.IOException;\n\n public class GetTask {\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 // Name of the region hosts the job.\n String region = \"europe-central2\";\n // The name of the job you want to retrieve information about.\n String jobName = \"JOB_NAME\";\n // The name of the group that owns the task you want to check. Usually it's `group0`.\n String groupName = \"group0\";\n // Number of the task you want to look up.\n int taskNumber = 0;\n\n getTask(projectId, region, jobName, groupName, taskNumber);\n }\n\n // Retrieve information about a Task.\n public static void getTask(String projectId, String region, String jobName, String groupName,\n int taskNumber) 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.Task.html task = batchServiceClient.getTask(https://cloud.google.com/java/docs/reference/google-cloud-batch/latest/com.google.cloud.batch.v1.TaskName.html.newBuilder()\n .setProject(projectId)\n .setLocation(region)\n .setJob(jobName)\n .setTaskGroup(groupName)\n .https://cloud.google.com/java/docs/reference/google-cloud-batch/latest/com.google.cloud.batch.v1.TaskName.Builder.html#com_google_cloud_batch_v1_TaskName_Builder_setTask_java_lang_String_(String.valueOf(taskNumber))\n .build());\n System.out.printf(\"Retrieved task information: %s\", task.https://cloud.google.com/java/docs/reference/google-cloud-batch/latest/com.google.cloud.batch.v1.Task.html#com_google_cloud_batch_v1_Task_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 * The name of the group that owns the task you want to check.\n * Usually it's `group0`.\n */\n // const groupName = 'group0';\n /**\n * The number of the task you want to look up.\n */\n // const taskNumber = 0;\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:\n `projects/${projectId}/locations/${region}/jobs/${jobName}` +\n `/taskGroups/${groupName}/tasks/${taskNumber}`,\n };\n\n // Run request\n const response = await batchClient.getTask(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_task(\n project_id: str, region: str, job_name: str, group_name: str, task_number: int\n ) -\u003e batch_v1.Task:\n \"\"\"\n Retrieve information about a Task.\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 group_name: the name of the group that owns the task you want to check. Usually it's `group0`.\n task_number: number of the task you want to look up.\n\n Returns:\n A Task object representing the specified task.\n \"\"\"\n client = batch_v1.https://cloud.google.com/python/docs/reference/batch/latest/google.cloud.batch_v1.services.batch_service.BatchServiceClient.html()\n\n return client.https://cloud.google.com/python/docs/reference/batch/latest/google.cloud.batch_v1.services.batch_service.BatchServiceClient.html#google_cloud_batch_v1_services_batch_service_BatchServiceClient_get_task(\n name=f\"projects/{project_id}/locations/{region}/jobs/{job_name}\"\n f\"/taskGroups/{group_name}/tasks/{task_number}\"\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)."]]