Amazon Forecast is no longer available to new customers. Existing customers of
Amazon Forecast can continue to use the service as normal.
Learn more"
Use CreateForecast
with an AWS SDK
The following code example shows how to use CreateForecast
.
- Java
-
- SDK for Java 2.x
-
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 "";
}
}
For a complete list of AWS SDK developer guides and code examples, see
Using Forecast with an AWS SDK.
This topic also includes information about getting started and details about previous SDK versions.