diff --git a/automl/beta/pom.xml b/automl/beta/pom.xml index de781956c72..17a1fec5cb3 100644 --- a/automl/beta/pom.xml +++ b/automl/beta/pom.xml @@ -40,7 +40,7 @@ com.google.cloud libraries-bom - 4.4.1 + 5.2.0 pom import @@ -60,7 +60,6 @@ com.google.cloud google-cloud-storage - 1.107.0 net.sourceforge.argparse4j diff --git a/automl/beta/src/main/java/com/example/automl/ImportDataset.java b/automl/beta/src/main/java/com/example/automl/ImportDataset.java index 5f58e364138..651fd43eaf1 100644 --- a/automl/beta/src/main/java/com/example/automl/ImportDataset.java +++ b/automl/beta/src/main/java/com/example/automl/ImportDataset.java @@ -17,19 +17,26 @@ package com.example.automl; // [START automl_import_dataset_beta] +import com.google.api.gax.longrunning.OperationFuture; +import com.google.api.gax.retrying.RetrySettings; import com.google.cloud.automl.v1beta1.AutoMlClient; +import com.google.cloud.automl.v1beta1.AutoMlSettings; import com.google.cloud.automl.v1beta1.DatasetName; import com.google.cloud.automl.v1beta1.GcsSource; import com.google.cloud.automl.v1beta1.InputConfig; +import com.google.cloud.automl.v1beta1.OperationMetadata; import com.google.protobuf.Empty; import java.io.IOException; import java.util.Arrays; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.threeten.bp.Duration; class ImportDataset { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. String projectId = "YOUR_PROJECT_ID"; String datasetId = "YOUR_DATASET_ID"; @@ -39,11 +46,17 @@ public static void main(String[] args) // Import a dataset static void importDataset(String projectId, String datasetId, String path) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { + Duration totalTimeout = Duration.ofMinutes(45); + RetrySettings retrySettings = RetrySettings.newBuilder().setTotalTimeout(totalTimeout).build(); + AutoMlSettings.Builder builder = AutoMlSettings.newBuilder(); + builder.importDataSettings().setRetrySettings(retrySettings).build(); + AutoMlSettings settings = builder.build(); + // 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()) { + try (AutoMlClient client = AutoMlClient.create(settings)) { // Get the complete path of the dataset. DatasetName datasetFullId = DatasetName.of(projectId, "us-central1", datasetId); @@ -55,8 +68,22 @@ static void importDataset(String projectId, String datasetId, String path) InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); System.out.println("Processing import..."); - Empty response = client.importDataAsync(datasetFullId, inputConfig).get(); + // Start the import job + OperationFuture operation = client + .importDataAsync(datasetFullId, inputConfig); + + System.out.format("Operation name: %s%n", operation.getName()); + + // If you want to wait for the operation to finish, adjust the timeout appropriately. The + // operation will still run if you choose not to wait for it to complete. You can check the + // status of your operation using the operation's name. + Empty response = operation.get(45, TimeUnit.MINUTES); System.out.format("Dataset imported. %s%n", response); + } catch (TimeoutException e) { + System.out.println("The operation's polling period was not long enough."); + System.out.println("You can use the Operation's name to get the current status."); + System.out.println("The import job is still running and will complete as expected."); + throw e; } } } diff --git a/automl/beta/src/test/java/com/example/automl/ImportDatasetTest.java b/automl/beta/src/test/java/com/example/automl/ImportDatasetTest.java index db92201be1d..577f0c140d1 100644 --- a/automl/beta/src/test/java/com/example/automl/ImportDatasetTest.java +++ b/automl/beta/src/test/java/com/example/automl/ImportDatasetTest.java @@ -28,6 +28,7 @@ import java.io.PrintStream; import java.util.UUID; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; @@ -92,7 +93,8 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept } @Test - public void testImportDataset() throws IOException, ExecutionException, InterruptedException { + public void testImportDataset() + throws InterruptedException, ExecutionException, TimeoutException, IOException { ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/entity-extraction/dataset.csv"); String got = bout.toString(); assertThat(got).contains("Dataset imported."); diff --git a/automl/beta/src/test/java/com/example/automl/TablesImportDatasetTest.java b/automl/beta/src/test/java/com/example/automl/TablesImportDatasetTest.java index 8dea7e76e39..71cebe3371e 100644 --- a/automl/beta/src/test/java/com/example/automl/TablesImportDatasetTest.java +++ b/automl/beta/src/test/java/com/example/automl/TablesImportDatasetTest.java @@ -65,7 +65,7 @@ public void tearDown() { @Test public void testTablesImportDataset() { try { - ImportDataset.importDataset( + TablesImportDataset.importDataset( PROJECT_ID, "TEN0000000000000000000", "gs://cloud-ml-tables-data/bank-marketing.csv"); String got = bout.toString(); assertThat(got).contains("The Dataset doesn't exist or is inaccessible for use with AutoMl."); diff --git a/automl/cloud-client/pom.xml b/automl/cloud-client/pom.xml index e7b98adb2d1..ebbede2fae1 100644 --- a/automl/cloud-client/pom.xml +++ b/automl/cloud-client/pom.xml @@ -32,18 +32,30 @@ UTF-8 + + + + + + com.google.cloud + libraries-bom + 5.2.0 + pom + import + + + + - com.google.cloud google-cloud-automl - 1.1.1 com.google.cloud google-cloud-storage - 1.107.0 net.sourceforge.argparse4j @@ -64,5 +76,7 @@ 1.0.1 test + + \ No newline at end of file diff --git a/automl/cloud-client/src/main/java/com/example/automl/ImportDataset.java b/automl/cloud-client/src/main/java/com/example/automl/ImportDataset.java index 01996669f6a..3ead88326b3 100644 --- a/automl/cloud-client/src/main/java/com/example/automl/ImportDataset.java +++ b/automl/cloud-client/src/main/java/com/example/automl/ImportDataset.java @@ -17,19 +17,23 @@ package com.example.automl; // [START automl_import_dataset] +import com.google.api.gax.longrunning.OperationFuture; import com.google.cloud.automl.v1.AutoMlClient; import com.google.cloud.automl.v1.DatasetName; import com.google.cloud.automl.v1.GcsSource; import com.google.cloud.automl.v1.InputConfig; +import com.google.cloud.automl.v1.OperationMetadata; import com.google.protobuf.Empty; import java.io.IOException; import java.util.Arrays; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; class ImportDataset { public static void main(String[] args) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // TODO(developer): Replace these variables before running the sample. String projectId = "YOUR_PROJECT_ID"; String datasetId = "YOUR_DATASET_ID"; @@ -39,7 +43,7 @@ public static void main(String[] args) // Import a dataset static void importDataset(String projectId, String datasetId, String path) - throws IOException, ExecutionException, InterruptedException { + throws IOException, ExecutionException, InterruptedException, TimeoutException { // 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. @@ -55,8 +59,22 @@ static void importDataset(String projectId, String datasetId, String path) InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build(); System.out.println("Processing import..."); - Empty response = client.importDataAsync(datasetFullId, inputConfig).get(); - System.out.format("Dataset imported. %s\n", response); + // Start the import job + OperationFuture operation = + client.importDataAsync(datasetFullId, inputConfig); + + System.out.format("Operation name: %s%n", operation.getName()); + + // If you want to wait for the operation to finish, adjust the timeout appropriately. The + // operation will still run if you choose not to wait for it to complete. You can check the + // status of your operation using the operation's name. + Empty response = operation.get(45, TimeUnit.MINUTES); + System.out.format("Dataset imported. %s%n", response); + } catch (TimeoutException e) { + System.out.println("The operation's polling period was not long enough."); + System.out.println("You can use the Operation's name to get the current status."); + System.out.println("The import job is still running and will complete as expected."); + throw e; } } } diff --git a/automl/cloud-client/src/test/java/com/example/automl/ImportDatasetTest.java b/automl/cloud-client/src/test/java/com/example/automl/ImportDatasetTest.java index 164c6445c94..9933a5d7d1a 100644 --- a/automl/cloud-client/src/test/java/com/example/automl/ImportDatasetTest.java +++ b/automl/cloud-client/src/test/java/com/example/automl/ImportDatasetTest.java @@ -24,6 +24,7 @@ import java.io.PrintStream; import java.util.UUID; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeoutException; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; @@ -83,7 +84,8 @@ public void tearDown() throws InterruptedException, ExecutionException, IOExcept } @Test - public void testImportDataset() throws IOException, ExecutionException, InterruptedException { + public void testImportDataset() + throws IOException, ExecutionException, InterruptedException, TimeoutException { ImportDataset.importDataset(PROJECT_ID, datasetId, BUCKET + "/entity-extraction/dataset.csv"); String got = bout.toString(); assertThat(got).contains("Dataset imported.");