AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用SDK适用于 Java 2.x 的 Forecast 示例
以下代码示例向您展示了如何使用 with Forecast 来执行操作和实现常见场景。 AWS SDK for Java 2.x
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。
主题
操作
以下代码示例演示如何使用 CreateDataset
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 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; } }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 CreateDataset” 中的。
-
以下代码示例演示如何使用 CreateForecast
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 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 ""; } }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 CreateForecast” 中的。
-
以下代码示例演示如何使用 DeleteDataset
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 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); } } }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DeleteDataset” 中的。
-
以下代码示例演示如何使用 DeleteForecast
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 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); } } }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DeleteForecast” 中的。
-
以下代码示例演示如何使用 DescribeForecast
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 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); } } }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DescribeForecast” 中的。
-
以下代码示例演示如何使用 ListDatasetGroups
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 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); } } }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 ListDatasetGroups” 中的。
-
以下代码示例演示如何使用 ListForecasts
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 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); } } }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 ListForecasts” 中的。
-