Ejemplos de Forecast que se utilizan SDK para Java 2.x - AWS SDK for Java 2.x

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

Ejemplos de Forecast que se utilizan SDK para Java 2.x

Los siguientes ejemplos de código muestran cómo realizar acciones e implementar escenarios comunes mediante el uso de AWS SDK for Java 2.x with Forecast.

Las acciones son extractos de código de programas más grandes y deben ejecutarse en contexto. Mientras las acciones muestran cómo llamar a las funciones de servicio individuales, es posible ver las acciones en contexto en los escenarios relacionados.

Cada ejemplo incluye un enlace al código fuente completo, donde puede encontrar instrucciones sobre cómo configurar y ejecutar el código en su contexto.

Acciones

En el siguiente ejemplo de código se muestra cómo usarloCreateDataset.

SDKpara Java 2.x
nota

Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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; } }
  • Para API obtener más información, consulte CreateDatasetla AWS SDK for Java 2.x APIReferencia.

El siguiente ejemplo de código muestra cómo usarloCreateForecast.

SDKpara Java 2.x
nota

Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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 ""; } }
  • Para API obtener más información, consulte CreateForecastla AWS SDK for Java 2.x APIReferencia.

El siguiente ejemplo de código muestra cómo usarloDeleteDataset.

SDKpara Java 2.x
nota

Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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); } } }
  • Para API obtener más información, consulte DeleteDatasetla AWS SDK for Java 2.x APIReferencia.

El siguiente ejemplo de código muestra cómo usarloDeleteForecast.

SDKpara Java 2.x
nota

Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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); } } }
  • Para API obtener más información, consulte DeleteForecastla AWS SDK for Java 2.x APIReferencia.

El siguiente ejemplo de código muestra cómo usarloDescribeForecast.

SDKpara Java 2.x
nota

Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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); } } }
  • Para API obtener más información, consulte DescribeForecastla AWS SDK for Java 2.x APIReferencia.

El siguiente ejemplo de código muestra cómo usarloListDatasetGroups.

SDKpara Java 2.x
nota

Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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); } } }
  • Para API obtener más información, consulte ListDatasetGroupsla AWS SDK for Java 2.x APIReferencia.

El siguiente ejemplo de código muestra cómo usarloListForecasts.

SDKpara Java 2.x
nota

Hay más información. GitHub Busque el ejemplo completo y aprenda a configurar y ejecutar en el Repositorio de ejemplos de código de 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); } } }
  • Para API obtener más información, consulte ListForecastsla AWS SDK for Java 2.x APIReferencia.