32
32
33
33
import java .io .IOException ;
34
34
import java .io .PrintStream ;
35
+ import java .util .Arrays ;
35
36
36
37
import net .sourceforge .argparse4j .ArgumentParsers ;
37
38
import net .sourceforge .argparse4j .inf .ArgumentParser ;
@@ -62,76 +63,33 @@ public static void createDataset(
62
63
String projectId , String computeRegion , String datasetName , Boolean multiLabel )
63
64
throws IOException {
64
65
// 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 ("\t seconds: %s" , dataset .getCreateTime ().getSeconds ()));
102
- System .out .println (String .format ("\t nanos: %s" , dataset .getCreateTime ().getNanos ()));
103
- }
104
- // [END automl_language_create_dataset]
66
+ try (AutoMlClient client = AutoMlClient .create ()) {
105
67
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 );
119
70
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 ;
122
74
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 );
129
90
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
91
// Display the dataset information.
134
- System .out .println (String .format ("\n Dataset name: %s" , dataset .getName ()));
92
+ System .out .println (String .format ("Dataset name: %s" , dataset .getName ()));
135
93
System .out .println (
136
94
String .format (
137
95
"Dataset id: %s" ,
@@ -145,6 +103,51 @@ public static void listDatasets(String projectId, String computeRegion, String f
145
103
System .out .println (String .format ("\t nanos: %s" , dataset .getCreateTime ().getNanos ()));
146
104
}
147
105
}
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 ("\n Dataset 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 ("\t seconds: %s" , dataset .getCreateTime ().getSeconds ()));
147
+ System .out .println (String .format ("\t nanos: %s" , dataset .getCreateTime ().getNanos ()));
148
+ }
149
+ }
150
+ }
148
151
// [END automl_language_list_datasets]
149
152
150
153
// [START automl_language_get_dataset]
@@ -159,27 +162,28 @@ public static void listDatasets(String projectId, String computeRegion, String f
159
162
public static void getDataset (String projectId , String computeRegion , String datasetId )
160
163
throws IOException {
161
164
// 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 ("\t seconds: %s" , dataset .getCreateTime ().getSeconds ()));
182
- System .out .println (String .format ("\t nanos: %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 ("\t seconds: %s" , dataset .getCreateTime ().getSeconds ()));
185
+ System .out .println (String .format ("\t nanos: %s" , dataset .getCreateTime ().getNanos ()));
186
+ }
183
187
}
184
188
// [END automl_language_get_dataset]
185
189
@@ -197,25 +201,22 @@ public static void getDataset(String projectId, String computeRegion, String dat
197
201
public static void importData (
198
202
String projectId , String computeRegion , String datasetId , String path ) throws Exception {
199
203
// 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 ()) {
204
205
205
- GcsSource .Builder gcsSource = GcsSource .newBuilder ();
206
+ // Get the complete path of the dataset.
207
+ DatasetName datasetFullId = DatasetName .of (projectId , computeRegion , datasetId );
206
208
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 ();
212
212
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..." );
216
216
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
+ }
219
220
}
220
221
// [END automl_language_import_data]
221
222
@@ -232,20 +233,23 @@ public static void importData(
232
233
public static void exportData (
233
234
String projectId , String computeRegion , String datasetId , String gcsUri ) throws Exception {
234
235
// Instantiates a client
235
- AutoMlClient client = AutoMlClient .create ();
236
+ try ( AutoMlClient client = AutoMlClient .create ()) {
236
237
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 );
239
240
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 ();
242
244
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..." ));
246
249
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
+ }
249
253
}
250
254
// [END automl_language_export_data]
251
255
@@ -261,15 +265,16 @@ public static void exportData(
261
265
public static void deleteDataset (String projectId , String computeRegion , String datasetId )
262
266
throws Exception {
263
267
// Instantiates a client
264
- AutoMlClient client = AutoMlClient .create ();
268
+ try ( AutoMlClient client = AutoMlClient .create ()) {
265
269
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 );
268
272
269
- // Delete a dataset.
270
- Empty response = client .deleteDatasetAsync (datasetFullId ).get ();
273
+ // Delete a dataset.
274
+ Empty response = client .deleteDatasetAsync (datasetFullId ).get ();
271
275
272
- System .out .println (String .format ("Dataset deleted. %s" , response ));
276
+ System .out .println (String .format ("Dataset deleted. %s" , response ));
277
+ }
273
278
}
274
279
// [END automl_language_delete_dataset]
275
280
0 commit comments