Skip to content

Commit 186e893

Browse files
munkhuushmglShabirmean
authored andcommitted
samples: migrate samples from GoogleCloudPlatform/java-docs-samples /beta/automl (#290)
* samples: added missing beta samples * made requested changes * formatted the code * fixed all lint issues * refactored all create model tests and made requested changes * hardcoded table model id
1 parent d19bb96 commit 186e893

File tree

88 files changed

+4067
-211
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+4067
-211
lines changed

automl/snippets/pom.xml

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
<groupId>com.google.cloud</groupId>
4343
<artifactId>google-cloud-automl</artifactId>
4444
</dependency>
45+
<dependency>
46+
<groupId>com.google.cloud</groupId>
47+
<artifactId>google-cloud-bigquery</artifactId>
48+
</dependency>
49+
<dependency>
50+
<groupId>com.google.cloud</groupId>
51+
<artifactId>google-cloud-storage</artifactId>
52+
</dependency>
4553
<!-- [START_EXCLUDE] -->
4654
<dependency>
4755
<groupId>net.sourceforge.argparse4j</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 com.beta.automl;
18+
19+
// [START automl_batch_predict_beta]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.automl.v1beta1.BatchPredictInputConfig;
22+
import com.google.cloud.automl.v1beta1.BatchPredictOutputConfig;
23+
import com.google.cloud.automl.v1beta1.BatchPredictRequest;
24+
import com.google.cloud.automl.v1beta1.BatchPredictResult;
25+
import com.google.cloud.automl.v1beta1.GcsDestination;
26+
import com.google.cloud.automl.v1beta1.GcsSource;
27+
import com.google.cloud.automl.v1beta1.ModelName;
28+
import com.google.cloud.automl.v1beta1.OperationMetadata;
29+
import com.google.cloud.automl.v1beta1.PredictionServiceClient;
30+
import java.io.IOException;
31+
import java.util.concurrent.ExecutionException;
32+
33+
class BatchPredict {
34+
35+
static void batchPredict() throws IOException, ExecutionException, InterruptedException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String projectId = "YOUR_PROJECT_ID";
38+
String modelId = "YOUR_MODEL_ID";
39+
String inputUri = "gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl";
40+
String outputUri = "gs://YOUR_BUCKET_ID/path_to_save_results/";
41+
batchPredict(projectId, modelId, inputUri, outputUri);
42+
}
43+
44+
static void batchPredict(String projectId, String modelId, String inputUri, String outputUri)
45+
throws IOException, ExecutionException, InterruptedException {
46+
// Initialize client that will be used to send requests. This client only needs to be created
47+
// once, and can be reused for multiple requests. After completing all of your requests, call
48+
// the "close" method on the client to safely clean up any remaining background resources.
49+
try (PredictionServiceClient client = PredictionServiceClient.create()) {
50+
// Get the full path of the model.
51+
ModelName name = ModelName.of(projectId, "us-central1", modelId);
52+
53+
// Configure the source of the file from a GCS bucket
54+
GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build();
55+
BatchPredictInputConfig inputConfig =
56+
BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build();
57+
58+
// Configure where to store the output in a GCS bucket
59+
GcsDestination gcsDestination =
60+
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build();
61+
BatchPredictOutputConfig outputConfig =
62+
BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
63+
64+
// Build the request that will be sent to the API
65+
BatchPredictRequest request =
66+
BatchPredictRequest.newBuilder()
67+
.setName(name.toString())
68+
.setInputConfig(inputConfig)
69+
.setOutputConfig(outputConfig)
70+
.build();
71+
72+
// Start an asynchronous request
73+
OperationFuture<BatchPredictResult, OperationMetadata> future =
74+
client.batchPredictAsync(request);
75+
76+
System.out.println("Waiting for operation to complete...");
77+
BatchPredictResult response = future.get();
78+
System.out.println("Batch Prediction results saved to specified Cloud Storage bucket.");
79+
}
80+
}
81+
}
82+
// [END automl_batch_predict_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 com.beta.automl;
18+
19+
// [START automl_cancel_operation_beta]
20+
21+
import com.google.cloud.automl.v1beta1.AutoMlClient;
22+
import io.grpc.StatusRuntimeException;
23+
import java.io.IOException;
24+
25+
class CancelOperation {
26+
27+
static void cancelOperation() throws IOException, InterruptedException, StatusRuntimeException {
28+
// TODO(developer): Replace these variables before running the sample.
29+
String projectId = "YOUR_PROJECT_ID";
30+
String location = "us-central1";
31+
String operationId = "YOUR_OPERATION_ID";
32+
String operationFullId =
33+
String.format("projects/%s/locations/%s/operations/%s", projectId, location, operationId);
34+
cancelOperation(operationFullId);
35+
}
36+
37+
static void cancelOperation(String operationFullId)
38+
throws IOException, InterruptedException, StatusRuntimeException {
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests. After completing all of your requests, call
41+
// the "close" method on the client to safely clean up any remaining background resources.
42+
try (AutoMlClient client = AutoMlClient.create()) {
43+
client.getOperationsClient().cancelOperation(operationFullId);
44+
System.out.println("Operation cancelled");
45+
}
46+
}
47+
}
48+
// [END automl_cancel_operation_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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 com.beta.automl;
18+
19+
// [START automl_delete_dataset_beta]
20+
import com.google.cloud.automl.v1beta1.AutoMlClient;
21+
import com.google.cloud.automl.v1beta1.DatasetName;
22+
import com.google.protobuf.Empty;
23+
import java.io.IOException;
24+
import java.util.concurrent.ExecutionException;
25+
26+
class DeleteDataset {
27+
28+
static void deleteDataset() throws IOException, ExecutionException, InterruptedException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "YOUR_PROJECT_ID";
31+
String datasetId = "YOUR_DATASET_ID";
32+
deleteDataset(projectId, datasetId);
33+
}
34+
35+
// Delete a dataset
36+
static void deleteDataset(String projectId, String datasetId)
37+
throws IOException, ExecutionException, InterruptedException {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests. After completing all of your requests, call
40+
// the "close" method on the client to safely clean up any remaining background resources.
41+
try (AutoMlClient client = AutoMlClient.create()) {
42+
// Get the full path of the dataset.
43+
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId);
44+
Empty response = client.deleteDatasetAsync(datasetFullId).get();
45+
System.out.format("Dataset deleted. %s%n", response);
46+
}
47+
}
48+
}
49+
// [END automl_delete_dataset_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 com.beta.automl;
18+
19+
// [START automl_delete_model_beta]
20+
import com.google.cloud.automl.v1beta1.AutoMlClient;
21+
import com.google.cloud.automl.v1beta1.ModelName;
22+
import com.google.protobuf.Empty;
23+
import java.io.IOException;
24+
import java.util.concurrent.ExecutionException;
25+
26+
class DeleteModel {
27+
28+
public static void main(String[] args)
29+
throws IOException, ExecutionException, InterruptedException {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "YOUR_PROJECT_ID";
32+
String modelId = "YOUR_MODEL_ID";
33+
deleteModel(projectId, modelId);
34+
}
35+
36+
// Delete a model
37+
static void deleteModel(String projectId, String modelId)
38+
throws IOException, ExecutionException, InterruptedException {
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests. After completing all of your requests, call
41+
// the "close" method on the client to safely clean up any remaining background resources.
42+
try (AutoMlClient client = AutoMlClient.create()) {
43+
// Get the full path of the model.
44+
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
45+
46+
// Delete a model.
47+
Empty response = client.deleteModelAsync(modelFullId).get();
48+
49+
System.out.println("Model deletion started...");
50+
System.out.println(String.format("Model deleted. %s", response));
51+
}
52+
}
53+
}
54+
// [END automl_delete_model_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 com.beta.automl;
18+
19+
// [START automl_deploy_model_beta]
20+
import com.google.api.gax.longrunning.OperationFuture;
21+
import com.google.cloud.automl.v1beta1.AutoMlClient;
22+
import com.google.cloud.automl.v1beta1.DeployModelRequest;
23+
import com.google.cloud.automl.v1beta1.ModelName;
24+
import com.google.cloud.automl.v1beta1.OperationMetadata;
25+
import com.google.protobuf.Empty;
26+
import java.io.IOException;
27+
import java.util.concurrent.ExecutionException;
28+
29+
class DeployModel {
30+
31+
public static void main(String[] args)
32+
throws IOException, ExecutionException, InterruptedException {
33+
// TODO(developer): Replace these variables before running the sample.
34+
String projectId = "YOUR_PROJECT_ID";
35+
String modelId = "YOUR_MODEL_ID";
36+
deployModel(projectId, modelId);
37+
}
38+
39+
// Deploy a model for prediction
40+
static void deployModel(String projectId, String modelId)
41+
throws IOException, ExecutionException, InterruptedException {
42+
// Initialize client that will be used to send requests. This client only needs to be created
43+
// once, and can be reused for multiple requests. After completing all of your requests, call
44+
// the "close" method on the client to safely clean up any remaining background resources.
45+
try (AutoMlClient client = AutoMlClient.create()) {
46+
// Get the full path of the model.
47+
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
48+
DeployModelRequest request =
49+
DeployModelRequest.newBuilder().setName(modelFullId.toString()).build();
50+
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request);
51+
52+
future.get();
53+
System.out.println("Model deployment finished");
54+
}
55+
}
56+
}
57+
// [END automl_deploy_model_beta]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 com.beta.automl;
18+
19+
// [START automl_get_model_beta]
20+
import com.google.cloud.automl.v1beta1.AutoMlClient;
21+
import com.google.cloud.automl.v1beta1.Model;
22+
import com.google.cloud.automl.v1beta1.ModelName;
23+
import io.grpc.StatusRuntimeException;
24+
import java.io.IOException;
25+
26+
class GetModel {
27+
28+
static void getModel() throws IOException, StatusRuntimeException {
29+
// TODO(developer): Replace these variables before running the sample.
30+
String projectId = "YOUR_PROJECT_ID";
31+
String modelId = "YOUR_MODEL_ID";
32+
getModel(projectId, modelId);
33+
}
34+
35+
// Get a model
36+
static void getModel(String projectId, String modelId)
37+
throws IOException, StatusRuntimeException {
38+
// Initialize client that will be used to send requests. This client only needs to be created
39+
// once, and can be reused for multiple requests. After completing all of your requests, call
40+
// the "close" method on the client to safely clean up any remaining background resources.
41+
try (AutoMlClient client = AutoMlClient.create()) {
42+
// Get the full path of the model.
43+
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId);
44+
Model model = client.getModel(modelFullId);
45+
46+
// Display the model information.
47+
System.out.format("Model name: %s%n", model.getName());
48+
// To get the model id, you have to parse it out of the `name` field. As models Ids are
49+
// required for other methods.
50+
// Name Format: `projects/{project_id}/locations/{location_id}/models/{model_id}`
51+
String[] names = model.getName().split("/");
52+
String retrievedModelId = names[names.length - 1];
53+
System.out.format("Model id: %s%n", retrievedModelId);
54+
System.out.format("Model display name: %s%n", model.getDisplayName());
55+
System.out.println("Model create time:");
56+
System.out.format("\tseconds: %s%n", model.getCreateTime().getSeconds());
57+
System.out.format("\tnanos: %s%n", model.getCreateTime().getNanos());
58+
System.out.format("Model deployment state: %s%n", model.getDeploymentState());
59+
}
60+
}
61+
}
62+
// [END automl_get_model_beta]

0 commit comments

Comments
 (0)