From ba26282a0658c073e70b7dfe92a2c9f9d956173b Mon Sep 17 00:00:00 2001 From: nnegrey Date: Mon, 30 Sep 2019 09:59:02 -0600 Subject: [PATCH 1/4] Update auto ml tests --- .../cloud/translate/automl/DatasetApi.java | 248 +++++++++--------- .../cloud/translate/automl/ModelApi.java | 209 ++++++++------- .../cloud/translate/automl/PredictionApi.java | 28 +- .../cloud/translate/automl/DatasetApiIT.java | 4 +- .../translate/automl/PredictionApiIT.java | 2 +- 5 files changed, 253 insertions(+), 238 deletions(-) diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java index af569360b8f..c5f2a9591bb 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.io.PrintStream; +import java.util.concurrent.TimeUnit; import net.sourceforge.argparse4j.ArgumentParsers; import net.sourceforge.argparse4j.inf.ArgumentParser; @@ -60,47 +61,48 @@ public static void createDataset( String projectId, String computeRegion, String datasetName, String source, String target) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); - - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - - // Specify the source and target language. - TranslationDatasetMetadata translationDatasetMetadata = - TranslationDatasetMetadata.newBuilder() - .setSourceLanguageCode(source) - .setTargetLanguageCode(target) - .build(); - - // Set dataset name and dataset metadata. - Dataset myDataset = - Dataset.newBuilder() - .setDisplayName(datasetName) - .setTranslationDatasetMetadata(translationDatasetMetadata) - .build(); - - // Create a dataset with the dataset metadata in the region. - Dataset dataset = client.createDataset(projectLocation, myDataset); - - // Display the dataset information. - System.out.println(String.format("Dataset name: %s", dataset.getName())); - System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); - System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); - System.out.println("Translation dataset Metadata:"); - System.out.println( - String.format( - "\tSource language code: %s", - dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); - System.out.println( - String.format( - "\tTarget language code: %s", - dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); - System.out.println("Dataset create time:"); - System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); + try (AutoMlClient client = AutoMlClient.create()) { + + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, computeRegion); + + // Specify the source and target language. + TranslationDatasetMetadata translationDatasetMetadata = + TranslationDatasetMetadata.newBuilder() + .setSourceLanguageCode(source) + .setTargetLanguageCode(target) + .build(); + + // Set dataset name and dataset metadata. + Dataset myDataset = + Dataset.newBuilder() + .setDisplayName(datasetName) + .setTranslationDatasetMetadata(translationDatasetMetadata) + .build(); + + // Create a dataset with the dataset metadata in the region. + Dataset dataset = client.createDataset(projectLocation, myDataset); + + // Display the dataset information. + System.out.println(String.format("Dataset name: %s", dataset.getName())); + System.out.println( + String.format( + "Dataset id: %s", + dataset.getName().split("/")[dataset.getName().split("/").length - 1])); + System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); + System.out.println("Translation dataset Metadata:"); + System.out.println( + String.format( + "\tSource language code: %s", + dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); + System.out.println( + String.format( + "\tTarget language code: %s", + dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); + System.out.println("Dataset create time:"); + System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); + System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); + } } // [END automl_translate_create_dataset] @@ -116,39 +118,40 @@ public static void createDataset( public static void listDatasets(String projectId, String computeRegion, String filter) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); - - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - - ListDatasetsRequest request = - ListDatasetsRequest.newBuilder() - .setParent(projectLocation.toString()) - .setFilter(filter) - .build(); - - // List all the datasets available in the region by applying filter. - System.out.println("List of datasets:"); - for (Dataset dataset : client.listDatasets(request).iterateAll()) { - // Display the dataset information - System.out.println(String.format("\nDataset name: %s", dataset.getName())); - System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); - System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); - System.out.println("Translation dataset metadata:"); - System.out.println( - String.format( - "\tSource language code: %s", - dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); - System.out.println( - String.format( - "\tTarget language code: %s", - dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); - System.out.println("Dataset create time:"); - System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); + try (AutoMlClient client = AutoMlClient.create()) { + + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, computeRegion); + + ListDatasetsRequest request = + ListDatasetsRequest.newBuilder() + .setParent(projectLocation.toString()) + .setFilter(filter) + .build(); + + // List all the datasets available in the region by applying filter. + System.out.println("List of datasets:"); + for (Dataset dataset : client.listDatasets(request).iterateAll()) { + // Display the dataset information + System.out.println(String.format("\nDataset name: %s", dataset.getName())); + System.out.println( + String.format( + "Dataset id: %s", + dataset.getName().split("/")[dataset.getName().split("/").length - 1])); + System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); + System.out.println("Translation dataset metadata:"); + System.out.println( + String.format( + "\tSource language code: %s", + dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); + System.out.println( + String.format( + "\tTarget language code: %s", + dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); + System.out.println("Dataset create time:"); + System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); + System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); + } } } // [END automl_translate_list_datasets] @@ -165,33 +168,34 @@ public static void listDatasets(String projectId, String computeRegion, String f public static void getDataset(String projectId, String computeRegion, String datasetId) throws Exception { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); - - // Get the complete path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - - // Get all the information about a given dataset. - Dataset dataset = client.getDataset(datasetFullId); - - // Display the dataset information - System.out.println(String.format("Dataset name: %s", dataset.getName())); - System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); - System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); - System.out.println("Translation dataset metadata:"); - System.out.println( - String.format( - "\tSource language code: %s", - dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); - System.out.println( - String.format( - "\tTarget language code: %s", - dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); - System.out.println("Dataset create time:"); - System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); + try (AutoMlClient client = AutoMlClient.create()) { + + // Get the complete path of the dataset. + DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); + + // Get all the information about a given dataset. + Dataset dataset = client.getDataset(datasetFullId); + + // Display the dataset information + System.out.println(String.format("Dataset name: %s", dataset.getName())); + System.out.println( + String.format( + "Dataset id: %s", + dataset.getName().split("/")[dataset.getName().split("/").length - 1])); + System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); + System.out.println("Translation dataset metadata:"); + System.out.println( + String.format( + "\tSource language code: %s", + dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); + System.out.println( + String.format( + "\tTarget language code: %s", + dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); + System.out.println("Dataset create time:"); + System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); + System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); + } } // [END automl_translate_get_dataset] @@ -208,25 +212,26 @@ public static void getDataset(String projectId, String computeRegion, String dat public static void importData( String projectId, String computeRegion, String datasetId, String path) throws Exception { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); + try (AutoMlClient client = AutoMlClient.create()) { - // Get the complete path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); + // Get the complete path of the dataset. + DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - Builder gcsSource = GcsSource.newBuilder(); + Builder gcsSource = GcsSource.newBuilder(); - // Get multiple Google Cloud Storage URIs to import data from - String[] inputUris = path.split(","); - for (String inputUri : inputUris) { - gcsSource.addInputUris(inputUri); - } + // Get multiple Google Cloud Storage URIs to import data from + String[] inputUris = path.split(","); + for (String inputUri : inputUris) { + gcsSource.addInputUris(inputUri); + } - // Import data from the input URI - InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); - System.out.println("Processing import..."); + // Import data from the input URI + InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); + System.out.println("Processing import..."); - Empty response = client.importDataAsync(datasetFullId, inputConfig).get(); - System.out.println(String.format("Dataset imported. %s", response)); + Empty response = client.importDataAsync(datasetFullId, inputConfig).get(30, TimeUnit.MINUTES); + System.out.println(String.format("Dataset imported. %s", response)); + } } // [END automl_translate_import_data] @@ -242,15 +247,16 @@ public static void importData( public static void deleteDataset(String projectId, String computeRegion, String datasetId) throws Exception { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); + try (AutoMlClient client = AutoMlClient.create()) { - // Get the full path of the dataset. - DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); + // Get the full path of the dataset. + DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - // Delete a dataset. - Empty response = client.deleteDatasetAsync(datasetFullId).get(); + // Delete a dataset. + Empty response = client.deleteDatasetAsync(datasetFullId).get(); - System.out.println(String.format("Dataset deleted. %s", response)); + System.out.println(String.format("Dataset deleted. %s", response)); + } } // [END automl_translate_delete_dataset] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ModelApi.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ModelApi.java index 1769d81bcc0..17e542f0d20 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ModelApi.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ModelApi.java @@ -62,30 +62,31 @@ public class ModelApi { public static void createModel( String projectId, String computeRegion, String dataSetId, String modelName) throws Exception { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); - - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - - // Set model metadata. - TranslationModelMetadata translationModelMetadata = - TranslationModelMetadata.newBuilder().setBaseModel("").build(); - - // Set model name, dataset and metadata. - Model myModel = - Model.newBuilder() - .setDisplayName(modelName) - .setDatasetId(dataSetId) - .setTranslationModelMetadata(translationModelMetadata) - .build(); - - // Create a model with the model metadata in the region. - OperationFuture response = - client.createModelAsync(projectLocation, myModel); - - System.out.println( - String.format("Training operation name: %s", response.getInitialFuture().get().getName())); - System.out.println("Training started..."); + try (AutoMlClient client = AutoMlClient.create()) { + + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, computeRegion); + + // Set model metadata. + TranslationModelMetadata translationModelMetadata = + TranslationModelMetadata.newBuilder().setBaseModel("").build(); + + // Set model name, dataset and metadata. + Model myModel = + Model.newBuilder() + .setDisplayName(modelName) + .setDatasetId(dataSetId) + .setTranslationModelMetadata(translationModelMetadata) + .build(); + + // Create a model with the model metadata in the region. + OperationFuture response = + client.createModelAsync(projectLocation, myModel); + + System.out.println( + String.format("Training operation name: %s", response.getInitialFuture().get().getName())); + System.out.println("Training started..."); + } } // [END automl_translate_create_model] @@ -101,31 +102,32 @@ public static void createModel( public static void listModels(String projectId, String computeRegion, String filter) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); - - // A resource that represents Google Cloud Platform location. - LocationName projectLocation = LocationName.of(projectId, computeRegion); - - // Create list models request. - ListModelsRequest listModlesRequest = - ListModelsRequest.newBuilder() - .setParent(projectLocation.toString()) - .setFilter(filter) - .build(); - - // List all the models available in the region by applying filter. - System.out.println("List of models:"); - for (Model model : client.listModels(listModlesRequest).iterateAll()) { - // Display the model information. - System.out.println(String.format("Model name: %s", model.getName())); - System.out.println( - String.format( - "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); - System.out.println(String.format("Model display name: %s", model.getDisplayName())); - System.out.println("Model create time:"); - System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos())); - System.out.println(String.format("Model deployment state: %s", model.getDeploymentState())); + try (AutoMlClient client = AutoMlClient.create()) { + + // A resource that represents Google Cloud Platform location. + LocationName projectLocation = LocationName.of(projectId, computeRegion); + + // Create list models request. + ListModelsRequest listModlesRequest = + ListModelsRequest.newBuilder() + .setParent(projectLocation.toString()) + .setFilter(filter) + .build(); + + // List all the models available in the region by applying filter. + System.out.println("List of models:"); + for (Model model : client.listModels(listModlesRequest).iterateAll()) { + // Display the model information. + System.out.println(String.format("Model name: %s", model.getName())); + System.out.println( + String.format( + "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); + System.out.println(String.format("Model display name: %s", model.getDisplayName())); + System.out.println("Model create time:"); + System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); + System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos())); + System.out.println(String.format("Model deployment state: %s", model.getDeploymentState())); + } } } // [END automl_translate_list_models] @@ -142,24 +144,25 @@ public static void listModels(String projectId, String computeRegion, String fil public static void getModel(String projectId, String computeRegion, String modelId) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); - - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - - // Get complete detail of the model. - Model model = client.getModel(modelFullId); - - // Display the model information. - System.out.println(String.format("Model name: %s", model.getName())); - System.out.println( - String.format( - "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); - System.out.println(String.format("Model display name: %s", model.getDisplayName())); - System.out.println("Model create time:"); - System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); - System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos())); - System.out.println(String.format("Model deployment state: %s", model.getDeploymentState())); + try (AutoMlClient client = AutoMlClient.create()) { + + // Get the full path of the model. + ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); + + // Get complete detail of the model. + Model model = client.getModel(modelFullId); + + // Display the model information. + System.out.println(String.format("Model name: %s", model.getName())); + System.out.println( + String.format( + "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); + System.out.println(String.format("Model display name: %s", model.getDisplayName())); + System.out.println("Model create time:"); + System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); + System.out.println(String.format("\tnanos: %s", model.getCreateTime().getNanos())); + System.out.println(String.format("Model deployment state: %s", model.getDeploymentState())); + } } // [END automl_translate_get_model] @@ -176,23 +179,24 @@ public static void getModel(String projectId, String computeRegion, String model public static void listModelEvaluations( String projectId, String computeRegion, String modelId, String filter) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); - - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - - // Create list model evaluations request - ListModelEvaluationsRequest modelEvaluationsrequest = - ListModelEvaluationsRequest.newBuilder() - .setParent(modelFullId.toString()) - .setFilter(filter) - .build(); - - // List all the model evaluations in the model by applying filter. - System.out.println("List of model evaluations:"); - for (ModelEvaluation element : - client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { - System.out.println(element); + try (AutoMlClient client = AutoMlClient.create()) { + + // Get the full path of the model. + ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); + + // Create list model evaluations request + ListModelEvaluationsRequest modelEvaluationsrequest = + ListModelEvaluationsRequest.newBuilder() + .setParent(modelFullId.toString()) + .setFilter(filter) + .build(); + + // List all the model evaluations in the model by applying filter. + System.out.println("List of model evaluations:"); + for (ModelEvaluation element : + client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { + System.out.println(element); + } } } // [END automl_translate_list_model_evaluations] @@ -211,16 +215,17 @@ public static void getModelEvaluation( String projectId, String computeRegion, String modelId, String modelEvaluationId) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); + try (AutoMlClient client = AutoMlClient.create()) { - // Get the full path of the model evaluation. - ModelEvaluationName modelEvaluationFullId = - ModelEvaluationName.of(projectId, computeRegion, modelId, modelEvaluationId); + // Get the full path of the model evaluation. + ModelEvaluationName modelEvaluationFullId = + ModelEvaluationName.of(projectId, computeRegion, modelId, modelEvaluationId); - // Get complete detail of the model evaluation. - ModelEvaluation response = client.getModelEvaluation(modelEvaluationFullId); + // Get complete detail of the model evaluation. + ModelEvaluation response = client.getModelEvaluation(modelEvaluationFullId); - System.out.println(response); + System.out.println(response); + } } // [END automl_translate_get_model_evaluation] @@ -236,15 +241,16 @@ public static void getModelEvaluation( public static void deleteModel(String projectId, String computeRegion, String modelId) throws InterruptedException, ExecutionException, IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); + try (AutoMlClient client = AutoMlClient.create()) { - // Get the full path of the model. - ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); + // Get the full path of the model. + ModelName modelFullId = ModelName.of(projectId, computeRegion, modelId); - // Delete a model. - Empty response = client.deleteModelAsync(modelFullId).get(); + // Delete a model. + Empty response = client.deleteModelAsync(modelFullId).get(); - System.out.println("Model deletion started..."); + System.out.println("Model deletion started..."); + } } // [END automl_translate_delete_model] @@ -258,12 +264,13 @@ public static void deleteModel(String projectId, String computeRegion, String mo */ private static void getOperationStatus(String operationFullId) throws IOException { // Instantiates a client - AutoMlClient client = AutoMlClient.create(); + try (AutoMlClient client = AutoMlClient.create()) { - // Get the latest state of a long-running operation. - Operation response = client.getOperationsClient().getOperation(operationFullId); + // Get the latest state of a long-running operation. + Operation response = client.getOperationsClient().getOperation(operationFullId); - System.out.println(String.format("Operation status: %s", response)); + System.out.println(String.format("Operation status: %s", response)); + } } // [END automl_translate_get_operation_status] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java index 647e17ff94a..55aba31271e 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java @@ -67,26 +67,28 @@ public static void predict( String projectId, String computeRegion, String modelId, String filePath) throws IOException { // Instantiate client for prediction service. - PredictionServiceClient predictionClient = PredictionServiceClient.create(); + PredictResponse response; + try (PredictionServiceClient predictionClient = PredictionServiceClient.create()) { - // Get the full path of the model. - ModelName name = ModelName.of(projectId, computeRegion, modelId); + // Get the full path of the model. + ModelName name = ModelName.of(projectId, computeRegion, modelId); - // Read the file content for translation. - String content = new String(Files.readAllBytes(Paths.get(filePath))); + // Read the file content for translation. + String content = new String(Files.readAllBytes(Paths.get(filePath))); - TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).build(); + TextSnippet textSnippet = TextSnippet.newBuilder().setContent(content).build(); - // Set the payload by giving the content of the file. - ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build(); + // Set the payload by giving the content of the file. + ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(textSnippet).build(); - // Additional parameters that can be provided for prediction - Map params = new HashMap<>(); + // Additional parameters that can be provided for prediction + Map params = new HashMap<>(); - PredictResponse response = predictionClient.predict(name, payload, params); - TextSnippet translatedContent = response.getPayload(0).getTranslation().getTranslatedContent(); + response = predictionClient.predict(name, payload, params); + TextSnippet translatedContent = response.getPayload(0).getTranslation().getTranslatedContent(); - System.out.println(String.format("Translated Content: %s", translatedContent.getContent())); + System.out.println(String.format("Translated Content: %s", translatedContent.getContent())); + } } // [END automl_translate_predict] diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java index 2f47e55968a..4673fe770dc 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java @@ -40,7 +40,7 @@ public class DatasetApiIT { private PrintStream out; private DatasetApi app; private String datasetId; - private String getdatasetId = "3946265060617537378"; + private String getdatasetId = "TRL3946265060617537378"; @Before public void setUp() { @@ -68,7 +68,7 @@ public void testCreateImportDeleteDataset() throws Exception { assertThat(got).contains("Dataset id:"); // Act - DatasetApi.importData(PROJECT_ID, COMPUTE_REGION, datasetId, "gs://" + BUCKET + "/en-ja.csv"); + DatasetApi.importData(PROJECT_ID, COMPUTE_REGION, datasetId, "gs://" + BUCKET + "/en-ja-short.csv"); // Assert got = bout.toString(); diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java index 8f839493ce3..b4dff46302f 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/PredictionApiIT.java @@ -33,7 +33,7 @@ public class PredictionApiIT { private static final String COMPUTE_REGION = "us-central1"; private static final String PROJECT_ID = "java-docs-samples-testing"; - private static final String modelId = "2188848820815848149"; + private static final String modelId = "TRL2188848820815848149"; private static final String filePath = "./resources/input.txt"; private ByteArrayOutputStream bout; private PrintStream out; From f579649c23800a4f381e6023fb6baf664ec51d2c Mon Sep 17 00:00:00 2001 From: nnegrey Date: Tue, 8 Oct 2019 09:48:20 -0600 Subject: [PATCH 2/4] Update style format, slightly to see if that fixes tests --- .../cloud/translate/automl/DatasetApi.java | 34 ++++++++++--------- .../cloud/translate/automl/PredictionApi.java | 1 - .../cloud/translate/automl/DatasetApiIT.java | 6 ++-- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java index c5f2a9591bb..2a127f3da6b 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java @@ -30,6 +30,7 @@ import java.io.IOException; import java.io.PrintStream; +import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import net.sourceforge.argparse4j.ArgumentParsers; @@ -55,11 +56,9 @@ public class DatasetApi { * @param datasetName the name of the dataset to be created. * @param source the Source language * @param target the Target language - * @throws IOException on Input/Output errors. */ public static void createDataset( - String projectId, String computeRegion, String datasetName, String source, String target) - throws IOException { + String projectId, String computeRegion, String datasetName, String source, String target) { // Instantiates a client try (AutoMlClient client = AutoMlClient.create()) { @@ -102,6 +101,8 @@ public static void createDataset( System.out.println("Dataset create time:"); System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); + } catch (IOException e) { + e.printStackTrace(); } } // [END automl_translate_create_dataset] @@ -113,10 +114,8 @@ public static void createDataset( * @param projectId the Google Cloud Project ID. * @param computeRegion the Region name. (e.g., "us-central1"). * @param filter the Filter expression. - * @throws Exception on AutoML Client errors */ - public static void listDatasets(String projectId, String computeRegion, String filter) - throws IOException { + public static void listDatasets(String projectId, String computeRegion, String filter) { // Instantiates a client try (AutoMlClient client = AutoMlClient.create()) { @@ -152,6 +151,8 @@ public static void listDatasets(String projectId, String computeRegion, String f System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); } + } catch (IOException e) { + e.printStackTrace(); } } // [END automl_translate_list_datasets] @@ -163,10 +164,8 @@ public static void listDatasets(String projectId, String computeRegion, String f * @param projectId the Google Cloud Project ID. * @param computeRegion the Region name. (e.g., "us-central1"). * @param datasetId the Id of the dataset. - * @throws Exception on AutoML Client errors */ - public static void getDataset(String projectId, String computeRegion, String datasetId) - throws Exception { + public static void getDataset(String projectId, String computeRegion, String datasetId) { // Instantiates a client try (AutoMlClient client = AutoMlClient.create()) { @@ -195,6 +194,8 @@ public static void getDataset(String projectId, String computeRegion, String dat System.out.println("Dataset create time:"); System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); + } catch (IOException e) { + e.printStackTrace(); } } // [END automl_translate_get_dataset] @@ -207,10 +208,9 @@ public static void getDataset(String projectId, String computeRegion, String dat * @param computeRegion the Region name. (e.g., "us-central1"). * @param datasetId the Id of the dataset. * @param path the remote Path of the training data csv file. - * @throws Exception on AutoML Client errors */ public static void importData( - String projectId, String computeRegion, String datasetId, String path) throws Exception { + String projectId, String computeRegion, String datasetId, String path) { // Instantiates a client try (AutoMlClient client = AutoMlClient.create()) { @@ -229,8 +229,10 @@ public static void importData( InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); System.out.println("Processing import..."); - Empty response = client.importDataAsync(datasetFullId, inputConfig).get(30, TimeUnit.MINUTES); + Empty response = client.importDataAsync(datasetFullId, inputConfig).get(); System.out.println(String.format("Dataset imported. %s", response)); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); } } // [END automl_translate_import_data] @@ -242,10 +244,8 @@ public static void importData( * @param projectId the Google Cloud Project ID. * @param computeRegion the Region name. (e.g., "us-central1"). * @param datasetId the Id of the dataset. - * @throws Exception on AutoML Client errors */ - public static void deleteDataset(String projectId, String computeRegion, String datasetId) - throws Exception { + public static void deleteDataset(String projectId, String computeRegion, String datasetId) { // Instantiates a client try (AutoMlClient client = AutoMlClient.create()) { @@ -256,6 +256,8 @@ public static void deleteDataset(String projectId, String computeRegion, String Empty response = client.deleteDatasetAsync(datasetFullId).get(); System.out.println(String.format("Dataset deleted. %s", response)); + } catch (IOException | InterruptedException | ExecutionException e) { + e.printStackTrace(); } } // [END automl_translate_delete_dataset] @@ -290,7 +292,7 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception { String projectId = System.getenv("PROJECT_ID"); String computeRegion = System.getenv("REGION_NAME"); - Namespace ns = null; + Namespace ns; try { ns = parser.parseArgs(args); if (ns.get("command").equals("create_dataset")) { diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java index 55aba31271e..30444bad285 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java @@ -53,7 +53,6 @@ public class PredictionApi { // [START automl_translate_predict] - /** * Demonstrates using the AutoML client to predict an image. * diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java index 4673fe770dc..32a629443f1 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java @@ -55,7 +55,7 @@ public void tearDown() { } @Test - public void testCreateImportDeleteDataset() throws Exception { + public void testCreateImportDeleteDataset() { // Act DatasetApi.createDataset(PROJECT_ID, COMPUTE_REGION, DATASET_NAME, "en", "ja"); @@ -83,7 +83,7 @@ public void testCreateImportDeleteDataset() throws Exception { } @Test - public void testListDataset() throws Exception { + public void testListDataset() { // Act DatasetApi.listDatasets(PROJECT_ID, COMPUTE_REGION, "translation_dataset_metadata:*"); @@ -93,7 +93,7 @@ public void testListDataset() throws Exception { } @Test - public void testGetDataset() throws Exception { + public void testGetDataset() { // Act DatasetApi.getDataset(PROJECT_ID, COMPUTE_REGION, getdatasetId); From 9d1aee5e24d7d2cb78c011388d985f4a580be883 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Tue, 8 Oct 2019 14:29:25 -0600 Subject: [PATCH 3/4] Update to latest library and fix some syntax to fix the tests --- translate/automl/pom.xml | 2 +- .../cloud/translate/automl/DatasetApi.java | 82 +++++++++---------- .../cloud/translate/automl/ModelApi.java | 45 +++++----- .../cloud/translate/automl/PredictionApi.java | 4 +- .../cloud/translate/automl/DatasetApiIT.java | 13 ++- 5 files changed, 76 insertions(+), 70 deletions(-) diff --git a/translate/automl/pom.xml b/translate/automl/pom.xml index 221014744c8..ded7f5f3235 100644 --- a/translate/automl/pom.xml +++ b/translate/automl/pom.xml @@ -40,7 +40,7 @@ com.google.cloud google-cloud-automl - 0.55.0-beta + 0.113.0-beta net.sourceforge.argparse4j diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java index 2a127f3da6b..690400682ce 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java @@ -21,7 +21,6 @@ import com.google.cloud.automl.v1beta1.Dataset; import com.google.cloud.automl.v1beta1.DatasetName; import com.google.cloud.automl.v1beta1.GcsSource; -import com.google.cloud.automl.v1beta1.GcsSource.Builder; import com.google.cloud.automl.v1beta1.InputConfig; import com.google.cloud.automl.v1beta1.ListDatasetsRequest; import com.google.cloud.automl.v1beta1.LocationName; @@ -31,7 +30,6 @@ import java.io.IOException; import java.io.PrintStream; import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; import net.sourceforge.argparse4j.ArgumentParsers; import net.sourceforge.argparse4j.inf.ArgumentParser; @@ -67,17 +65,17 @@ public static void createDataset( // Specify the source and target language. TranslationDatasetMetadata translationDatasetMetadata = - TranslationDatasetMetadata.newBuilder() - .setSourceLanguageCode(source) - .setTargetLanguageCode(target) - .build(); + TranslationDatasetMetadata.newBuilder() + .setSourceLanguageCode(source) + .setTargetLanguageCode(target) + .build(); // Set dataset name and dataset metadata. Dataset myDataset = - Dataset.newBuilder() - .setDisplayName(datasetName) - .setTranslationDatasetMetadata(translationDatasetMetadata) - .build(); + Dataset.newBuilder() + .setDisplayName(datasetName) + .setTranslationDatasetMetadata(translationDatasetMetadata) + .build(); // Create a dataset with the dataset metadata in the region. Dataset dataset = client.createDataset(projectLocation, myDataset); @@ -85,19 +83,19 @@ public static void createDataset( // Display the dataset information. System.out.println(String.format("Dataset name: %s", dataset.getName())); System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); + String.format( + "Dataset id: %s", + dataset.getName().split("/")[dataset.getName().split("/").length - 1])); System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); System.out.println("Translation dataset Metadata:"); System.out.println( - String.format( - "\tSource language code: %s", - dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); + String.format( + "\tSource language code: %s", + dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); System.out.println( - String.format( - "\tTarget language code: %s", - dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); + String.format( + "\tTarget language code: %s", + dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); System.out.println("Dataset create time:"); System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); @@ -123,10 +121,10 @@ public static void listDatasets(String projectId, String computeRegion, String f LocationName projectLocation = LocationName.of(projectId, computeRegion); ListDatasetsRequest request = - ListDatasetsRequest.newBuilder() - .setParent(projectLocation.toString()) - .setFilter(filter) - .build(); + ListDatasetsRequest.newBuilder() + .setParent(projectLocation.toString()) + .setFilter(filter) + .build(); // List all the datasets available in the region by applying filter. System.out.println("List of datasets:"); @@ -134,19 +132,19 @@ public static void listDatasets(String projectId, String computeRegion, String f // Display the dataset information System.out.println(String.format("\nDataset name: %s", dataset.getName())); System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); + String.format( + "Dataset id: %s", + dataset.getName().split("/")[dataset.getName().split("/").length - 1])); System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); System.out.println("Translation dataset metadata:"); System.out.println( - String.format( - "\tSource language code: %s", - dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); + String.format( + "\tSource language code: %s", + dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); System.out.println( - String.format( - "\tTarget language code: %s", - dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); + String.format( + "\tTarget language code: %s", + dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); System.out.println("Dataset create time:"); System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); @@ -178,19 +176,19 @@ public static void getDataset(String projectId, String computeRegion, String dat // Display the dataset information System.out.println(String.format("Dataset name: %s", dataset.getName())); System.out.println( - String.format( - "Dataset id: %s", - dataset.getName().split("/")[dataset.getName().split("/").length - 1])); + String.format( + "Dataset id: %s", + dataset.getName().split("/")[dataset.getName().split("/").length - 1])); System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName())); System.out.println("Translation dataset metadata:"); System.out.println( - String.format( - "\tSource language code: %s", - dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); + String.format( + "\tSource language code: %s", + dataset.getTranslationDatasetMetadata().getSourceLanguageCode())); System.out.println( - String.format( - "\tTarget language code: %s", - dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); + String.format( + "\tTarget language code: %s", + dataset.getTranslationDatasetMetadata().getTargetLanguageCode())); System.out.println("Dataset create time:"); System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); @@ -217,7 +215,7 @@ public static void importData( // Get the complete path of the dataset. DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId); - Builder gcsSource = GcsSource.newBuilder(); + GcsSource.Builder gcsSource = GcsSource.newBuilder(); // Get multiple Google Cloud Storage URIs to import data from String[] inputUris = path.split(","); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ModelApi.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ModelApi.java index 17e542f0d20..937b9b7c0ee 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ModelApi.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ModelApi.java @@ -69,22 +69,23 @@ public static void createModel( // Set model metadata. TranslationModelMetadata translationModelMetadata = - TranslationModelMetadata.newBuilder().setBaseModel("").build(); + TranslationModelMetadata.newBuilder().setBaseModel("").build(); // Set model name, dataset and metadata. Model myModel = - Model.newBuilder() - .setDisplayName(modelName) - .setDatasetId(dataSetId) - .setTranslationModelMetadata(translationModelMetadata) - .build(); + Model.newBuilder() + .setDisplayName(modelName) + .setDatasetId(dataSetId) + .setTranslationModelMetadata(translationModelMetadata) + .build(); // Create a model with the model metadata in the region. OperationFuture response = - client.createModelAsync(projectLocation, myModel); + client.createModelAsync(projectLocation, myModel); System.out.println( - String.format("Training operation name: %s", response.getInitialFuture().get().getName())); + String.format( + "Training operation name: %s", response.getInitialFuture().get().getName())); System.out.println("Training started..."); } } @@ -109,10 +110,10 @@ public static void listModels(String projectId, String computeRegion, String fil // Create list models request. ListModelsRequest listModlesRequest = - ListModelsRequest.newBuilder() - .setParent(projectLocation.toString()) - .setFilter(filter) - .build(); + ListModelsRequest.newBuilder() + .setParent(projectLocation.toString()) + .setFilter(filter) + .build(); // List all the models available in the region by applying filter. System.out.println("List of models:"); @@ -120,8 +121,8 @@ public static void listModels(String projectId, String computeRegion, String fil // Display the model information. System.out.println(String.format("Model name: %s", model.getName())); System.out.println( - String.format( - "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); + String.format( + "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); System.out.println(String.format("Model display name: %s", model.getDisplayName())); System.out.println("Model create time:"); System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); @@ -155,8 +156,8 @@ public static void getModel(String projectId, String computeRegion, String model // Display the model information. System.out.println(String.format("Model name: %s", model.getName())); System.out.println( - String.format( - "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); + String.format( + "Model id: %s", model.getName().split("/")[model.getName().split("/").length - 1])); System.out.println(String.format("Model display name: %s", model.getDisplayName())); System.out.println("Model create time:"); System.out.println(String.format("\tseconds: %s", model.getCreateTime().getSeconds())); @@ -186,15 +187,15 @@ public static void listModelEvaluations( // Create list model evaluations request ListModelEvaluationsRequest modelEvaluationsrequest = - ListModelEvaluationsRequest.newBuilder() - .setParent(modelFullId.toString()) - .setFilter(filter) - .build(); + ListModelEvaluationsRequest.newBuilder() + .setParent(modelFullId.toString()) + .setFilter(filter) + .build(); // List all the model evaluations in the model by applying filter. System.out.println("List of model evaluations:"); for (ModelEvaluation element : - client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { + client.listModelEvaluations(modelEvaluationsrequest).iterateAll()) { System.out.println(element); } } @@ -219,7 +220,7 @@ public static void getModelEvaluation( // Get the full path of the model evaluation. ModelEvaluationName modelEvaluationFullId = - ModelEvaluationName.of(projectId, computeRegion, modelId, modelEvaluationId); + ModelEvaluationName.of(projectId, computeRegion, modelId, modelEvaluationId); // Get complete detail of the model evaluation. ModelEvaluation response = client.getModelEvaluation(modelEvaluationFullId); diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java index 30444bad285..e725f2ab585 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java @@ -84,7 +84,8 @@ public static void predict( Map params = new HashMap<>(); response = predictionClient.predict(name, payload, params); - TextSnippet translatedContent = response.getPayload(0).getTranslation().getTranslatedContent(); + TextSnippet translatedContent = + response.getPayload(0).getTranslation().getTranslatedContent(); System.out.println(String.format("Translated Content: %s", translatedContent.getContent())); } @@ -117,7 +118,6 @@ public static void argsHelper(String[] args, PrintStream out) throws IOException ns = parser.parseArgs(args); if (ns.get("command").equals("predict")) { predict(projectId, computeRegion, ns.getString("modelId"), ns.getString("filePath")); - } } catch (ArgumentParserException e) { parser.handleError(e); diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java index 32a629443f1..2a48a307513 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java @@ -20,6 +20,7 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.util.UUID; import org.junit.After; import org.junit.Before; @@ -35,7 +36,6 @@ public class DatasetApiIT { private static final String PROJECT_ID = "java-docs-samples-testing"; private static final String BUCKET = PROJECT_ID + "-vcm"; private static final String COMPUTE_REGION = "us-central1"; - private static final String DATASET_NAME = "test_translate_dataset"; private ByteArrayOutputStream bout; private PrintStream out; private DatasetApi app; @@ -56,8 +56,14 @@ public void tearDown() { @Test public void testCreateImportDeleteDataset() { + // Create a random dataset name with a length of 32 characters (max allowed by AutoML) + // To prevent name collisions when running tests in multiple java versions at once. + // AutoML doesn't allow "-", but accepts "_" + String datasetName = + String.format("test_%s", UUID.randomUUID().toString().replace("-", "_").substring(0, 26)); + // Act - DatasetApi.createDataset(PROJECT_ID, COMPUTE_REGION, DATASET_NAME, "en", "ja"); + DatasetApi.createDataset(PROJECT_ID, COMPUTE_REGION, datasetName, "en", "ja"); // Assert String got = bout.toString(); @@ -68,7 +74,8 @@ public void testCreateImportDeleteDataset() { assertThat(got).contains("Dataset id:"); // Act - DatasetApi.importData(PROJECT_ID, COMPUTE_REGION, datasetId, "gs://" + BUCKET + "/en-ja-short.csv"); + DatasetApi.importData( + PROJECT_ID, COMPUTE_REGION, datasetId, "gs://" + BUCKET + "/en-ja-short.csv"); // Assert got = bout.toString(); From 8a744aa3194a0726ee1aff9a658ce1f073afe039 Mon Sep 17 00:00:00 2001 From: nnegrey Date: Fri, 25 Oct 2019 10:29:04 -0600 Subject: [PATCH 4/4] Address review comments --- .../cloud/translate/automl/DatasetApi.java | 25 ++++++++----------- .../cloud/translate/automl/ModelApi.java | 4 +-- .../cloud/translate/automl/PredictionApi.java | 1 - .../cloud/translate/automl/DatasetApiIT.java | 9 ++++--- .../cloud/translate/automl/ModelApiIT.java | 2 -- 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java index 690400682ce..8616e0c1bae 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/DatasetApi.java @@ -56,7 +56,8 @@ public class DatasetApi { * @param target the Target language */ public static void createDataset( - String projectId, String computeRegion, String datasetName, String source, String target) { + String projectId, String computeRegion, String datasetName, String source, String target) + throws IOException { // Instantiates a client try (AutoMlClient client = AutoMlClient.create()) { @@ -99,8 +100,6 @@ public static void createDataset( System.out.println("Dataset create time:"); System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); - } catch (IOException e) { - e.printStackTrace(); } } // [END automl_translate_create_dataset] @@ -113,7 +112,8 @@ public static void createDataset( * @param computeRegion the Region name. (e.g., "us-central1"). * @param filter the Filter expression. */ - public static void listDatasets(String projectId, String computeRegion, String filter) { + public static void listDatasets(String projectId, String computeRegion, String filter) + throws IOException { // Instantiates a client try (AutoMlClient client = AutoMlClient.create()) { @@ -149,8 +149,6 @@ public static void listDatasets(String projectId, String computeRegion, String f System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); } - } catch (IOException e) { - e.printStackTrace(); } } // [END automl_translate_list_datasets] @@ -163,7 +161,8 @@ public static void listDatasets(String projectId, String computeRegion, String f * @param computeRegion the Region name. (e.g., "us-central1"). * @param datasetId the Id of the dataset. */ - public static void getDataset(String projectId, String computeRegion, String datasetId) { + public static void getDataset(String projectId, String computeRegion, String datasetId) + throws IOException { // Instantiates a client try (AutoMlClient client = AutoMlClient.create()) { @@ -192,8 +191,6 @@ public static void getDataset(String projectId, String computeRegion, String dat System.out.println("Dataset create time:"); System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds())); System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos())); - } catch (IOException e) { - e.printStackTrace(); } } // [END automl_translate_get_dataset] @@ -208,7 +205,8 @@ public static void getDataset(String projectId, String computeRegion, String dat * @param path the remote Path of the training data csv file. */ public static void importData( - String projectId, String computeRegion, String datasetId, String path) { + String projectId, String computeRegion, String datasetId, String path) + throws IOException, InterruptedException, ExecutionException { // Instantiates a client try (AutoMlClient client = AutoMlClient.create()) { @@ -229,8 +227,6 @@ public static void importData( Empty response = client.importDataAsync(datasetFullId, inputConfig).get(); System.out.println(String.format("Dataset imported. %s", response)); - } catch (IOException | InterruptedException | ExecutionException e) { - e.printStackTrace(); } } // [END automl_translate_import_data] @@ -243,7 +239,8 @@ public static void importData( * @param computeRegion the Region name. (e.g., "us-central1"). * @param datasetId the Id of the dataset. */ - public static void deleteDataset(String projectId, String computeRegion, String datasetId) { + public static void deleteDataset(String projectId, String computeRegion, String datasetId) + throws IOException, InterruptedException, ExecutionException { // Instantiates a client try (AutoMlClient client = AutoMlClient.create()) { @@ -254,8 +251,6 @@ public static void deleteDataset(String projectId, String computeRegion, String Empty response = client.deleteDatasetAsync(datasetFullId).get(); System.out.println(String.format("Dataset deleted. %s", response)); - } catch (IOException | InterruptedException | ExecutionException e) { - e.printStackTrace(); } } // [END automl_translate_delete_dataset] diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/ModelApi.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/ModelApi.java index 937b9b7c0ee..805aa0ab5f7 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/ModelApi.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/ModelApi.java @@ -57,10 +57,10 @@ public class ModelApi { * @param computeRegion the Region name. * @param dataSetId the Id of the dataset to which model is created. * @param modelName the Name of the model. - * @throws Exception on AutoML Client errors */ public static void createModel( - String projectId, String computeRegion, String dataSetId, String modelName) throws Exception { + String projectId, String computeRegion, String dataSetId, String modelName) + throws IOException, InterruptedException, ExecutionException { // Instantiates a client try (AutoMlClient client = AutoMlClient.create()) { diff --git a/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java b/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java index e725f2ab585..98bbbc03b40 100644 --- a/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java +++ b/translate/automl/src/main/java/com/google/cloud/translate/automl/PredictionApi.java @@ -29,7 +29,6 @@ import com.google.cloud.automl.v1beta1.ModelName; import com.google.cloud.automl.v1beta1.PredictResponse; import com.google.cloud.automl.v1beta1.PredictionServiceClient; - import com.google.cloud.automl.v1beta1.TextSnippet; import java.io.IOException; import java.io.PrintStream; diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java index 2a48a307513..2bb697faa02 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/DatasetApiIT.java @@ -19,8 +19,10 @@ import static com.google.common.truth.Truth.assertThat; import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; import java.util.UUID; +import java.util.concurrent.ExecutionException; import org.junit.After; import org.junit.Before; @@ -55,7 +57,8 @@ public void tearDown() { } @Test - public void testCreateImportDeleteDataset() { + public void testCreateImportDeleteDataset() + throws IOException, ExecutionException, InterruptedException { // Create a random dataset name with a length of 32 characters (max allowed by AutoML) // To prevent name collisions when running tests in multiple java versions at once. // AutoML doesn't allow "-", but accepts "_" @@ -90,7 +93,7 @@ public void testCreateImportDeleteDataset() { } @Test - public void testListDataset() { + public void testListDataset() throws IOException { // Act DatasetApi.listDatasets(PROJECT_ID, COMPUTE_REGION, "translation_dataset_metadata:*"); @@ -100,7 +103,7 @@ public void testListDataset() { } @Test - public void testGetDataset() { + public void testGetDataset() throws IOException { // Act DatasetApi.getDataset(PROJECT_ID, COMPUTE_REGION, getdatasetId); diff --git a/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelApiIT.java b/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelApiIT.java index 0ebfeed339b..fff8a1a1561 100644 --- a/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelApiIT.java +++ b/translate/automl/src/test/java/com/google/cloud/translate/automl/ModelApiIT.java @@ -83,7 +83,5 @@ public void testModelApi() throws Exception { // Assert got = bout.toString(); assertThat(got).contains("name:"); - } } -