1+ // Copyright 2024 Google LLC
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // http://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
14+
15+ package com .example .batch ;
16+
17+ // [START batch_labels_job]
18+
19+ import com .google .cloud .batch .v1 .BatchServiceClient ;
20+ import com .google .cloud .batch .v1 .ComputeResource ;
21+ import com .google .cloud .batch .v1 .CreateJobRequest ;
22+ import com .google .cloud .batch .v1 .Job ;
23+ import com .google .cloud .batch .v1 .LogsPolicy ;
24+ import com .google .cloud .batch .v1 .Runnable ;
25+ import com .google .cloud .batch .v1 .TaskGroup ;
26+ import com .google .cloud .batch .v1 .TaskSpec ;
27+ import com .google .protobuf .Duration ;
28+ import java .io .IOException ;
29+ import java .util .concurrent .ExecutionException ;
30+ import java .util .concurrent .TimeUnit ;
31+ import java .util .concurrent .TimeoutException ;
32+
33+
34+ public class CreateBatchLabelJob {
35+
36+ public static void main (String [] args )
37+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
38+ // TODO(developer): Replace these variables before running the sample.
39+ // Project ID or project number of the Google Cloud project you want to use.
40+ String projectId = "YOUR_PROJECT_ID" ;
41+ // Name of the region you want to use to run the job. Regions that are
42+ // available for Batch are listed on: https://cloud.google.com/batch/docs/get-started#locations
43+ String region = "us-central1" ;
44+ // The name of the job that will be created.
45+ // It needs to be unique for each project and region pair.
46+ String jobName = "example-job" ;
47+ // Name of the label1 to be applied for your Job.
48+ String labelName1 = "JOB_LABEL_NAME1" ;
49+ // Value for the label1 to be applied for your Job.
50+ String labelValue1 = "JOB_LABEL_VALUE1" ;
51+ // Name of the label2 to be applied for your Job.
52+ String labelName2 = "JOB_LABEL_NAME2" ;
53+ // Value for the label2 to be applied for your Job.
54+ String labelValue2 = "JOB_LABEL_VALUE2" ;
55+
56+ createBatchLabelJob (projectId , region , jobName , labelName1 ,
57+ labelValue1 , labelName2 , labelValue2 );
58+ }
59+
60+ // Creates a job with labels defined in the labels field.
61+ public static Job createBatchLabelJob (String projectId , String region , String jobName ,
62+ String labelName1 , String labelValue1 , String labelName2 , String labelValue2 )
63+ throws IOException , ExecutionException , InterruptedException , TimeoutException {
64+ // Initialize client that will be used to send requests. This client only needs to be created
65+ // once, and can be reused for multiple requests.
66+ try (BatchServiceClient batchServiceClient = BatchServiceClient .create ()) {
67+
68+ // Define what will be done as part of the job.
69+ Runnable runnable =
70+ Runnable .newBuilder ()
71+ .setContainer (
72+ Runnable .Container .newBuilder ()
73+ .setImageUri ("gcr.io/google-containers/busybox" )
74+ .setEntrypoint ("/bin/sh" )
75+ .addCommands ("-c" )
76+ .addCommands (
77+ "echo Hello world! This is task ${BATCH_TASK_INDEX}. "
78+ + "This job has a total of ${BATCH_TASK_COUNT} tasks." )
79+ .build ())
80+ .build ();
81+
82+ // We can specify what resources are requested by each task.
83+ ComputeResource computeResource =
84+ ComputeResource .newBuilder ()
85+ // In milliseconds per cpu-second. This means the task requires 50% of a single CPUs.
86+ .setCpuMilli (2000 )
87+ // In MiB.
88+ .setMemoryMib (2000 )
89+ .build ();
90+
91+ TaskSpec task =
92+ TaskSpec .newBuilder ()
93+ // Jobs can be divided into tasks. In this case, we have only one task.
94+ .addRunnables (runnable )
95+ .setComputeResource (computeResource )
96+ .setMaxRetryCount (2 )
97+ .setMaxRunDuration (Duration .newBuilder ().setSeconds (3600 ).build ())
98+ .build ();
99+
100+ // Tasks are grouped inside a job using TaskGroups.
101+ // Currently, it's possible to have only one task group.
102+ TaskGroup taskGroup = TaskGroup .newBuilder ().setTaskCount (1 ).setTaskSpec (task ).build ();
103+
104+ Job job =
105+ Job .newBuilder ()
106+ .addTaskGroups (taskGroup )
107+ // We use Cloud Logging as it's an out of the box available option.
108+ .setLogsPolicy (LogsPolicy .newBuilder ()
109+ .setDestination (LogsPolicy .Destination .CLOUD_LOGGING ).build ())
110+ // Labels and their value to be applied to the job.
111+ .putLabels (labelName1 , labelValue1 )
112+ .putLabels (labelName2 , labelValue2 )
113+ .build ();
114+
115+ CreateJobRequest createJobRequest =
116+ CreateJobRequest .newBuilder ()
117+ // The job's parent is the region in which the job will run.
118+ .setParent (String .format ("projects/%s/locations/%s" , projectId , region ))
119+ .setJob (job )
120+ .setJobId (jobName )
121+ .build ();
122+
123+ Job result =
124+ batchServiceClient
125+ .createJobCallable ()
126+ .futureCall (createJobRequest )
127+ .get (5 , TimeUnit .MINUTES );
128+
129+ System .out .printf ("Successfully created the job: %s" , result .getName ());
130+
131+ return result ;
132+ }
133+ }
134+
135+ }
136+ // [END batch_labels_job]
0 commit comments