|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.translate.automl; |
| 18 | + |
| 19 | +// [START automl_translate_create_dataset] |
| 20 | +import com.google.api.gax.longrunning.OperationFuture; |
| 21 | +import com.google.cloud.automl.v1.AutoMlClient; |
| 22 | +import com.google.cloud.automl.v1.Dataset; |
| 23 | +import com.google.cloud.automl.v1.LocationName; |
| 24 | +import com.google.cloud.automl.v1.OperationMetadata; |
| 25 | +import com.google.cloud.automl.v1.TranslationDatasetMetadata; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.concurrent.ExecutionException; |
| 29 | + |
| 30 | +class CreateDataset { |
| 31 | + |
| 32 | + // Create a dataset |
| 33 | + static void createDataset(String projectId, String displayName) { |
| 34 | + // String projectId = "YOUR_PROJECT_ID"; |
| 35 | + // String displayName = "YOUR_DATASET_NAME"; |
| 36 | + |
| 37 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 38 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 39 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 40 | + try (AutoMlClient client = AutoMlClient.create()) { |
| 41 | + // A resource that represents Google Cloud Platform location. |
| 42 | + LocationName projectLocation = LocationName.of(projectId, "us-central1"); |
| 43 | + |
| 44 | + // Specify the source and target language. |
| 45 | + TranslationDatasetMetadata translationDatasetMetadata = |
| 46 | + TranslationDatasetMetadata.newBuilder() |
| 47 | + .setSourceLanguageCode("en") |
| 48 | + .setTargetLanguageCode("ja") |
| 49 | + .build(); |
| 50 | + Dataset dataset = |
| 51 | + Dataset.newBuilder() |
| 52 | + .setDisplayName(displayName) |
| 53 | + .setTranslationDatasetMetadata(translationDatasetMetadata) |
| 54 | + .build(); |
| 55 | + OperationFuture<Dataset, OperationMetadata> future = |
| 56 | + client.createDatasetAsync(projectLocation, dataset); |
| 57 | + |
| 58 | + Dataset createdDataset = future.get(); |
| 59 | + |
| 60 | + // Display the dataset information. |
| 61 | + System.out.format("Dataset name: %s\n", createdDataset.getName()); |
| 62 | + // To get the dataset id, you have to parse it out of the `name` field. As dataset Ids are |
| 63 | + // required for other methods. |
| 64 | + // Name Form: `projects/{project_id}/locations/{location_id}/datasets/{dataset_id}` |
| 65 | + String[] names = createdDataset.getName().split("/"); |
| 66 | + String datasetId = names[names.length - 1]; |
| 67 | + System.out.format("Dataset id: %s\n", datasetId); |
| 68 | + System.out.format("Dataset display name: %s\n", createdDataset.getDisplayName()); |
| 69 | + System.out.println("Translation dataset Metadata:"); |
| 70 | + System.out.format( |
| 71 | + "\tSource language code: %s\n", |
| 72 | + createdDataset.getTranslationDatasetMetadata().getSourceLanguageCode()); |
| 73 | + System.out.format( |
| 74 | + "\tTarget language code: %s\n", |
| 75 | + createdDataset.getTranslationDatasetMetadata().getTargetLanguageCode()); |
| 76 | + System.out.println("Dataset create time:"); |
| 77 | + System.out.format("\tseconds: %s\n", createdDataset.getCreateTime().getSeconds()); |
| 78 | + System.out.format("\tnanos: %s\n", createdDataset.getCreateTime().getNanos()); |
| 79 | + } catch (IOException | InterruptedException | ExecutionException e) { |
| 80 | + e.printStackTrace(); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | +// [END automl_translate_create_dataset] |
0 commit comments