1

我正在寻找Amazon Forecast API 的 JAVA 示例,以便可以将其集成到我的 JAVA 应用程序中。

我搜索并没有找到任何解决方案,即使我向 AWS 团队提出了支持票,他们也无法提供我作为屏幕截图附加的内容。在此处输入图像描述

文档适用于 python、NodeJS 和其他语言,但不适用于 JAVA。

在与AWS Forecast Java SDK集成方面,我已经做了很多努力。

更新

最后,我在下面的答案中发布了一些内容,但仍在寻找更好的选择。

4

1 回答 1

0

在花了几天时间寻找文档或工作示例后,我得到了这个解决方案。我可以通过使用此代码获得预测,但仍在寻找更好的方法(如果可能)。

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());
    }
}

请检查工作示例

于 2020-02-22T10:12:45.573 回答