使用 SDK 获取批量翻译作业详细信息

fetch batch translate job details using SDK

我试图使用 JAVA SDK 查找与给定 AWS 翻译作业关联的语言对和操作。

使用 AWS Web 控制台,我创建了几个批处理作业来将一些英语句子翻译成法语。在 CloudWatch 中,我可以看到指标维度为

语言对:en-fr 操作:TranslateText

我可以为给定的工作检索相同的信息(语言对和操作)吗? 使用 TranslateAsyncClient.describeTextTranslationJob(...) 方法 ?

您可以使用 TranslateClient 来描述翻译作业,并将作业编号作为输入。这使用 TranslateClient;但是您也可以使用异步版本。

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.translate.TranslateClient;
import software.amazon.awssdk.services.translate.model.DescribeTextTranslationJobRequest;
import software.amazon.awssdk.services.translate.model.DescribeTextTranslationJobResponse;
import software.amazon.awssdk.services.translate.model.TranslateException;
// snippet-end:[translate.java2._describe_jobs.import]

/**
 * To run this Java V2 code example, ensure that you have setup your development environment, including your credentials.
 *
 * For information, see this documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class DescribeTextTranslationJob {

    public static void main(String[] args) {


        final String USAGE = "\n" +
                "Usage:\n" +
                "    DescribeTextTranslationJob <id> \n\n" +
                "Where:\n" +
                "    id - a translation job ID value. You can obtain this value from the BatchTranslation example.\n";

        if (args.length != 1) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String id = args[0];
        Region region = Region.US_WEST_2;
        TranslateClient translateClient = TranslateClient.builder()
                .region(region)
                .build();

        describeTextTranslationJob(translateClient, id);
        translateClient.close();
    }

    // snippet-start:[translate.java2._describe_jobs.main]
    public static void describeTextTranslationJob(TranslateClient translateClient, String id) {

      try {

        DescribeTextTranslationJobRequest textTranslationJobRequest = DescribeTextTranslationJobRequest.builder()
                .jobId(id)
                 .build();

        DescribeTextTranslationJobResponse jobResponse = translateClient.describeTextTranslationJob(textTranslationJobRequest);
        System.out.println("The job status is "+jobResponse.textTranslationJobProperties().jobStatus());
        System.out.println("The source language is "+jobResponse.textTranslationJobProperties().sourceLanguageCode());
        System.out.println("The target language is "+jobResponse.textTranslationJobProperties().targetLanguageCodes());
    
      } catch (TranslateException e) {
          System.err.println(e.getMessage());
          System.exit(1);
      }
        // snippet-end:[translate.java2._describe_jobs.main]
   }
}

要查看使用此代码可以取回的所有数据,请参阅此 JavaDoc - https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/translate/model/TextTranslationJobProperties.html