|
| 1 | +/* |
| 2 | + * Copyright 2021 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.example.translatev3beta1; |
| 18 | + |
| 19 | +// [START translate_v3beta1_batch_translate_document] |
| 20 | + |
| 21 | +import com.google.api.gax.longrunning.OperationFuture; |
| 22 | +import com.google.api.gax.longrunning.OperationTimedPollAlgorithm; |
| 23 | +import com.google.api.gax.retrying.RetrySettings; |
| 24 | +import com.google.api.gax.retrying.TimedRetryAlgorithm; |
| 25 | +import com.google.cloud.translate.v3beta1.BatchDocumentInputConfig; |
| 26 | +import com.google.cloud.translate.v3beta1.BatchDocumentOutputConfig; |
| 27 | +import com.google.cloud.translate.v3beta1.BatchTranslateDocumentMetadata; |
| 28 | +import com.google.cloud.translate.v3beta1.BatchTranslateDocumentRequest; |
| 29 | +import com.google.cloud.translate.v3beta1.BatchTranslateDocumentResponse; |
| 30 | +import com.google.cloud.translate.v3beta1.GcsDestination; |
| 31 | +import com.google.cloud.translate.v3beta1.GcsSource; |
| 32 | +import com.google.cloud.translate.v3beta1.LocationName; |
| 33 | +import com.google.cloud.translate.v3beta1.TranslationServiceClient; |
| 34 | +import com.google.cloud.translate.v3beta1.TranslationServiceSettings; |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.concurrent.ExecutionException; |
| 37 | +import java.util.concurrent.ThreadLocalRandom; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | +import java.util.concurrent.TimeoutException; |
| 40 | +import org.threeten.bp.Duration; |
| 41 | + |
| 42 | +public class BatchTranslateDocument { |
| 43 | + |
| 44 | + public static void batchTranslateDocument() |
| 45 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 46 | + // TODO(developer): Replace these variables before running the sample. |
| 47 | + String projectId = "YOUR-PROJECT-ID"; |
| 48 | + // Supported Languages: https://cloud.google.com/translate/docs/languages |
| 49 | + String sourceLanguage = "your-source-language"; |
| 50 | + String targetLanguage = "your-target-language"; |
| 51 | + String inputUri = "gs://your-gcs-bucket/path/to/input/file.txt"; |
| 52 | + String outputUri = "gs://your-gcs-bucket/path/to/results/"; |
| 53 | + int timeout = 400; // timeout in seconds |
| 54 | + batchTranslateDocument(projectId, sourceLanguage, targetLanguage, inputUri, outputUri, timeout); |
| 55 | + } |
| 56 | + |
| 57 | + // Batch translate document |
| 58 | + public static void batchTranslateDocument( |
| 59 | + String projectId, |
| 60 | + String sourceLanguage, |
| 61 | + String targetLanguage, |
| 62 | + String inputUri, |
| 63 | + String outputUri, |
| 64 | + int timeout) |
| 65 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 66 | + // refer to https://github.com/googleapis/java-translate/issues/613 |
| 67 | + TranslationServiceSettings.Builder translationServiceSettingsBuilder = |
| 68 | + TranslationServiceSettings.newBuilder(); |
| 69 | + TimedRetryAlgorithm timedRetryAlgorithm = |
| 70 | + OperationTimedPollAlgorithm.create( |
| 71 | + RetrySettings.newBuilder().setTotalTimeout(Duration.ofSeconds(1000)).build()); |
| 72 | + translationServiceSettingsBuilder |
| 73 | + .batchTranslateDocumentOperationSettings() |
| 74 | + .setPollingAlgorithm(timedRetryAlgorithm); |
| 75 | + TranslationServiceSettings translationServiceSettings = |
| 76 | + translationServiceSettingsBuilder.build(); |
| 77 | + |
| 78 | + // Initialize client that sends requests. This client can be reused for multiple requests. After |
| 79 | + // completing all of your requests, call the "close" method on the client to clean |
| 80 | + // up any remaining background resources. |
| 81 | + try (TranslationServiceClient client = |
| 82 | + TranslationServiceClient.create(translationServiceSettings)) { |
| 83 | + // The ``global`` location is not supported for batch translation |
| 84 | + LocationName parent = LocationName.of(projectId, "us-central1"); |
| 85 | + |
| 86 | + // Google Cloud Storage location for the source input. This can be a single file |
| 87 | + // (for example, ``gs://translation-test/input.docx``) or a wildcard |
| 88 | + // (for example, ``gs://translation-test/*``). |
| 89 | + // Supported file types: https://cloud.google.com/translate/docs/supported-formats |
| 90 | + GcsSource gcsSource = GcsSource.newBuilder().setInputUri(inputUri).build(); |
| 91 | + |
| 92 | + BatchDocumentInputConfig batchDocumentInputConfig = |
| 93 | + BatchDocumentInputConfig.newBuilder().setGcsSource(gcsSource).build(); |
| 94 | + |
| 95 | + GcsDestination gcsDestination = |
| 96 | + GcsDestination.newBuilder().setOutputUriPrefix(outputUri).build(); |
| 97 | + |
| 98 | + BatchDocumentOutputConfig batchDocumentOutputConfig = |
| 99 | + BatchDocumentOutputConfig.newBuilder().setGcsDestination(gcsDestination).build(); |
| 100 | + |
| 101 | + BatchTranslateDocumentRequest request = |
| 102 | + BatchTranslateDocumentRequest.newBuilder() |
| 103 | + .setParent(parent.toString()) |
| 104 | + .setSourceLanguageCode(sourceLanguage) |
| 105 | + .addTargetLanguageCodes(targetLanguage) |
| 106 | + .addInputConfigs(batchDocumentInputConfig) |
| 107 | + .setOutputConfig(batchDocumentOutputConfig) |
| 108 | + .build(); |
| 109 | + |
| 110 | + OperationFuture<BatchTranslateDocumentResponse, BatchTranslateDocumentMetadata> future = |
| 111 | + client.batchTranslateDocumentAsync(request); |
| 112 | + |
| 113 | + System.out.println("Waiting for operation to complete..."); |
| 114 | + |
| 115 | + // random number between timeout |
| 116 | + long randomNumber = ThreadLocalRandom.current().nextInt(timeout, timeout + 100); |
| 117 | + BatchTranslateDocumentResponse response = future.get(randomNumber, TimeUnit.SECONDS); |
| 118 | + |
| 119 | + System.out.println("Total Pages: " + response.getTotalPages()); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
| 123 | +// [END translate_v3beta1_batch_translate_document] |
0 commit comments