Visitar URL original
docs(samples): added basic samples and tests by Sita04 · Pull Request #43 · googleapis/java-batch · GitHub
Skip to content
This repository was archived by the owner on Sep 16, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
🦉 Updates from OwlBot post-processor
  • Loading branch information
gcf-owl-bot[bot] committed Sep 19, 2022
commit 06d91d12b0f3d7b39fb856dea2a7e4e00779ba33
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ If you are using Maven, add this to your pom.xml file:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-batch</artifactId>
<version>0.2.2</version>
<version>0.3.0</version>
</dependency>
```

If you are using Gradle without BOM, add this to your dependencies:

```Groovy
implementation 'com.google.cloud:google-cloud-batch:0.2.2'
implementation 'com.google.cloud:google-cloud-batch:0.3.0'
```

If you are using SBT, add this to your dependencies:

```Scala
libraryDependencies += "com.google.cloud" % "google-cloud-batch" % "0.2.2"
libraryDependencies += "com.google.cloud" % "google-cloud-batch" % "0.3.0"
```

## Authentication
Expand Down Expand Up @@ -73,6 +73,19 @@ use this Cloud Batch Client Library.



## Samples

Samples are in the [`samples/`](https://github.com/googleapis/java-batch/tree/main/samples) directory.

| Sample | Source Code | Try it |
| --------------------------- | --------------------------------- | ------ |
| Create With Container No Mounting | [source code](https://github.com/googleapis/java-batch/blob/main/samples/snippets/src/main/java/CreateWithContainerNoMounting.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-batch&page=editor&open_in_editor=samples/snippets/src/main/java/CreateWithContainerNoMounting.java) |
| Create With Script No Mounting | [source code](https://github.com/googleapis/java-batch/blob/main/samples/snippets/src/main/java/CreateWithScriptNoMounting.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-batch&page=editor&open_in_editor=samples/snippets/src/main/java/CreateWithScriptNoMounting.java) |
| Delete Job | [source code](https://github.com/googleapis/java-batch/blob/main/samples/snippets/src/main/java/DeleteJob.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-batch&page=editor&open_in_editor=samples/snippets/src/main/java/DeleteJob.java) |
| Get Job | [source code](https://github.com/googleapis/java-batch/blob/main/samples/snippets/src/main/java/GetJob.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-batch&page=editor&open_in_editor=samples/snippets/src/main/java/GetJob.java) |
| List Jobs | [source code](https://github.com/googleapis/java-batch/blob/main/samples/snippets/src/main/java/ListJobs.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-batch&page=editor&open_in_editor=samples/snippets/src/main/java/ListJobs.java) |



## Troubleshooting

Expand Down
123 changes: 64 additions & 59 deletions samples/snippets/src/main/java/CreateWithContainerNoMounting.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,76 +63,81 @@ public static void createContainerJob(String projectId, String region, String jo
try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {

// Define what will be done as part of the job.
Runnable runnable = Runnable.newBuilder()
.setContainer(Container.newBuilder()
.setImageUri("gcr.io/google-containers/busybox")
.setEntrypoint("/bin/sh")
.addCommands("-c")
.addCommands(
"echo Hello world! This is task ${BATCH_TASK_INDEX}. "
+ "This job has a total of ${BATCH_TASK_COUNT} tasks.")
.build())
.build();
Runnable runnable =
Runnable.newBuilder()
.setContainer(
Container.newBuilder()
.setImageUri("gcr.io/google-containers/busybox")
.setEntrypoint("/bin/sh")
.addCommands("-c")
.addCommands(
"echo Hello world! This is task ${BATCH_TASK_INDEX}. "
+ "This job has a total of ${BATCH_TASK_COUNT} tasks.")
.build())
.build();

// We can specify what resources are requested by each task.
ComputeResource computeResource = ComputeResource.newBuilder()
// In milliseconds per cpu-second. This means the task requires 2 whole CPUs.
.setCpuMilli(2000)
// In MiB.
.setMemoryMib(16)
.build();

TaskSpec task = TaskSpec.newBuilder()
// Jobs can be divided into tasks. In this case, we have only one task.
.addRunnables(runnable)
.setComputeResource(computeResource)
.setMaxRetryCount(2)
.setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
.build();
ComputeResource computeResource =
ComputeResource.newBuilder()
// In milliseconds per cpu-second. This means the task requires 2 whole CPUs.
.setCpuMilli(2000)
// In MiB.
.setMemoryMib(16)
.build();

TaskSpec task =
TaskSpec.newBuilder()
// Jobs can be divided into tasks. In this case, we have only one task.
.addRunnables(runnable)
.setComputeResource(computeResource)
.setMaxRetryCount(2)
.setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
.build();

// Tasks are grouped inside a job using TaskGroups.
TaskGroup taskGroup = TaskGroup.newBuilder()
.setTaskCount(4)
.setTaskSpec(task)
.build();
TaskGroup taskGroup = TaskGroup.newBuilder().setTaskCount(4).setTaskSpec(task).build();

// Policies are used to define on what kind of virtual machines the tasks will run on.
// In this case, we tell the system to use "e2-standard-4" machine type.
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
InstancePolicy instancePolicy = InstancePolicy.newBuilder()
.setMachineType("e2-standard-4")
.build();

AllocationPolicy allocationPolicy = AllocationPolicy.newBuilder()
.addInstances(InstancePolicyOrTemplate.newBuilder()
.setPolicy(instancePolicy)
.build())
.build();

Job job = Job.newBuilder()
.addTaskGroups(taskGroup)
.setAllocationPolicy(allocationPolicy)
.putLabels("env", "testing").putLabels("type", "container")
// We use Cloud Logging as it's an out of the box available option.
.setLogsPolicy(LogsPolicy.newBuilder()
.setDestination(Destination.CLOUD_LOGGING)
.build())
.build();

CreateJobRequest createJobRequest = CreateJobRequest.newBuilder()
// The job's parent is the region in which the job will run.
.setParent(String.format("projects/%s/locations/%s", projectId, region))
.setJob(job)
.setJobId(jobName)
.build();

Job result = batchServiceClient.createJobCallable().futureCall(createJobRequest)
.get(3, TimeUnit.MINUTES);
InstancePolicy instancePolicy =
InstancePolicy.newBuilder().setMachineType("e2-standard-4").build();

AllocationPolicy allocationPolicy =
AllocationPolicy.newBuilder()
.addInstances(InstancePolicyOrTemplate.newBuilder().setPolicy(instancePolicy).build())
.build();

Job job =
Job.newBuilder()
.addTaskGroups(taskGroup)
.setAllocationPolicy(allocationPolicy)
.putLabels("env", "testing")
.putLabels("type", "container")
// We use Cloud Logging as it's an out of the box available option.
.setLogsPolicy(
LogsPolicy.newBuilder().setDestination(Destination.CLOUD_LOGGING).build())
.build();

CreateJobRequest createJobRequest =
CreateJobRequest.newBuilder()
// The job's parent is the region in which the job will run.
.setParent(String.format("projects/%s/locations/%s", projectId, region))
.setJob(job)
.setJobId(jobName)
.build();

Job result =
batchServiceClient
.createJobCallable()
.futureCall(createJobRequest)
.get(3, TimeUnit.MINUTES);

String resultJobName = result.getName();
System.out.printf("Successfully created the job: %s",
System.out.printf(
"Successfully created the job: %s",
resultJobName.substring(resultJobName.lastIndexOf("\\") + 1));
}
}
}
// [END batch_create_container_job]
// [END batch_create_container_job]
126 changes: 66 additions & 60 deletions samples/snippets/src/main/java/CreateWithScriptNoMounting.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,77 +63,83 @@ public static void createScriptJob(String projectId, String region, String jobNa
try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {

// Define what will be done as part of the job.
Runnable runnable = Runnable.newBuilder()
.setScript(Script.newBuilder()
.setText(
"echo Hello world! This is task ${BATCH_TASK_INDEX}. "
+ "This job has a total of ${BATCH_TASK_COUNT} tasks.")
// You can also run a script from a file. Just remember, that needs to be a script that's
// already on the VM that will be running the job.
// Using setText() and setPath() is mutually exclusive.
// .setPath("/tmp/test.sh")
.build())
.build();
Runnable runnable =
Runnable.newBuilder()
.setScript(
Script.newBuilder()
.setText(
"echo Hello world! This is task ${BATCH_TASK_INDEX}. "
+ "This job has a total of ${BATCH_TASK_COUNT} tasks.")
// You can also run a script from a file. Just remember, that needs to be a
// script that's
// already on the VM that will be running the job.
// Using setText() and setPath() is mutually exclusive.
// .setPath("/tmp/test.sh")
.build())
.build();

// We can specify what resources are requested by each task.
ComputeResource computeResource = ComputeResource.newBuilder()
// In milliseconds per cpu-second. This means the task requires 2 whole CPUs.
.setCpuMilli(2000)
// In MiB.
.setMemoryMib(16)
.build();

TaskSpec task = TaskSpec.newBuilder()
// Jobs can be divided into tasks. In this case, we have only one task.
.addRunnables(runnable)
.setComputeResource(computeResource)
.setMaxRetryCount(2)
.setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
.build();
ComputeResource computeResource =
ComputeResource.newBuilder()
// In milliseconds per cpu-second. This means the task requires 2 whole CPUs.
.setCpuMilli(2000)
// In MiB.
.setMemoryMib(16)
.build();

TaskSpec task =
TaskSpec.newBuilder()
// Jobs can be divided into tasks. In this case, we have only one task.
.addRunnables(runnable)
.setComputeResource(computeResource)
.setMaxRetryCount(2)
.setMaxRunDuration(Duration.newBuilder().setSeconds(3600).build())
.build();

// Tasks are grouped inside a job using TaskGroups.
TaskGroup taskGroup = TaskGroup.newBuilder()
.setTaskCount(4)
.setTaskSpec(task)
.build();
TaskGroup taskGroup = TaskGroup.newBuilder().setTaskCount(4).setTaskSpec(task).build();

// Policies are used to define on what kind of virtual machines the tasks will run on.
// In this case, we tell the system to use "e2-standard-4" machine type.
// Read more about machine types here: https://cloud.google.com/compute/docs/machine-types
InstancePolicy instancePolicy = InstancePolicy.newBuilder()
.setMachineType("e2-standard-4")
.build();

AllocationPolicy allocationPolicy = AllocationPolicy.newBuilder()
.addInstances(InstancePolicyOrTemplate.newBuilder()
.setPolicy(instancePolicy)
.build())
.build();

Job job = Job.newBuilder()
.addTaskGroups(taskGroup)
.setAllocationPolicy(allocationPolicy)
.putLabels("env", "testing").putLabels("type", "script")
// We use Cloud Logging as it's an out of the box available option.
.setLogsPolicy(LogsPolicy.newBuilder()
.setDestination(Destination.CLOUD_LOGGING)
.build())
.build();

CreateJobRequest createJobRequest = CreateJobRequest.newBuilder()
// The job's parent is the region in which the job will run.
.setParent(String.format("projects/%s/locations/%s", projectId, region))
.setJob(job)
.setJobId(jobName)
.build();

Job result = batchServiceClient.createJobCallable().futureCall(createJobRequest)
.get(3, TimeUnit.MINUTES);
InstancePolicy instancePolicy =
InstancePolicy.newBuilder().setMachineType("e2-standard-4").build();

AllocationPolicy allocationPolicy =
AllocationPolicy.newBuilder()
.addInstances(InstancePolicyOrTemplate.newBuilder().setPolicy(instancePolicy).build())
.build();

Job job =
Job.newBuilder()
.addTaskGroups(taskGroup)
.setAllocationPolicy(allocationPolicy)
.putLabels("env", "testing")
.putLabels("type", "script")
// We use Cloud Logging as it's an out of the box available option.
.setLogsPolicy(
LogsPolicy.newBuilder().setDestination(Destination.CLOUD_LOGGING).build())
.build();

CreateJobRequest createJobRequest =
CreateJobRequest.newBuilder()
// The job's parent is the region in which the job will run.
.setParent(String.format("projects/%s/locations/%s", projectId, region))
.setJob(job)
.setJobId(jobName)
.build();

Job result =
batchServiceClient
.createJobCallable()
.futureCall(createJobRequest)
.get(3, TimeUnit.MINUTES);

String resultJobName = result.getName();
System.out.printf("Successfully created the job: %s",
System.out.printf(
"Successfully created the job: %s",
resultJobName.substring(resultJobName.lastIndexOf("\\") + 1));
}
}
}
// [END batch_create_script_job]
// [END batch_create_script_job]
2 changes: 1 addition & 1 deletion samples/snippets/src/main/java/DeleteJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ public static void deleteJob(String projectId, String region, String jobName)
}
}
}
// [END batch_delete_job]
// [END batch_delete_job]
15 changes: 8 additions & 7 deletions samples/snippets/src/main/java/GetJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ public static void getJob(String projectId, String region, String jobName) throw
// clean up any remaining background resources.
try (BatchServiceClient batchServiceClient = BatchServiceClient.create()) {

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

System.out.printf("Retrieved the job: %s ", job.getName());
}
}
}
// [END batch_get_job]
// [END batch_get_job]
2 changes: 1 addition & 1 deletion samples/snippets/src/main/java/ListJobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ public static void listJobs(String projectId, String region) throws IOException
}
}
}
// [END batch_list_jobs]
// [END batch_list_jobs]
Loading