2121import com .google .cloud .automl .v1beta1 .Dataset ;
2222import com .google .cloud .automl .v1beta1 .DatasetName ;
2323import com .google .cloud .automl .v1beta1 .GcsSource ;
24- import com .google .cloud .automl .v1beta1 .GcsSource .Builder ;
2524import com .google .cloud .automl .v1beta1 .InputConfig ;
2625import com .google .cloud .automl .v1beta1 .ListDatasetsRequest ;
2726import com .google .cloud .automl .v1beta1 .LocationName ;
3029
3130import java .io .IOException ;
3231import java .io .PrintStream ;
32+ import java .util .concurrent .ExecutionException ;
3333
3434import net .sourceforge .argparse4j .ArgumentParsers ;
3535import net .sourceforge .argparse4j .inf .ArgumentParser ;
@@ -54,53 +54,53 @@ public class DatasetApi {
5454 * @param datasetName the name of the dataset to be created.
5555 * @param source the Source language
5656 * @param target the Target language
57- * @throws IOException on Input/Output errors.
5857 */
5958 public static void createDataset (
6059 String projectId , String computeRegion , String datasetName , String source , String target )
6160 throws IOException {
6261 // 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+ }
104104 }
105105 // [END automl_translate_create_dataset]
106106
@@ -111,27 +111,69 @@ public static void createDataset(
111111 * @param projectId the Google Cloud Project ID.
112112 * @param computeRegion the Region name. (e.g., "us-central1").
113113 * @param filter the Filter expression.
114- * @throws Exception on AutoML Client errors
115114 */
116115 public static void listDatasets (String projectId , String computeRegion , String filter )
117116 throws IOException {
118117 // 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 ()) {
120168
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 );
123171
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 );
129174
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 ()) {
133175 // 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 ()));
135177 System .out .println (
136178 String .format (
137179 "Dataset id: %s" ,
@@ -151,48 +193,6 @@ public static void listDatasets(String projectId, String computeRegion, String f
151193 System .out .println (String .format ("\t nanos: %s" , dataset .getCreateTime ().getNanos ()));
152194 }
153195 }
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- }
196196 // [END automl_translate_get_dataset]
197197
198198 // [START automl_translate_import_data]
@@ -203,30 +203,31 @@ public static void getDataset(String projectId, String computeRegion, String dat
203203 * @param computeRegion the Region name. (e.g., "us-central1").
204204 * @param datasetId the Id of the dataset.
205205 * @param path the remote Path of the training data csv file.
206- * @throws Exception on AutoML Client errors
207206 */
208207 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 {
210210 // Instantiates a client
211- AutoMlClient client = AutoMlClient .create ();
211+ try ( AutoMlClient client = AutoMlClient .create ()) {
212212
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 );
215215
216- Builder gcsSource = GcsSource .newBuilder ();
216+ GcsSource . Builder gcsSource = GcsSource .newBuilder ();
217217
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+ }
223223
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..." );
227227
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+ }
230231 }
231232 // [END automl_translate_import_data]
232233
@@ -237,20 +238,20 @@ public static void importData(
237238 * @param projectId the Google Cloud Project ID.
238239 * @param computeRegion the Region name. (e.g., "us-central1").
239240 * @param datasetId the Id of the dataset.
240- * @throws Exception on AutoML Client errors
241241 */
242242 public static void deleteDataset (String projectId , String computeRegion , String datasetId )
243- throws Exception {
243+ throws IOException , InterruptedException , ExecutionException {
244244 // Instantiates a client
245- AutoMlClient client = AutoMlClient .create ();
245+ try ( AutoMlClient client = AutoMlClient .create ()) {
246246
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 );
249249
250- // Delete a dataset.
251- Empty response = client .deleteDatasetAsync (datasetFullId ).get ();
250+ // Delete a dataset.
251+ Empty response = client .deleteDatasetAsync (datasetFullId ).get ();
252252
253- System .out .println (String .format ("Dataset deleted. %s" , response ));
253+ System .out .println (String .format ("Dataset deleted. %s" , response ));
254+ }
254255 }
255256 // [END automl_translate_delete_dataset]
256257
@@ -284,7 +285,7 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
284285 String projectId = System .getenv ("PROJECT_ID" );
285286 String computeRegion = System .getenv ("REGION_NAME" );
286287
287- Namespace ns = null ;
288+ Namespace ns ;
288289 try {
289290 ns = parser .parseArgs (args );
290291 if (ns .get ("command" ).equals ("create_dataset" )) {
0 commit comments