-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
d19bb96
commit 186e893
Showing
88 changed files
with
4,067 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
automl/snippets/src/main/java/com/beta/automl/BatchPredict.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.beta.automl; | ||
|
||
// [START automl_batch_predict_beta] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.automl.v1beta1.BatchPredictInputConfig; | ||
import com.google.cloud.automl.v1beta1.BatchPredictOutputConfig; | ||
import com.google.cloud.automl.v1beta1.BatchPredictRequest; | ||
import com.google.cloud.automl.v1beta1.BatchPredictResult; | ||
import com.google.cloud.automl.v1beta1.GcsDestination; | ||
import com.google.cloud.automl.v1beta1.GcsSource; | ||
import com.google.cloud.automl.v1beta1.ModelName; | ||
import com.google.cloud.automl.v1beta1.OperationMetadata; | ||
import com.google.cloud.automl.v1beta1.PredictionServiceClient; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class BatchPredict { | ||
|
||
static void batchPredict() throws IOException, ExecutionException, InterruptedException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR_PROJECT_ID"; | ||
String modelId = "YOUR_MODEL_ID"; | ||
String inputUri = "gs://YOUR_BUCKET_ID/path_to_your_input_csv_or_jsonl"; | ||
String outputUri = "gs://YOUR_BUCKET_ID/path_to_save_results/"; | ||
batchPredict(projectId, modelId, inputUri, outputUri); | ||
} | ||
|
||
static void batchPredict(String projectId, String modelId, String inputUri, String outputUri) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (PredictionServiceClient client = PredictionServiceClient.create()) { | ||
// Get the full path of the model. | ||
ModelName name = ModelName.of(projectId, "us-central1", modelId); | ||
|
||
// Configure the source of the file from a GCS bucket | ||
GcsSource gcsSource = GcsSource.newBuilder().addInputUris(inputUri).build(); | ||
BatchPredictInputConfig inputConfig = | ||
BatchPredictInputConfig.newBuilder().setGcsSource(gcsSource).build(); | ||
|
||
// Configure where to store the output in a GCS bucket | ||
GcsDestination gcsDestination = | ||
GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build(); | ||
BatchPredictOutputConfig outputConfig = | ||
BatchPredictOutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); | ||
|
||
// Build the request that will be sent to the API | ||
BatchPredictRequest request = | ||
BatchPredictRequest.newBuilder() | ||
.setName(name.toString()) | ||
.setInputConfig(inputConfig) | ||
.setOutputConfig(outputConfig) | ||
.build(); | ||
|
||
// Start an asynchronous request | ||
OperationFuture<BatchPredictResult, OperationMetadata> future = | ||
client.batchPredictAsync(request); | ||
|
||
System.out.println("Waiting for operation to complete..."); | ||
BatchPredictResult response = future.get(); | ||
System.out.println("Batch Prediction results saved to specified Cloud Storage bucket."); | ||
} | ||
} | ||
} | ||
// [END automl_batch_predict_beta] |
48 changes: 48 additions & 0 deletions
48
automl/snippets/src/main/java/com/beta/automl/CancelOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.beta.automl; | ||
|
||
// [START automl_cancel_operation_beta] | ||
|
||
import com.google.cloud.automl.v1beta1.AutoMlClient; | ||
import io.grpc.StatusRuntimeException; | ||
import java.io.IOException; | ||
|
||
class CancelOperation { | ||
|
||
static void cancelOperation() throws IOException, InterruptedException, StatusRuntimeException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR_PROJECT_ID"; | ||
String location = "us-central1"; | ||
String operationId = "YOUR_OPERATION_ID"; | ||
String operationFullId = | ||
String.format("projects/%s/locations/%s/operations/%s", projectId, location, operationId); | ||
cancelOperation(operationFullId); | ||
} | ||
|
||
static void cancelOperation(String operationFullId) | ||
throws IOException, InterruptedException, StatusRuntimeException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (AutoMlClient client = AutoMlClient.create()) { | ||
client.getOperationsClient().cancelOperation(operationFullId); | ||
System.out.println("Operation cancelled"); | ||
} | ||
} | ||
} | ||
// [END automl_cancel_operation_beta] |
49 changes: 49 additions & 0 deletions
49
automl/snippets/src/main/java/com/beta/automl/DeleteDataset.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.beta.automl; | ||
|
||
// [START automl_delete_dataset_beta] | ||
import com.google.cloud.automl.v1beta1.AutoMlClient; | ||
import com.google.cloud.automl.v1beta1.DatasetName; | ||
import com.google.protobuf.Empty; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class DeleteDataset { | ||
|
||
static void deleteDataset() throws IOException, ExecutionException, InterruptedException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR_PROJECT_ID"; | ||
String datasetId = "YOUR_DATASET_ID"; | ||
deleteDataset(projectId, datasetId); | ||
} | ||
|
||
// Delete a dataset | ||
static void deleteDataset(String projectId, String datasetId) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (AutoMlClient client = AutoMlClient.create()) { | ||
// Get the full path of the dataset. | ||
DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId); | ||
Empty response = client.deleteDatasetAsync(datasetFullId).get(); | ||
System.out.format("Dataset deleted. %s%n", response); | ||
} | ||
} | ||
} | ||
// [END automl_delete_dataset_beta] |
54 changes: 54 additions & 0 deletions
54
automl/snippets/src/main/java/com/beta/automl/DeleteModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.beta.automl; | ||
|
||
// [START automl_delete_model_beta] | ||
import com.google.cloud.automl.v1beta1.AutoMlClient; | ||
import com.google.cloud.automl.v1beta1.ModelName; | ||
import com.google.protobuf.Empty; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class DeleteModel { | ||
|
||
public static void main(String[] args) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR_PROJECT_ID"; | ||
String modelId = "YOUR_MODEL_ID"; | ||
deleteModel(projectId, modelId); | ||
} | ||
|
||
// Delete a model | ||
static void deleteModel(String projectId, String modelId) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (AutoMlClient client = AutoMlClient.create()) { | ||
// Get the full path of the model. | ||
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId); | ||
|
||
// Delete a model. | ||
Empty response = client.deleteModelAsync(modelFullId).get(); | ||
|
||
System.out.println("Model deletion started..."); | ||
System.out.println(String.format("Model deleted. %s", response)); | ||
} | ||
} | ||
} | ||
// [END automl_delete_model_beta] |
57 changes: 57 additions & 0 deletions
57
automl/snippets/src/main/java/com/beta/automl/DeployModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.beta.automl; | ||
|
||
// [START automl_deploy_model_beta] | ||
import com.google.api.gax.longrunning.OperationFuture; | ||
import com.google.cloud.automl.v1beta1.AutoMlClient; | ||
import com.google.cloud.automl.v1beta1.DeployModelRequest; | ||
import com.google.cloud.automl.v1beta1.ModelName; | ||
import com.google.cloud.automl.v1beta1.OperationMetadata; | ||
import com.google.protobuf.Empty; | ||
import java.io.IOException; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
class DeployModel { | ||
|
||
public static void main(String[] args) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR_PROJECT_ID"; | ||
String modelId = "YOUR_MODEL_ID"; | ||
deployModel(projectId, modelId); | ||
} | ||
|
||
// Deploy a model for prediction | ||
static void deployModel(String projectId, String modelId) | ||
throws IOException, ExecutionException, InterruptedException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (AutoMlClient client = AutoMlClient.create()) { | ||
// Get the full path of the model. | ||
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId); | ||
DeployModelRequest request = | ||
DeployModelRequest.newBuilder().setName(modelFullId.toString()).build(); | ||
OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request); | ||
|
||
future.get(); | ||
System.out.println("Model deployment finished"); | ||
} | ||
} | ||
} | ||
// [END automl_deploy_model_beta] |
62 changes: 62 additions & 0 deletions
62
automl/snippets/src/main/java/com/beta/automl/GetModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2020 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.beta.automl; | ||
|
||
// [START automl_get_model_beta] | ||
import com.google.cloud.automl.v1beta1.AutoMlClient; | ||
import com.google.cloud.automl.v1beta1.Model; | ||
import com.google.cloud.automl.v1beta1.ModelName; | ||
import io.grpc.StatusRuntimeException; | ||
import java.io.IOException; | ||
|
||
class GetModel { | ||
|
||
static void getModel() throws IOException, StatusRuntimeException { | ||
// TODO(developer): Replace these variables before running the sample. | ||
String projectId = "YOUR_PROJECT_ID"; | ||
String modelId = "YOUR_MODEL_ID"; | ||
getModel(projectId, modelId); | ||
} | ||
|
||
// Get a model | ||
static void getModel(String projectId, String modelId) | ||
throws IOException, StatusRuntimeException { | ||
// Initialize client that will be used to send requests. This client only needs to be created | ||
// once, and can be reused for multiple requests. After completing all of your requests, call | ||
// the "close" method on the client to safely clean up any remaining background resources. | ||
try (AutoMlClient client = AutoMlClient.create()) { | ||
// Get the full path of the model. | ||
ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId); | ||
Model model = client.getModel(modelFullId); | ||
|
||
// Display the model information. | ||
System.out.format("Model name: %s%n", model.getName()); | ||
// To get the model id, you have to parse it out of the `name` field. As models Ids are | ||
// required for other methods. | ||
// Name Format: `projects/{project_id}/locations/{location_id}/models/{model_id}` | ||
String[] names = model.getName().split("/"); | ||
String retrievedModelId = names[names.length - 1]; | ||
System.out.format("Model id: %s%n", retrievedModelId); | ||
System.out.format("Model display name: %s%n", model.getDisplayName()); | ||
System.out.println("Model create time:"); | ||
System.out.format("\tseconds: %s%n", model.getCreateTime().getSeconds()); | ||
System.out.format("\tnanos: %s%n", model.getCreateTime().getNanos()); | ||
System.out.format("Model deployment state: %s%n", model.getDeploymentState()); | ||
} | ||
} | ||
} | ||
// [END automl_get_model_beta] |
Oops, something went wrong.