|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package aiplatform; |
| 18 | + |
| 19 | +// [START aiplatform_create_batch_prediction_job_sample] |
| 20 | +import com.google.cloud.aiplatform.v1beta1.AcceleratorType; |
| 21 | +import com.google.cloud.aiplatform.v1beta1.BatchDedicatedResources; |
| 22 | +import com.google.cloud.aiplatform.v1beta1.BatchPredictionJob; |
| 23 | +import com.google.cloud.aiplatform.v1beta1.GcsDestination; |
| 24 | +import com.google.cloud.aiplatform.v1beta1.GcsSource; |
| 25 | +import com.google.cloud.aiplatform.v1beta1.JobServiceClient; |
| 26 | +import com.google.cloud.aiplatform.v1beta1.JobServiceSettings; |
| 27 | +import com.google.cloud.aiplatform.v1beta1.LocationName; |
| 28 | +import com.google.cloud.aiplatform.v1beta1.MachineSpec; |
| 29 | +import com.google.cloud.aiplatform.v1beta1.ModelName; |
| 30 | +import com.google.gson.JsonObject; |
| 31 | +import com.google.protobuf.Value; |
| 32 | +import com.google.protobuf.util.JsonFormat; |
| 33 | +import java.io.IOException; |
| 34 | + |
| 35 | +public class CreateBatchPredictionJobSample { |
| 36 | + |
| 37 | + public static void main(String[] args) throws IOException { |
| 38 | + // TODO(developer): Replace these variables before running the sample. |
| 39 | + String project = "PROJECT"; |
| 40 | + String displayName = "DISPLAY_NAME"; |
| 41 | + String modelName = "MODEL_NAME"; |
| 42 | + String instancesFormat = "INSTANCES_FORMAT"; |
| 43 | + String gcsSourceUri = "GCS_SOURCE_URI"; |
| 44 | + String predictionsFormat = "PREDICTIONS_FORMAT"; |
| 45 | + String gcsDestinationOutputUriPrefix = "GCS_DESTINATION_OUTPUT_URI_PREFIX"; |
| 46 | + createBatchPredictionJobSample( |
| 47 | + project, |
| 48 | + displayName, |
| 49 | + modelName, |
| 50 | + instancesFormat, |
| 51 | + gcsSourceUri, |
| 52 | + predictionsFormat, |
| 53 | + gcsDestinationOutputUriPrefix); |
| 54 | + } |
| 55 | + |
| 56 | + static void createBatchPredictionJobSample( |
| 57 | + String project, |
| 58 | + String displayName, |
| 59 | + String model, |
| 60 | + String instancesFormat, |
| 61 | + String gcsSourceUri, |
| 62 | + String predictionsFormat, |
| 63 | + String gcsDestinationOutputUriPrefix) |
| 64 | + throws IOException { |
| 65 | + JobServiceSettings settings = |
| 66 | + JobServiceSettings.newBuilder() |
| 67 | + .setEndpoint("us-central1-aiplatform.googleapis.com:443") |
| 68 | + .build(); |
| 69 | + String location = "us-central1"; |
| 70 | + |
| 71 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 72 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 73 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 74 | + try (JobServiceClient client = JobServiceClient.create(settings)) { |
| 75 | + |
| 76 | + // Passing in an empty Value object for model parameters |
| 77 | + JsonObject jsonModelParameters = new JsonObject(); |
| 78 | + Value.Builder modelParametersBuilder = Value.newBuilder(); |
| 79 | + JsonFormat.parser().merge(jsonModelParameters.toString(), modelParametersBuilder); |
| 80 | + Value modelParameters = modelParametersBuilder.build(); |
| 81 | + |
| 82 | + GcsSource gcsSource = GcsSource.newBuilder().addUris(gcsSourceUri).build(); |
| 83 | + BatchPredictionJob.InputConfig inputConfig = |
| 84 | + BatchPredictionJob.InputConfig.newBuilder() |
| 85 | + .setInstancesFormat(instancesFormat) |
| 86 | + .setGcsSource(gcsSource) |
| 87 | + .build(); |
| 88 | + GcsDestination gcsDestination = |
| 89 | + GcsDestination.newBuilder().setOutputUriPrefix(gcsDestinationOutputUriPrefix).build(); |
| 90 | + BatchPredictionJob.OutputConfig outputConfig = |
| 91 | + BatchPredictionJob.OutputConfig.newBuilder() |
| 92 | + .setPredictionsFormat(predictionsFormat) |
| 93 | + .setGcsDestination(gcsDestination) |
| 94 | + .build(); |
| 95 | + MachineSpec machineSpec = |
| 96 | + MachineSpec.newBuilder() |
| 97 | + .setMachineType("n1-standard-2") |
| 98 | + .setAcceleratorType(AcceleratorType.NVIDIA_TESLA_K80) |
| 99 | + .setAcceleratorCount(1) |
| 100 | + .build(); |
| 101 | + BatchDedicatedResources dedicatedResources = |
| 102 | + BatchDedicatedResources.newBuilder() |
| 103 | + .setMachineSpec(machineSpec) |
| 104 | + .setStartingReplicaCount(1) |
| 105 | + .setMaxReplicaCount(1) |
| 106 | + .build(); |
| 107 | + String modelName = ModelName.of(project, location, model).toString(); |
| 108 | + BatchPredictionJob batchPredictionJob = |
| 109 | + BatchPredictionJob.newBuilder() |
| 110 | + .setDisplayName(displayName) |
| 111 | + .setModel(modelName) |
| 112 | + .setModelParameters(modelParameters) |
| 113 | + .setInputConfig(inputConfig) |
| 114 | + .setOutputConfig(outputConfig) |
| 115 | + .setDedicatedResources(dedicatedResources) |
| 116 | + .build(); |
| 117 | + LocationName parent = LocationName.of(project, location); |
| 118 | + BatchPredictionJob response = client.createBatchPredictionJob(parent, batchPredictionJob); |
| 119 | + System.out.format("response: %s\n", response); |
| 120 | + System.out.format("\tName: %s\n", response.getName()); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// [END aiplatform_create_batch_prediction_job_sample] |
0 commit comments