Skip to content

Commit 7f079f7

Browse files
authored
samples: initial commit and it is missing env vars for testing (#7)
* samples: initial commit and it is missing env vars for testing * sampels: fixed dependency issues and lint errors * added missing dependencies for other samples pom.xml * updated the copyright links * fixed wrong version on imports * fixed compilar error * removed env vars * fiexd the lint issuwe * lowered uppercase of local var name * corrected the test resources * fixed the IT tests * refactored the deploymodel test * added missing region tag * fixed env vars issue * fixed table issues * fixed delete tests
1 parent 18bd403 commit 7f079f7

23 files changed

+1745
-2
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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_dataset_sample]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.aiplatform.v1beta1.CreateDatasetOperationMetadata;
23+
import com.google.cloud.aiplatform.v1beta1.Dataset;
24+
import com.google.cloud.aiplatform.v1beta1.DatasetServiceClient;
25+
import com.google.cloud.aiplatform.v1beta1.DatasetServiceSettings;
26+
import com.google.cloud.aiplatform.v1beta1.LocationName;
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.TimeUnit;
30+
import java.util.concurrent.TimeoutException;
31+
32+
public class CreateDatasetSample {
33+
34+
public static void main(String[] args)
35+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String project = "YOUR_PROJECT_ID";
38+
String datasetDisplayName = "YOUR_DATASET_DISPLAY_NAME";
39+
String metadataSchemaUri = "YOUR_METADATA_SCHEMA_URI";
40+
createDatasetSample(project, datasetDisplayName, metadataSchemaUri);
41+
}
42+
43+
static void createDatasetSample(
44+
String project, String datasetDisplayName, String metadataSchemaUri)
45+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
46+
DatasetServiceSettings datasetServiceSettings =
47+
DatasetServiceSettings.newBuilder()
48+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
49+
.build();
50+
51+
// Initialize client that will be used to send requests. This client only needs to be created
52+
// once, and can be reused for multiple requests. After completing all of your requests, call
53+
// the "close" method on the client to safely clean up any remaining background resources.
54+
try (DatasetServiceClient datasetServiceClient =
55+
DatasetServiceClient.create(datasetServiceSettings)) {
56+
String location = "us-central1";
57+
LocationName locationName = LocationName.of(project, location);
58+
Dataset dataset =
59+
Dataset.newBuilder()
60+
.setDisplayName(datasetDisplayName)
61+
.setMetadataSchemaUri(metadataSchemaUri)
62+
.build();
63+
64+
OperationFuture<Dataset, CreateDatasetOperationMetadata> datasetFuture =
65+
datasetServiceClient.createDatasetAsync(locationName, dataset);
66+
System.out.format("Operation name: %s\n", datasetFuture.getInitialFuture().get().getName());
67+
System.out.println("Waiting for operation to finish...");
68+
Dataset datasetResponse = datasetFuture.get(300, TimeUnit.SECONDS);
69+
70+
System.out.println("Create Dataset Response");
71+
System.out.format("Name: %s\n", datasetResponse.getName());
72+
System.out.format("Display Name: %s\n", datasetResponse.getDisplayName());
73+
System.out.format("Metadata Schema Uri: %s\n", datasetResponse.getMetadataSchemaUri());
74+
System.out.format("Metadata: %s\n", datasetResponse.getMetadata());
75+
System.out.format("Create Time: %s\n", datasetResponse.getCreateTime());
76+
System.out.format("Update Time: %s\n", datasetResponse.getUpdateTime());
77+
System.out.format("Labels: %s\n", datasetResponse.getLabelsMap());
78+
}
79+
}
80+
}
81+
// [END aiplatform_create_dataset_sample]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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_endpoint_sample]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.aiplatform.v1beta1.CreateEndpointOperationMetadata;
23+
import com.google.cloud.aiplatform.v1beta1.Endpoint;
24+
import com.google.cloud.aiplatform.v1beta1.EndpointServiceClient;
25+
import com.google.cloud.aiplatform.v1beta1.EndpointServiceSettings;
26+
import com.google.cloud.aiplatform.v1beta1.LocationName;
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.TimeUnit;
30+
import java.util.concurrent.TimeoutException;
31+
32+
public class CreateEndpointSample {
33+
34+
public static void main(String[] args)
35+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String project = "YOUR_PROJECT_ID";
38+
String endpointDisplayName = "YOUR_ENDPOINT_DISPLAY_NAME";
39+
createEndpointSample(project, endpointDisplayName);
40+
}
41+
42+
static void createEndpointSample(String project, String endpointDisplayName)
43+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
44+
EndpointServiceSettings endpointServiceSettings =
45+
EndpointServiceSettings.newBuilder()
46+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
47+
.build();
48+
49+
// Initialize client that will be used to send requests. This client only needs to be created
50+
// once, and can be reused for multiple requests. After completing all of your requests, call
51+
// the "close" method on the client to safely clean up any remaining background resources.
52+
try (EndpointServiceClient endpointServiceClient =
53+
EndpointServiceClient.create(endpointServiceSettings)) {
54+
String location = "us-central1";
55+
LocationName locationName = LocationName.of(project, location);
56+
Endpoint endpoint = Endpoint.newBuilder().setDisplayName(endpointDisplayName).build();
57+
58+
OperationFuture<Endpoint, CreateEndpointOperationMetadata> endpointFuture =
59+
endpointServiceClient.createEndpointAsync(locationName, endpoint);
60+
System.out.format("Operation name: %s\n", endpointFuture.getInitialFuture().get().getName());
61+
System.out.println("Waiting for operation to finish...");
62+
Endpoint endpointResponse = endpointFuture.get(300, TimeUnit.SECONDS);
63+
64+
System.out.println("Create Endpoint Response");
65+
System.out.format("Name: %s\n", endpointResponse.getName());
66+
System.out.format("Display Name: %s\n", endpointResponse.getDisplayName());
67+
System.out.format("Description: %s\n", endpointResponse.getDescription());
68+
System.out.format("Labels: %s\n", endpointResponse.getLabelsMap());
69+
System.out.format("Create Time: %s\n", endpointResponse.getCreateTime());
70+
System.out.format("Update Time: %s\n", endpointResponse.getUpdateTime());
71+
}
72+
}
73+
}
74+
// [END aiplatform_create_endpoint_sample]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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_delete_endpoint_sample]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.aiplatform.v1beta1.DeleteOperationMetadata;
23+
import com.google.cloud.aiplatform.v1beta1.EndpointName;
24+
import com.google.cloud.aiplatform.v1beta1.EndpointServiceClient;
25+
import com.google.cloud.aiplatform.v1beta1.EndpointServiceSettings;
26+
import com.google.protobuf.Empty;
27+
import java.io.IOException;
28+
import java.util.concurrent.ExecutionException;
29+
import java.util.concurrent.TimeUnit;
30+
import java.util.concurrent.TimeoutException;
31+
32+
public class DeleteEndpointSample {
33+
34+
public static void main(String[] args)
35+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String project = "YOUR_PROJECT_ID";
38+
String endpointId = "YOUR_ENDPOINT_ID";
39+
deleteEndpointSample(project, endpointId);
40+
}
41+
42+
static void deleteEndpointSample(String project, String endpointId)
43+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
44+
EndpointServiceSettings endpointServiceSettings =
45+
EndpointServiceSettings.newBuilder()
46+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
47+
.build();
48+
49+
// Initialize client that will be used to send requests. This client only needs to be created
50+
// once, and can be reused for multiple requests. After completing all of your requests, call
51+
// the "close" method on the client to safely clean up any remaining background resources.
52+
try (EndpointServiceClient endpointServiceClient =
53+
EndpointServiceClient.create(endpointServiceSettings)) {
54+
String location = "us-central1";
55+
EndpointName endpointName = EndpointName.of(project, location, endpointId);
56+
57+
OperationFuture<Empty, DeleteOperationMetadata> operationFuture =
58+
endpointServiceClient.deleteEndpointAsync(endpointName);
59+
System.out.format("Operation name: %s\n", operationFuture.getInitialFuture().get().getName());
60+
System.out.println("Waiting for operation to finish...");
61+
Empty deleteResponse = operationFuture.get(300, TimeUnit.SECONDS);
62+
63+
System.out.format("Delete Endpoint Response: %s\n", deleteResponse);
64+
}
65+
}
66+
}
67+
// [END aiplatform_delete_endpoint_sample]
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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_deploy_model_sample]
20+
21+
import com.google.api.gax.longrunning.OperationFuture;
22+
import com.google.cloud.aiplatform.v1beta1.AutomaticResources;
23+
import com.google.cloud.aiplatform.v1beta1.DedicatedResources;
24+
import com.google.cloud.aiplatform.v1beta1.DeployModelOperationMetadata;
25+
import com.google.cloud.aiplatform.v1beta1.DeployModelResponse;
26+
import com.google.cloud.aiplatform.v1beta1.DeployedModel;
27+
import com.google.cloud.aiplatform.v1beta1.EndpointName;
28+
import com.google.cloud.aiplatform.v1beta1.EndpointServiceClient;
29+
import com.google.cloud.aiplatform.v1beta1.EndpointServiceSettings;
30+
import com.google.cloud.aiplatform.v1beta1.MachineSpec;
31+
import com.google.cloud.aiplatform.v1beta1.ModelName;
32+
import java.io.IOException;
33+
import java.util.HashMap;
34+
import java.util.Map;
35+
import java.util.concurrent.ExecutionException;
36+
import java.util.concurrent.TimeUnit;
37+
import java.util.concurrent.TimeoutException;
38+
39+
public class DeployModelSample {
40+
41+
public static void main(String[] args)
42+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
43+
// TODO(developer): Replace these variables before running the sample.
44+
String project = "YOUR_PROJECT_ID";
45+
String deployedModelDisplayName = "YOUR_DEPLOYED_MODEL_DISPLAY_NAME";
46+
String endpointId = "YOUR_ENDPOINT_NAME";
47+
String modelId = "YOUR_MODEL_ID";
48+
deployModelSample(project, deployedModelDisplayName, endpointId, modelId);
49+
}
50+
51+
static void deployModelSample(
52+
String project, String deployedModelDisplayName, String endpointId, String modelId)
53+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
54+
EndpointServiceSettings endpointServiceSettings =
55+
EndpointServiceSettings.newBuilder()
56+
.setEndpoint("us-central1-aiplatform.googleapis.com:443")
57+
.build();
58+
59+
// Initialize client that will be used to send requests. This client only needs to be created
60+
// once, and can be reused for multiple requests. After completing all of your requests, call
61+
// the "close" method on the client to safely clean up any remaining background resources.
62+
try (EndpointServiceClient endpointServiceClient =
63+
EndpointServiceClient.create(endpointServiceSettings)) {
64+
String location = "us-central1";
65+
EndpointName endpointName = EndpointName.of(project, location, endpointId);
66+
// key '0' assigns traffic for the newly deployed model
67+
// Traffic percentage values must add up to 100
68+
// Leave dictionary empty if endpoint should not accept any traffic
69+
Map<String, Integer> trafficSplit = new HashMap<>();
70+
trafficSplit.put("0", 100);
71+
ModelName modelName = ModelName.of(project, location, modelId);
72+
AutomaticResources automaticResourcesInput =
73+
AutomaticResources.newBuilder().setMinReplicaCount(1).setMaxReplicaCount(1).build();
74+
DeployedModel deployedModelInput =
75+
DeployedModel.newBuilder()
76+
.setModel(modelName.toString())
77+
.setDisplayName(deployedModelDisplayName)
78+
.setAutomaticResources(automaticResourcesInput)
79+
.build();
80+
81+
OperationFuture<DeployModelResponse, DeployModelOperationMetadata> deployModelResponseFuture =
82+
endpointServiceClient.deployModelAsync(endpointName, deployedModelInput, trafficSplit);
83+
System.out.format(
84+
"Operation name: %s\n", deployModelResponseFuture.getInitialFuture().get().getName());
85+
System.out.println("Waiting for operation to finish...");
86+
DeployModelResponse deployModelResponse = deployModelResponseFuture.get(20, TimeUnit.MINUTES);
87+
88+
System.out.println("Deploy Model Response");
89+
DeployedModel deployedModel = deployModelResponse.getDeployedModel();
90+
System.out.println("\tDeployed Model");
91+
System.out.format("\t\tid: %s\n", deployedModel.getId());
92+
System.out.format("\t\tmodel: %s\n", deployedModel.getModel());
93+
System.out.format("\t\tDisplay Name: %s\n", deployedModel.getDisplayName());
94+
System.out.format("\t\tCreate Time: %s\n", deployedModel.getCreateTime());
95+
96+
DedicatedResources dedicatedResources = deployedModel.getDedicatedResources();
97+
System.out.println("\t\tDedicated Resources");
98+
System.out.format("\t\t\tMin Replica Count: %s\n", dedicatedResources.getMinReplicaCount());
99+
100+
MachineSpec machineSpec = dedicatedResources.getMachineSpec();
101+
System.out.println("\t\t\tMachine Spec");
102+
System.out.format("\t\t\t\tMachine Type: %s\n", machineSpec.getMachineType());
103+
System.out.format("\t\t\t\tAccelerator Type: %s\n", machineSpec.getAcceleratorType());
104+
System.out.format("\t\t\t\tAccelerator Count: %s\n", machineSpec.getAcceleratorCount());
105+
106+
AutomaticResources automaticResources = deployedModel.getAutomaticResources();
107+
System.out.println("\t\tAutomatic Resources");
108+
System.out.format("\t\t\tMin Replica Count: %s\n", automaticResources.getMinReplicaCount());
109+
System.out.format("\t\t\tMax Replica Count: %s\n", automaticResources.getMaxReplicaCount());
110+
}
111+
}
112+
}
113+
// [END aiplatform_deploy_model_sample]

0 commit comments

Comments
 (0)