Exemples de prévisions utilisées SDK pour Java 2.x - AWS SDK for Java 2.x

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Exemples de prévisions utilisées SDK pour Java 2.x

Les exemples de code suivants vous montrent comment effectuer des actions et implémenter des scénarios courants à l'aide de AWS SDK for Java 2.x with Forecast.

Les actions sont des extraits de code de programmes plus larges et doivent être exécutées dans leur contexte. Les actions vous indiquent comment appeler des fonctions de service individuelles, mais vous pouvez les visualiser dans leur contexte dans les scénarios correspondants.

Chaque exemple inclut un lien vers le code source complet, où vous trouverez des instructions sur la façon de configurer et d'exécuter le code en contexte.

Rubriques

Actions

L'exemple de code suivant montre comment utiliserCreateDataset.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.forecast.ForecastClient; import software.amazon.awssdk.services.forecast.model.CreateDatasetRequest; import software.amazon.awssdk.services.forecast.model.Schema; import software.amazon.awssdk.services.forecast.model.SchemaAttribute; import software.amazon.awssdk.services.forecast.model.CreateDatasetResponse; import software.amazon.awssdk.services.forecast.model.ForecastException; import java.util.ArrayList; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateDataSet { public static void main(String[] args) { final String usage = """ Usage: <name>\s Where: name - The name of the data set.\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String name = args[0]; Region region = Region.US_WEST_2; ForecastClient forecast = ForecastClient.builder() .region(region) .build(); String myDataSetARN = createForecastDataSet(forecast, name); System.out.println("The ARN of the new data set is " + myDataSetARN); forecast.close(); } public static String createForecastDataSet(ForecastClient forecast, String name) { try { Schema schema = Schema.builder() .attributes(getSchema()) .build(); CreateDatasetRequest datasetRequest = CreateDatasetRequest.builder() .datasetName(name) .domain("CUSTOM") .datasetType("RELATED_TIME_SERIES") .dataFrequency("D") .schema(schema) .build(); CreateDatasetResponse response = forecast.createDataset(datasetRequest); return response.datasetArn(); } catch (ForecastException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } // Create a SchemaAttribute list required to create a data set. private static List<SchemaAttribute> getSchema() { List<SchemaAttribute> schemaList = new ArrayList<>(); SchemaAttribute att1 = SchemaAttribute.builder() .attributeName("item_id") .attributeType("string") .build(); SchemaAttribute att2 = SchemaAttribute.builder() .attributeName("timestamp") .attributeType("timestamp") .build(); SchemaAttribute att3 = SchemaAttribute.builder() .attributeName("target_value") .attributeType("float") .build(); // Push the SchemaAttribute objects to the List. schemaList.add(att1); schemaList.add(att2); schemaList.add(att3); return schemaList; } }
  • Pour API plus de détails, voir CreateDatasetla section AWS SDK for Java 2.x APIRéférence.

L'exemple de code suivant montre comment utiliserCreateForecast.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.forecast.ForecastClient; import software.amazon.awssdk.services.forecast.model.CreateForecastRequest; import software.amazon.awssdk.services.forecast.model.CreateForecastResponse; import software.amazon.awssdk.services.forecast.model.ForecastException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateForecast { public static void main(String[] args) { final String usage = """ Usage: <name> <predictorArn>\s Where: name - The name of the forecast.\s predictorArn - The arn of the predictor to use.\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String name = args[0]; String predictorArn = args[1]; Region region = Region.US_WEST_2; ForecastClient forecast = ForecastClient.builder() .region(region) .build(); String forecastArn = createNewForecast(forecast, name, predictorArn); System.out.println("The ARN of the new forecast is " + forecastArn); forecast.close(); } public static String createNewForecast(ForecastClient forecast, String name, String predictorArn) { try { CreateForecastRequest forecastRequest = CreateForecastRequest.builder() .forecastName(name) .predictorArn(predictorArn) .build(); CreateForecastResponse response = forecast.createForecast(forecastRequest); return response.forecastArn(); } catch (ForecastException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } }
  • Pour API plus de détails, voir CreateForecastla section AWS SDK for Java 2.x APIRéférence.

L'exemple de code suivant montre comment utiliserDeleteDataset.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.forecast.ForecastClient; import software.amazon.awssdk.services.forecast.model.DeleteDatasetRequest; import software.amazon.awssdk.services.forecast.model.ForecastException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DeleteDataset { public static void main(String[] args) { final String usage = """ Usage: <datasetARN>\s Where: datasetARN - The ARN of the data set to delete.\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String datasetARN = args[0]; Region region = Region.US_WEST_2; ForecastClient forecast = ForecastClient.builder() .region(region) .build(); deleteForecastDataSet(forecast, datasetARN); forecast.close(); } public static void deleteForecastDataSet(ForecastClient forecast, String myDataSetARN) { try { DeleteDatasetRequest deleteRequest = DeleteDatasetRequest.builder() .datasetArn(myDataSetARN) .build(); forecast.deleteDataset(deleteRequest); System.out.println("The Data Set was deleted"); } catch (ForecastException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • Pour API plus de détails, voir DeleteDatasetla section AWS SDK for Java 2.x APIRéférence.

L'exemple de code suivant montre comment utiliserDeleteForecast.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.forecast.ForecastClient; import software.amazon.awssdk.services.forecast.model.DeleteDatasetRequest; import software.amazon.awssdk.services.forecast.model.ForecastException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DeleteDataset { public static void main(String[] args) { final String usage = """ Usage: <datasetARN>\s Where: datasetARN - The ARN of the data set to delete.\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String datasetARN = args[0]; Region region = Region.US_WEST_2; ForecastClient forecast = ForecastClient.builder() .region(region) .build(); deleteForecastDataSet(forecast, datasetARN); forecast.close(); } public static void deleteForecastDataSet(ForecastClient forecast, String myDataSetARN) { try { DeleteDatasetRequest deleteRequest = DeleteDatasetRequest.builder() .datasetArn(myDataSetARN) .build(); forecast.deleteDataset(deleteRequest); System.out.println("The Data Set was deleted"); } catch (ForecastException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • Pour API plus de détails, voir DeleteForecastla section AWS SDK for Java 2.x APIRéférence.

L'exemple de code suivant montre comment utiliserDescribeForecast.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.forecast.ForecastClient; import software.amazon.awssdk.services.forecast.model.DescribeForecastRequest; import software.amazon.awssdk.services.forecast.model.DescribeForecastResponse; import software.amazon.awssdk.services.forecast.model.ForecastException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DescribeForecast { public static void main(String[] args) { final String usage = """ Usage: <forecastarn>\s Where: forecastarn - The arn of the forecast (for example, "arn:aws:forecast:us-west-2:xxxxx322:forecast/my_forecast) """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String forecastarn = args[0]; Region region = Region.US_WEST_2; ForecastClient forecast = ForecastClient.builder() .region(region) .build(); describe(forecast, forecastarn); forecast.close(); } public static void describe(ForecastClient forecast, String forecastarn) { try { DescribeForecastRequest request = DescribeForecastRequest.builder() .forecastArn(forecastarn) .build(); DescribeForecastResponse response = forecast.describeForecast(request); System.out.println("The name of the forecast is " + response.forecastName()); } catch (ForecastException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • Pour API plus de détails, voir DescribeForecastla section AWS SDK for Java 2.x APIRéférence.

L'exemple de code suivant montre comment utiliserListDatasetGroups.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.forecast.ForecastClient; import software.amazon.awssdk.services.forecast.model.DatasetGroupSummary; import software.amazon.awssdk.services.forecast.model.ListDatasetGroupsRequest; import software.amazon.awssdk.services.forecast.model.ListDatasetGroupsResponse; import software.amazon.awssdk.services.forecast.model.ForecastException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListDataSetGroups { public static void main(String[] args) { Region region = Region.US_WEST_2; ForecastClient forecast = ForecastClient.builder() .region(region) .build(); listDataGroups(forecast); forecast.close(); } public static void listDataGroups(ForecastClient forecast) { try { ListDatasetGroupsRequest group = ListDatasetGroupsRequest.builder() .maxResults(10) .build(); ListDatasetGroupsResponse response = forecast.listDatasetGroups(group); List<DatasetGroupSummary> groups = response.datasetGroups(); for (DatasetGroupSummary myGroup : groups) { System.out.println("The Data Set name is " + myGroup.datasetGroupName()); } } catch (ForecastException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • Pour API plus de détails, voir ListDatasetGroupsla section AWS SDK for Java 2.x APIRéférence.

L'exemple de code suivant montre comment utiliserListForecasts.

SDKpour Java 2.x
Note

Il y en a plus à ce sujet GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.forecast.ForecastClient; import software.amazon.awssdk.services.forecast.model.ListForecastsResponse; import software.amazon.awssdk.services.forecast.model.ListForecastsRequest; import software.amazon.awssdk.services.forecast.model.ForecastSummary; import software.amazon.awssdk.services.forecast.model.ForecastException; import java.util.List; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListForecasts { public static void main(String[] args) { Region region = Region.US_WEST_2; ForecastClient forecast = ForecastClient.builder() .region(region) .build(); listAllForeCasts(forecast); forecast.close(); } public static void listAllForeCasts(ForecastClient forecast) { try { ListForecastsRequest request = ListForecastsRequest.builder() .maxResults(10) .build(); ListForecastsResponse response = forecast.listForecasts(request); List<ForecastSummary> forecasts = response.forecasts(); for (ForecastSummary forecastSummary : forecasts) { System.out.println("The name of the forecast is " + forecastSummary.forecastName()); } } catch (ForecastException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • Pour API plus de détails, voir ListForecastsla section AWS SDK for Java 2.x APIRéférence.