2

I am looking for JAVA example of Amazon Forecast API so I can integrate this in my JAVA Application.

I searched and didn't found any solution, even I raised a support ticket with the AWS team and they are also unable to provide that which I am attaching as a screenshot. enter image description here

Documentations are available for python, NodeJS, and other languages but not for JAVA.

I have already struggled a lot in integration with AWS Forecast Java SDK.

UPDATE

Finally, I got something that I am posting in my below answer but still looking for some better option.

Mayur Agarwal
  • 869
  • 10
  • 28

1 Answers1

0

After spending a few days in search of documentation or working example, I got this solution for me. I am able to get the predictions by using this code but still looking for some better approach (if possible).

package com.mayur.awsforecastexample;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.forecastquery.AmazonForecastQueryClientBuilder;
import com.amazonaws.services.forecastquery.model.DataPoint;
import com.amazonaws.services.forecastquery.model.Forecast;
import com.amazonaws.services.forecastquery.model.QueryForecastRequest;
import com.amazonaws.services.forecastquery.model.QueryForecastResult;

public class ForecastTest {
    AmazonForecastQueryClientBuilder client = AmazonForecastQueryClientBuilder.standard();

    public QueryForecastResult queryForecast(QueryForecastRequest request) {

        client.setCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("ACCESS_KEY", "SECRET_KEY")));
        client.setRegion("REGION");

        return client.build().queryForecast(request);
    }

    public static void main(String ar[]) {
        Map<String, String> filters = new HashMap<String, String>();
        filters.put("item_id", "YOUR_ITEM_ID");

        QueryForecastRequest request = new QueryForecastRequest();
        request.setForecastArn("FORECAST_ARN");
        request.setFilters(filters);
        request.setStartDate(null);
        request.setEndDate(null);

        ForecastTest forecastTest = new ForecastTest();
        QueryForecastResult res = forecastTest.queryForecast(request);
        Forecast f = res.getForecast();

        Map<String, List<DataPoint>> predictions = f.getPredictions();

        for (Entry<String, List<DataPoint>> entry : predictions.entrySet())
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
    }
}

Please check the working Example

Mayur Agarwal
  • 869
  • 10
  • 28