Skip to content

Commit c5aedde

Browse files
authored
Fix failing tests: (#1599)
* Update samples to use try for clients and update model IDs due to API backend changes * Update to latest library to fix timeout
1 parent fbc1ae2 commit c5aedde

File tree

7 files changed

+307
-294
lines changed

7 files changed

+307
-294
lines changed

language/automl/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<dependency>
4141
<groupId>com.google.cloud</groupId>
4242
<artifactId>google-cloud-automl</artifactId>
43-
<version>0.55.1-beta</version>
43+
<version>0.114.0-beta</version>
4444
</dependency>
4545
<!-- [END automl_language_java_dependencies] -->
4646
<dependency>

language/automl/src/main/java/com/google/cloud/language/samples/DatasetApi.java

Lines changed: 122 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import java.io.IOException;
3434
import java.io.PrintStream;
35+
import java.util.Arrays;
3536

3637
import net.sourceforge.argparse4j.ArgumentParsers;
3738
import net.sourceforge.argparse4j.inf.ArgumentParser;
@@ -62,76 +63,33 @@ public static void createDataset(
6263
String projectId, String computeRegion, String datasetName, Boolean multiLabel)
6364
throws IOException {
6465
// Instantiates a client
65-
AutoMlClient client = AutoMlClient.create();
66-
67-
// A resource that represents Google Cloud Platform location.
68-
LocationName projectLocation = LocationName.of(projectId, computeRegion);
69-
70-
// Classification type assigned based on multilabel value.
71-
ClassificationType classificationType =
72-
multiLabel ? ClassificationType.MULTILABEL : ClassificationType.MULTICLASS;
73-
74-
// Specify the text classification type for the dataset.
75-
TextClassificationDatasetMetadata textClassificationDatasetMetadata =
76-
TextClassificationDatasetMetadata.newBuilder()
77-
.setClassificationType(classificationType)
78-
.build();
79-
80-
// Set dataset name and dataset metadata.
81-
Dataset myDataset =
82-
Dataset.newBuilder()
83-
.setDisplayName(datasetName)
84-
.setTextClassificationDatasetMetadata(textClassificationDatasetMetadata)
85-
.build();
86-
87-
// Create a dataset with the dataset metadata in the region.
88-
Dataset dataset = client.createDataset(projectLocation, myDataset);
89-
90-
// Display the dataset information.
91-
System.out.println(String.format("Dataset name: %s", dataset.getName()));
92-
System.out.println(
93-
String.format(
94-
"Dataset id: %s",
95-
dataset.getName().split("/")[dataset.getName().split("/").length - 1]));
96-
System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName()));
97-
System.out.println("Text classification dataset metadata:");
98-
System.out.print(String.format("\t%s", dataset.getTextClassificationDatasetMetadata()));
99-
System.out.println(String.format("Dataset example count: %d", dataset.getExampleCount()));
100-
System.out.println("Dataset create time:");
101-
System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds()));
102-
System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos()));
103-
}
104-
// [END automl_language_create_dataset]
66+
try (AutoMlClient client = AutoMlClient.create()) {
10567

106-
// [START automl_language_list_datasets]
107-
/**
108-
* Demonstrates using the AutoML client to list all datasets.
109-
*
110-
* @param projectId the Id of the project.
111-
* @param computeRegion the Region name.
112-
* @param filter the Filter expression.
113-
* @throws IOException on Input/Output errors.
114-
*/
115-
public static void listDatasets(String projectId, String computeRegion, String filter)
116-
throws IOException {
117-
// Instantiates a client
118-
AutoMlClient client = AutoMlClient.create();
68+
// A resource that represents Google Cloud Platform location.
69+
LocationName projectLocation = LocationName.of(projectId, computeRegion);
11970

120-
// A resource that represents Google Cloud Platform location.
121-
LocationName projectLocation = LocationName.of(projectId, computeRegion);
71+
// Classification type assigned based on multilabel value.
72+
ClassificationType classificationType =
73+
multiLabel ? ClassificationType.MULTILABEL : ClassificationType.MULTICLASS;
12274

123-
// Build the List datasets request
124-
ListDatasetsRequest request =
125-
ListDatasetsRequest.newBuilder()
126-
.setParent(projectLocation.toString())
127-
.setFilter(filter)
128-
.build();
75+
// Specify the text classification type for the dataset.
76+
TextClassificationDatasetMetadata textClassificationDatasetMetadata =
77+
TextClassificationDatasetMetadata.newBuilder()
78+
.setClassificationType(classificationType)
79+
.build();
80+
81+
// Set dataset name and dataset metadata.
82+
Dataset myDataset =
83+
Dataset.newBuilder()
84+
.setDisplayName(datasetName)
85+
.setTextClassificationDatasetMetadata(textClassificationDatasetMetadata)
86+
.build();
87+
88+
// Create a dataset with the dataset metadata in the region.
89+
Dataset dataset = client.createDataset(projectLocation, myDataset);
12990

130-
// List all the datasets available in the region by applying filter.
131-
System.out.println("List of datasets:");
132-
for (Dataset dataset : client.listDatasets(request).iterateAll()) {
13391
// Display the dataset information.
134-
System.out.println(String.format("\nDataset name: %s", dataset.getName()));
92+
System.out.println(String.format("Dataset name: %s", dataset.getName()));
13593
System.out.println(
13694
String.format(
13795
"Dataset id: %s",
@@ -145,6 +103,51 @@ public static void listDatasets(String projectId, String computeRegion, String f
145103
System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos()));
146104
}
147105
}
106+
// [END automl_language_create_dataset]
107+
108+
// [START automl_language_list_datasets]
109+
/**
110+
* Demonstrates using the AutoML client to list all datasets.
111+
*
112+
* @param projectId the Id of the project.
113+
* @param computeRegion the Region name.
114+
* @param filter the Filter expression.
115+
* @throws IOException on Input/Output errors.
116+
*/
117+
public static void listDatasets(String projectId, String computeRegion, String filter)
118+
throws IOException {
119+
// Instantiates a client
120+
try (AutoMlClient client = AutoMlClient.create()) {
121+
122+
// A resource that represents Google Cloud Platform location.
123+
LocationName projectLocation = LocationName.of(projectId, computeRegion);
124+
125+
// Build the List datasets request
126+
ListDatasetsRequest request =
127+
ListDatasetsRequest.newBuilder()
128+
.setParent(projectLocation.toString())
129+
.setFilter(filter)
130+
.build();
131+
132+
// List all the datasets available in the region by applying filter.
133+
System.out.println("List of datasets:");
134+
for (Dataset dataset : client.listDatasets(request).iterateAll()) {
135+
// Display the dataset information.
136+
System.out.println(String.format("\nDataset name: %s", dataset.getName()));
137+
System.out.println(
138+
String.format(
139+
"Dataset id: %s",
140+
dataset.getName().split("/")[dataset.getName().split("/").length - 1]));
141+
System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName()));
142+
System.out.println("Text classification dataset metadata:");
143+
System.out.print(String.format("\t%s", dataset.getTextClassificationDatasetMetadata()));
144+
System.out.println(String.format("Dataset example count: %d", dataset.getExampleCount()));
145+
System.out.println("Dataset create time:");
146+
System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds()));
147+
System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos()));
148+
}
149+
}
150+
}
148151
// [END automl_language_list_datasets]
149152

150153
// [START automl_language_get_dataset]
@@ -159,27 +162,28 @@ public static void listDatasets(String projectId, String computeRegion, String f
159162
public static void getDataset(String projectId, String computeRegion, String datasetId)
160163
throws IOException {
161164
// Instantiates a client
162-
AutoMlClient client = AutoMlClient.create();
163-
164-
// Get the complete path of the dataset.
165-
DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
166-
167-
// Get all the information about a given dataset.
168-
Dataset dataset = client.getDataset(datasetFullId);
169-
170-
// Display the dataset information.
171-
System.out.println(String.format("Dataset name: %s", dataset.getName()));
172-
System.out.println(
173-
String.format(
174-
"Dataset id: %s",
175-
dataset.getName().split("/")[dataset.getName().split("/").length - 1]));
176-
System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName()));
177-
System.out.println("Text classification dataset metadata:");
178-
System.out.print(String.format("\t%s", dataset.getTextClassificationDatasetMetadata()));
179-
System.out.println(String.format("Dataset example count: %d", dataset.getExampleCount()));
180-
System.out.println("Dataset create time:");
181-
System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds()));
182-
System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos()));
165+
try (AutoMlClient client = AutoMlClient.create()) {
166+
167+
// Get the complete path of the dataset.
168+
DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
169+
170+
// Get all the information about a given dataset.
171+
Dataset dataset = client.getDataset(datasetFullId);
172+
173+
// Display the dataset information.
174+
System.out.println(String.format("Dataset name: %s", dataset.getName()));
175+
System.out.println(
176+
String.format(
177+
"Dataset id: %s",
178+
dataset.getName().split("/")[dataset.getName().split("/").length - 1]));
179+
System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName()));
180+
System.out.println("Text classification dataset metadata:");
181+
System.out.print(String.format("\t%s", dataset.getTextClassificationDatasetMetadata()));
182+
System.out.println(String.format("Dataset example count: %d", dataset.getExampleCount()));
183+
System.out.println("Dataset create time:");
184+
System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds()));
185+
System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos()));
186+
}
183187
}
184188
// [END automl_language_get_dataset]
185189

@@ -197,25 +201,22 @@ public static void getDataset(String projectId, String computeRegion, String dat
197201
public static void importData(
198202
String projectId, String computeRegion, String datasetId, String path) throws Exception {
199203
// Instantiates a client
200-
AutoMlClient client = AutoMlClient.create();
201-
202-
// Get the complete path of the dataset.
203-
DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
204+
try (AutoMlClient client = AutoMlClient.create()) {
204205

205-
GcsSource.Builder gcsSource = GcsSource.newBuilder();
206+
// Get the complete path of the dataset.
207+
DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
206208

207-
// Get multiple training data files to be imported
208-
String[] inputUris = path.split(",");
209-
for (String inputUri : inputUris) {
210-
gcsSource.addInputUris(inputUri);
211-
}
209+
// Get multiple training data files to be imported
210+
GcsSource gcsSource =
211+
GcsSource.newBuilder().addAllInputUris(Arrays.asList(path.split(","))).build();
212212

213-
// Import data from the input URI
214-
InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build();
215-
System.out.println("Processing import...");
213+
// Import data from the input URI
214+
InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build();
215+
System.out.println("Processing import...");
216216

217-
Empty response = client.importDataAsync(datasetFullId, inputConfig).get();
218-
System.out.println(String.format("Dataset imported. %s", response));
217+
Empty response = client.importDataAsync(datasetFullId, inputConfig).get();
218+
System.out.println(String.format("Dataset imported. %s", response));
219+
}
219220
}
220221
// [END automl_language_import_data]
221222

@@ -232,20 +233,23 @@ public static void importData(
232233
public static void exportData(
233234
String projectId, String computeRegion, String datasetId, String gcsUri) throws Exception {
234235
// Instantiates a client
235-
AutoMlClient client = AutoMlClient.create();
236+
try (AutoMlClient client = AutoMlClient.create()) {
236237

237-
// Get the complete path of the dataset.
238-
DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
238+
// Get the complete path of the dataset.
239+
DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
239240

240-
// Set the output URI.
241-
GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(gcsUri).build();
241+
// Set the output URI.
242+
GcsDestination gcsDestination =
243+
GcsDestination.newBuilder().setOutputUriPrefix(gcsUri).build();
242244

243-
// Export the data to the output URI.
244-
OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
245-
System.out.println(String.format("Processing export..."));
245+
// Export the data to the output URI.
246+
OutputConfig outputConfig =
247+
OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
248+
System.out.println(String.format("Processing export..."));
246249

247-
Empty response = client.exportDataAsync(datasetFullId, outputConfig).get();
248-
System.out.println(String.format("Dataset exported. %s", response));
250+
Empty response = client.exportDataAsync(datasetFullId, outputConfig).get();
251+
System.out.println(String.format("Dataset exported. %s", response));
252+
}
249253
}
250254
// [END automl_language_export_data]
251255

@@ -261,15 +265,16 @@ public static void exportData(
261265
public static void deleteDataset(String projectId, String computeRegion, String datasetId)
262266
throws Exception {
263267
// Instantiates a client
264-
AutoMlClient client = AutoMlClient.create();
268+
try (AutoMlClient client = AutoMlClient.create()) {
265269

266-
// Get the complete path of the dataset.
267-
DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
270+
// Get the complete path of the dataset.
271+
DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
268272

269-
// Delete a dataset.
270-
Empty response = client.deleteDatasetAsync(datasetFullId).get();
273+
// Delete a dataset.
274+
Empty response = client.deleteDatasetAsync(datasetFullId).get();
271275

272-
System.out.println(String.format("Dataset deleted. %s", response));
276+
System.out.println(String.format("Dataset deleted. %s", response));
277+
}
273278
}
274279
// [END automl_language_delete_dataset]
275280

0 commit comments

Comments
 (0)