21
21
import com .google .cloud .automl .v1beta1 .Dataset ;
22
22
import com .google .cloud .automl .v1beta1 .DatasetName ;
23
23
import com .google .cloud .automl .v1beta1 .GcsSource ;
24
- import com .google .cloud .automl .v1beta1 .GcsSource .Builder ;
25
24
import com .google .cloud .automl .v1beta1 .InputConfig ;
26
25
import com .google .cloud .automl .v1beta1 .ListDatasetsRequest ;
27
26
import com .google .cloud .automl .v1beta1 .LocationName ;
30
29
31
30
import java .io .IOException ;
32
31
import java .io .PrintStream ;
32
+ import java .util .concurrent .ExecutionException ;
33
33
34
34
import net .sourceforge .argparse4j .ArgumentParsers ;
35
35
import net .sourceforge .argparse4j .inf .ArgumentParser ;
@@ -54,53 +54,53 @@ public class DatasetApi {
54
54
* @param datasetName the name of the dataset to be created.
55
55
* @param source the Source language
56
56
* @param target the Target language
57
- * @throws IOException on Input/Output errors.
58
57
*/
59
58
public static void createDataset (
60
59
String projectId , String computeRegion , String datasetName , String source , String target )
61
60
throws IOException {
62
61
// Instantiates a client
63
- AutoMlClient client = AutoMlClient .create ();
64
-
65
- // A resource that represents Google Cloud Platform location.
66
- LocationName projectLocation = LocationName .of (projectId , computeRegion );
67
-
68
- // Specify the source and target language.
69
- TranslationDatasetMetadata translationDatasetMetadata =
70
- TranslationDatasetMetadata .newBuilder ()
71
- .setSourceLanguageCode (source )
72
- .setTargetLanguageCode (target )
73
- .build ();
74
-
75
- // Set dataset name and dataset metadata.
76
- Dataset myDataset =
77
- Dataset .newBuilder ()
78
- .setDisplayName (datasetName )
79
- .setTranslationDatasetMetadata (translationDatasetMetadata )
80
- .build ();
81
-
82
- // Create a dataset with the dataset metadata in the region.
83
- Dataset dataset = client .createDataset (projectLocation , myDataset );
84
-
85
- // Display the dataset information.
86
- System .out .println (String .format ("Dataset name: %s" , dataset .getName ()));
87
- System .out .println (
88
- String .format (
89
- "Dataset id: %s" ,
90
- dataset .getName ().split ("/" )[dataset .getName ().split ("/" ).length - 1 ]));
91
- System .out .println (String .format ("Dataset display name: %s" , dataset .getDisplayName ()));
92
- System .out .println ("Translation dataset Metadata:" );
93
- System .out .println (
94
- String .format (
95
- "\t Source language code: %s" ,
96
- dataset .getTranslationDatasetMetadata ().getSourceLanguageCode ()));
97
- System .out .println (
98
- String .format (
99
- "\t Target language code: %s" ,
100
- dataset .getTranslationDatasetMetadata ().getTargetLanguageCode ()));
101
- System .out .println ("Dataset create time:" );
102
- System .out .println (String .format ("\t seconds: %s" , dataset .getCreateTime ().getSeconds ()));
103
- System .out .println (String .format ("\t nanos: %s" , dataset .getCreateTime ().getNanos ()));
62
+ try (AutoMlClient client = AutoMlClient .create ()) {
63
+
64
+ // A resource that represents Google Cloud Platform location.
65
+ LocationName projectLocation = LocationName .of (projectId , computeRegion );
66
+
67
+ // Specify the source and target language.
68
+ TranslationDatasetMetadata translationDatasetMetadata =
69
+ TranslationDatasetMetadata .newBuilder ()
70
+ .setSourceLanguageCode (source )
71
+ .setTargetLanguageCode (target )
72
+ .build ();
73
+
74
+ // Set dataset name and dataset metadata.
75
+ Dataset myDataset =
76
+ Dataset .newBuilder ()
77
+ .setDisplayName (datasetName )
78
+ .setTranslationDatasetMetadata (translationDatasetMetadata )
79
+ .build ();
80
+
81
+ // Create a dataset with the dataset metadata in the region.
82
+ Dataset dataset = client .createDataset (projectLocation , myDataset );
83
+
84
+ // Display the dataset information.
85
+ System .out .println (String .format ("Dataset name: %s" , dataset .getName ()));
86
+ System .out .println (
87
+ String .format (
88
+ "Dataset id: %s" ,
89
+ dataset .getName ().split ("/" )[dataset .getName ().split ("/" ).length - 1 ]));
90
+ System .out .println (String .format ("Dataset display name: %s" , dataset .getDisplayName ()));
91
+ System .out .println ("Translation dataset Metadata:" );
92
+ System .out .println (
93
+ String .format (
94
+ "\t Source language code: %s" ,
95
+ dataset .getTranslationDatasetMetadata ().getSourceLanguageCode ()));
96
+ System .out .println (
97
+ String .format (
98
+ "\t Target language code: %s" ,
99
+ dataset .getTranslationDatasetMetadata ().getTargetLanguageCode ()));
100
+ System .out .println ("Dataset create time:" );
101
+ System .out .println (String .format ("\t seconds: %s" , dataset .getCreateTime ().getSeconds ()));
102
+ System .out .println (String .format ("\t nanos: %s" , dataset .getCreateTime ().getNanos ()));
103
+ }
104
104
}
105
105
// [END automl_translate_create_dataset]
106
106
@@ -111,27 +111,69 @@ public static void createDataset(
111
111
* @param projectId the Google Cloud Project ID.
112
112
* @param computeRegion the Region name. (e.g., "us-central1").
113
113
* @param filter the Filter expression.
114
- * @throws Exception on AutoML Client errors
115
114
*/
116
115
public static void listDatasets (String projectId , String computeRegion , String filter )
117
116
throws IOException {
118
117
// Instantiates a client
119
- AutoMlClient client = AutoMlClient .create ();
118
+ try (AutoMlClient client = AutoMlClient .create ()) {
119
+
120
+ // A resource that represents Google Cloud Platform location.
121
+ LocationName projectLocation = LocationName .of (projectId , computeRegion );
122
+
123
+ ListDatasetsRequest request =
124
+ ListDatasetsRequest .newBuilder ()
125
+ .setParent (projectLocation .toString ())
126
+ .setFilter (filter )
127
+ .build ();
128
+
129
+ // List all the datasets available in the region by applying filter.
130
+ System .out .println ("List of datasets:" );
131
+ for (Dataset dataset : client .listDatasets (request ).iterateAll ()) {
132
+ // Display the dataset information
133
+ System .out .println (String .format ("\n Dataset name: %s" , dataset .getName ()));
134
+ System .out .println (
135
+ String .format (
136
+ "Dataset id: %s" ,
137
+ dataset .getName ().split ("/" )[dataset .getName ().split ("/" ).length - 1 ]));
138
+ System .out .println (String .format ("Dataset display name: %s" , dataset .getDisplayName ()));
139
+ System .out .println ("Translation dataset metadata:" );
140
+ System .out .println (
141
+ String .format (
142
+ "\t Source language code: %s" ,
143
+ dataset .getTranslationDatasetMetadata ().getSourceLanguageCode ()));
144
+ System .out .println (
145
+ String .format (
146
+ "\t Target language code: %s" ,
147
+ dataset .getTranslationDatasetMetadata ().getTargetLanguageCode ()));
148
+ System .out .println ("Dataset create time:" );
149
+ System .out .println (String .format ("\t seconds: %s" , dataset .getCreateTime ().getSeconds ()));
150
+ System .out .println (String .format ("\t nanos: %s" , dataset .getCreateTime ().getNanos ()));
151
+ }
152
+ }
153
+ }
154
+ // [END automl_translate_list_datasets]
155
+
156
+ // [START automl_translate_get_dataset]
157
+ /**
158
+ * Demonstrates using the AutoML client to get a dataset by ID.
159
+ *
160
+ * @param projectId the Google Cloud Project ID.
161
+ * @param computeRegion the Region name. (e.g., "us-central1").
162
+ * @param datasetId the Id of the dataset.
163
+ */
164
+ public static void getDataset (String projectId , String computeRegion , String datasetId )
165
+ throws IOException {
166
+ // Instantiates a client
167
+ try (AutoMlClient client = AutoMlClient .create ()) {
120
168
121
- // A resource that represents Google Cloud Platform location .
122
- LocationName projectLocation = LocationName .of (projectId , computeRegion );
169
+ // Get the complete path of the dataset .
170
+ DatasetName datasetFullId = DatasetName .of (projectId , computeRegion , datasetId );
123
171
124
- ListDatasetsRequest request =
125
- ListDatasetsRequest .newBuilder ()
126
- .setParent (projectLocation .toString ())
127
- .setFilter (filter )
128
- .build ();
172
+ // Get all the information about a given dataset.
173
+ Dataset dataset = client .getDataset (datasetFullId );
129
174
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 ()) {
133
175
// Display the dataset information
134
- System .out .println (String .format ("\n Dataset name: %s" , dataset .getName ()));
176
+ System .out .println (String .format ("Dataset name: %s" , dataset .getName ()));
135
177
System .out .println (
136
178
String .format (
137
179
"Dataset id: %s" ,
@@ -151,48 +193,6 @@ public static void listDatasets(String projectId, String computeRegion, String f
151
193
System .out .println (String .format ("\t nanos: %s" , dataset .getCreateTime ().getNanos ()));
152
194
}
153
195
}
154
- // [END automl_translate_list_datasets]
155
-
156
- // [START automl_translate_get_dataset]
157
- /**
158
- * Demonstrates using the AutoML client to get a dataset by ID.
159
- *
160
- * @param projectId the Google Cloud Project ID.
161
- * @param computeRegion the Region name. (e.g., "us-central1").
162
- * @param datasetId the Id of the dataset.
163
- * @throws Exception on AutoML Client errors
164
- */
165
- public static void getDataset (String projectId , String computeRegion , String datasetId )
166
- throws Exception {
167
- // Instantiates a client
168
- AutoMlClient client = AutoMlClient .create ();
169
-
170
- // Get the complete path of the dataset.
171
- DatasetName datasetFullId = DatasetName .of (projectId , computeRegion , datasetId );
172
-
173
- // Get all the information about a given dataset.
174
- Dataset dataset = client .getDataset (datasetFullId );
175
-
176
- // Display the dataset information
177
- System .out .println (String .format ("Dataset name: %s" , dataset .getName ()));
178
- System .out .println (
179
- String .format (
180
- "Dataset id: %s" ,
181
- dataset .getName ().split ("/" )[dataset .getName ().split ("/" ).length - 1 ]));
182
- System .out .println (String .format ("Dataset display name: %s" , dataset .getDisplayName ()));
183
- System .out .println ("Translation dataset metadata:" );
184
- System .out .println (
185
- String .format (
186
- "\t Source language code: %s" ,
187
- dataset .getTranslationDatasetMetadata ().getSourceLanguageCode ()));
188
- System .out .println (
189
- String .format (
190
- "\t Target language code: %s" ,
191
- dataset .getTranslationDatasetMetadata ().getTargetLanguageCode ()));
192
- System .out .println ("Dataset create time:" );
193
- System .out .println (String .format ("\t seconds: %s" , dataset .getCreateTime ().getSeconds ()));
194
- System .out .println (String .format ("\t nanos: %s" , dataset .getCreateTime ().getNanos ()));
195
- }
196
196
// [END automl_translate_get_dataset]
197
197
198
198
// [START automl_translate_import_data]
@@ -203,30 +203,31 @@ public static void getDataset(String projectId, String computeRegion, String dat
203
203
* @param computeRegion the Region name. (e.g., "us-central1").
204
204
* @param datasetId the Id of the dataset.
205
205
* @param path the remote Path of the training data csv file.
206
- * @throws Exception on AutoML Client errors
207
206
*/
208
207
public static void importData (
209
- String projectId , String computeRegion , String datasetId , String path ) throws Exception {
208
+ String projectId , String computeRegion , String datasetId , String path )
209
+ throws IOException , InterruptedException , ExecutionException {
210
210
// Instantiates a client
211
- AutoMlClient client = AutoMlClient .create ();
211
+ try ( AutoMlClient client = AutoMlClient .create ()) {
212
212
213
- // Get the complete path of the dataset.
214
- DatasetName datasetFullId = DatasetName .of (projectId , computeRegion , datasetId );
213
+ // Get the complete path of the dataset.
214
+ DatasetName datasetFullId = DatasetName .of (projectId , computeRegion , datasetId );
215
215
216
- Builder gcsSource = GcsSource .newBuilder ();
216
+ GcsSource . Builder gcsSource = GcsSource .newBuilder ();
217
217
218
- // Get multiple Google Cloud Storage URIs to import data from
219
- String [] inputUris = path .split ("," );
220
- for (String inputUri : inputUris ) {
221
- gcsSource .addInputUris (inputUri );
222
- }
218
+ // Get multiple Google Cloud Storage URIs to import data from
219
+ String [] inputUris = path .split ("," );
220
+ for (String inputUri : inputUris ) {
221
+ gcsSource .addInputUris (inputUri );
222
+ }
223
223
224
- // Import data from the input URI
225
- InputConfig inputConfig = InputConfig .newBuilder ().setGcsSource (gcsSource ).build ();
226
- System .out .println ("Processing import..." );
224
+ // Import data from the input URI
225
+ InputConfig inputConfig = InputConfig .newBuilder ().setGcsSource (gcsSource ).build ();
226
+ System .out .println ("Processing import..." );
227
227
228
- Empty response = client .importDataAsync (datasetFullId , inputConfig ).get ();
229
- System .out .println (String .format ("Dataset imported. %s" , response ));
228
+ Empty response = client .importDataAsync (datasetFullId , inputConfig ).get ();
229
+ System .out .println (String .format ("Dataset imported. %s" , response ));
230
+ }
230
231
}
231
232
// [END automl_translate_import_data]
232
233
@@ -237,20 +238,20 @@ public static void importData(
237
238
* @param projectId the Google Cloud Project ID.
238
239
* @param computeRegion the Region name. (e.g., "us-central1").
239
240
* @param datasetId the Id of the dataset.
240
- * @throws Exception on AutoML Client errors
241
241
*/
242
242
public static void deleteDataset (String projectId , String computeRegion , String datasetId )
243
- throws Exception {
243
+ throws IOException , InterruptedException , ExecutionException {
244
244
// Instantiates a client
245
- AutoMlClient client = AutoMlClient .create ();
245
+ try ( AutoMlClient client = AutoMlClient .create ()) {
246
246
247
- // Get the full path of the dataset.
248
- DatasetName datasetFullId = DatasetName .of (projectId , computeRegion , datasetId );
247
+ // Get the full path of the dataset.
248
+ DatasetName datasetFullId = DatasetName .of (projectId , computeRegion , datasetId );
249
249
250
- // Delete a dataset.
251
- Empty response = client .deleteDatasetAsync (datasetFullId ).get ();
250
+ // Delete a dataset.
251
+ Empty response = client .deleteDatasetAsync (datasetFullId ).get ();
252
252
253
- System .out .println (String .format ("Dataset deleted. %s" , response ));
253
+ System .out .println (String .format ("Dataset deleted. %s" , response ));
254
+ }
254
255
}
255
256
// [END automl_translate_delete_dataset]
256
257
@@ -284,7 +285,7 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
284
285
String projectId = System .getenv ("PROJECT_ID" );
285
286
String computeRegion = System .getenv ("REGION_NAME" );
286
287
287
- Namespace ns = null ;
288
+ Namespace ns ;
288
289
try {
289
290
ns = parser .parseArgs (args );
290
291
if (ns .get ("command" ).equals ("create_dataset" )) {
0 commit comments