1818
1919// Imports the Google Cloud client library
2020import com .google .cloud .automl .v1beta1 .AutoMlClient ;
21- import com .google .cloud .automl .v1beta1 .Dataset ;
2221import com .google .cloud .automl .v1beta1 .DatasetName ;
2322import com .google .cloud .automl .v1beta1 .GcsSource ;
2423import com .google .cloud .automl .v1beta1 .InputConfig ;
25- import com .google .cloud .automl .v1beta1 .ListDatasetsRequest ;
26- import com .google .cloud .automl .v1beta1 .LocationName ;
27- import com .google .cloud .automl .v1beta1 .TranslationDatasetMetadata ;
2824import com .google .protobuf .Empty ;
2925import java .io .IOException ;
3026import java .io .PrintStream ;
4339 */
4440public class DatasetApi {
4541
46- // [START automl_translate_create_dataset]
47- /**
48- * Demonstrates using the AutoML client to create a dataset
49- *
50- * @param projectId the Google Cloud Project ID.
51- * @param computeRegion the Region name. (e.g., "us-central1").
52- * @param datasetName the name of the dataset to be created.
53- * @param source the Source language
54- * @param target the Target language
55- */
56- public static void createDataset (
57- String projectId , String computeRegion , String datasetName , String source , String target )
58- throws IOException {
59- // Instantiates a client
60- try (AutoMlClient client = AutoMlClient .create ()) {
61-
62- // A resource that represents Google Cloud Platform location.
63- LocationName projectLocation = LocationName .of (projectId , computeRegion );
64-
65- // Specify the source and target language.
66- TranslationDatasetMetadata translationDatasetMetadata =
67- TranslationDatasetMetadata .newBuilder ()
68- .setSourceLanguageCode (source )
69- .setTargetLanguageCode (target )
70- .build ();
71-
72- // Set dataset name and dataset metadata.
73- Dataset myDataset =
74- Dataset .newBuilder ()
75- .setDisplayName (datasetName )
76- .setTranslationDatasetMetadata (translationDatasetMetadata )
77- .build ();
78-
79- // Create a dataset with the dataset metadata in the region.
80- Dataset dataset = client .createDataset (projectLocation , myDataset );
81-
82- // Display the dataset information.
83- System .out .println (String .format ("Dataset name: %s" , dataset .getName ()));
84- System .out .println (
85- String .format (
86- "Dataset id: %s" ,
87- dataset .getName ().split ("/" )[dataset .getName ().split ("/" ).length - 1 ]));
88- System .out .println (String .format ("Dataset display name: %s" , dataset .getDisplayName ()));
89- System .out .println ("Translation dataset Metadata:" );
90- System .out .println (
91- String .format (
92- "\t Source language code: %s" ,
93- dataset .getTranslationDatasetMetadata ().getSourceLanguageCode ()));
94- System .out .println (
95- String .format (
96- "\t Target language code: %s" ,
97- dataset .getTranslationDatasetMetadata ().getTargetLanguageCode ()));
98- System .out .println ("Dataset create time:" );
99- System .out .println (String .format ("\t seconds: %s" , dataset .getCreateTime ().getSeconds ()));
100- System .out .println (String .format ("\t nanos: %s" , dataset .getCreateTime ().getNanos ()));
101- }
102- }
103- // [END automl_translate_create_dataset]
104-
105- // [START automl_translate_list_datasets]
106- /**
107- * Demonstrates using the AutoML client to list all datasets.
108- *
109- * @param projectId the Google Cloud Project ID.
110- * @param computeRegion the Region name. (e.g., "us-central1").
111- * @param filter the Filter expression.
112- */
113- public static void listDatasets (String projectId , String computeRegion , String filter )
114- throws IOException {
115- // Instantiates a client
116- try (AutoMlClient client = AutoMlClient .create ()) {
117-
118- // A resource that represents Google Cloud Platform location.
119- LocationName projectLocation = LocationName .of (projectId , computeRegion );
120-
121- ListDatasetsRequest request =
122- ListDatasetsRequest .newBuilder ()
123- .setParent (projectLocation .toString ())
124- .setFilter (filter )
125- .build ();
126-
127- // List all the datasets available in the region by applying filter.
128- System .out .println ("List of datasets:" );
129- for (Dataset dataset : client .listDatasets (request ).iterateAll ()) {
130- // Display the dataset information
131- System .out .println (String .format ("\n Dataset name: %s" , dataset .getName ()));
132- System .out .println (
133- String .format (
134- "Dataset id: %s" ,
135- dataset .getName ().split ("/" )[dataset .getName ().split ("/" ).length - 1 ]));
136- System .out .println (String .format ("Dataset display name: %s" , dataset .getDisplayName ()));
137- System .out .println ("Translation dataset metadata:" );
138- System .out .println (
139- String .format (
140- "\t Source language code: %s" ,
141- dataset .getTranslationDatasetMetadata ().getSourceLanguageCode ()));
142- System .out .println (
143- String .format (
144- "\t Target language code: %s" ,
145- dataset .getTranslationDatasetMetadata ().getTargetLanguageCode ()));
146- System .out .println ("Dataset create time:" );
147- System .out .println (String .format ("\t seconds: %s" , dataset .getCreateTime ().getSeconds ()));
148- System .out .println (String .format ("\t nanos: %s" , dataset .getCreateTime ().getNanos ()));
149- }
150- }
151- }
152- // [END automl_translate_list_datasets]
153-
154- // [START automl_translate_get_dataset]
155- /**
156- * Demonstrates using the AutoML client to get a dataset by ID.
157- *
158- * @param projectId the Google Cloud Project ID.
159- * @param computeRegion the Region name. (e.g., "us-central1").
160- * @param datasetId the Id of the dataset.
161- */
162- public static void getDataset (String projectId , String computeRegion , String datasetId )
163- throws IOException {
164- // Instantiates a client
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 ("Translation dataset metadata:" );
181- System .out .println (
182- String .format (
183- "\t Source language code: %s" ,
184- dataset .getTranslationDatasetMetadata ().getSourceLanguageCode ()));
185- System .out .println (
186- String .format (
187- "\t Target language code: %s" ,
188- dataset .getTranslationDatasetMetadata ().getTargetLanguageCode ()));
189- System .out .println ("Dataset create time:" );
190- System .out .println (String .format ("\t seconds: %s" , dataset .getCreateTime ().getSeconds ()));
191- System .out .println (String .format ("\t nanos: %s" , dataset .getCreateTime ().getNanos ()));
192- }
193- }
194- // [END automl_translate_get_dataset]
195-
19642 // [START automl_translate_import_data]
19743 /**
19844 * Import sentence pairs to the dataset.
@@ -262,17 +108,6 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
262108 ArgumentParser parser = ArgumentParsers .newFor ("" ).build ();
263109 Subparsers subparsers = parser .addSubparsers ().dest ("command" );
264110
265- Subparser createDatasetParser = subparsers .addParser ("create_dataset" );
266- createDatasetParser .addArgument ("datasetName" );
267- createDatasetParser .addArgument ("source" );
268- createDatasetParser .addArgument ("target" );
269-
270- Subparser listDatasetParser = subparsers .addParser ("list_datasets" );
271- listDatasetParser .addArgument ("filter" ).nargs ("?" ).setDefault ("translation_dataset_metadata:*" );
272-
273- Subparser getDatasetParser = subparsers .addParser ("get_dataset" );
274- getDatasetParser .addArgument ("datasetId" );
275-
276111 Subparser importDataParser = subparsers .addParser ("import_data" );
277112 importDataParser .addArgument ("datasetId" );
278113 importDataParser .addArgument ("path" );
@@ -286,20 +121,6 @@ public static void argsHelper(String[] args, PrintStream out) throws Exception {
286121 Namespace ns ;
287122 try {
288123 ns = parser .parseArgs (args );
289- if (ns .get ("command" ).equals ("create_dataset" )) {
290- createDataset (
291- projectId ,
292- computeRegion ,
293- ns .getString ("datasetName" ),
294- ns .getString ("source" ),
295- ns .getString ("target" ));
296- }
297- if (ns .get ("command" ).equals ("list_datasets" )) {
298- listDatasets (projectId , computeRegion , ns .getString ("filter" ));
299- }
300- if (ns .get ("command" ).equals ("get_dataset" )) {
301- getDataset (projectId , computeRegion , ns .getString ("datasetId" ));
302- }
303124 if (ns .get ("command" ).equals ("import_data" )) {
304125 importData (projectId , computeRegion , ns .getString ("datasetId" ), ns .getString ("path" ));
305126 }
0 commit comments