Amazon ML应用程序中的AmazonClientException

时间:2017-06-15 22:20:00

标签: java amazon-web-services amazon-machine-learning

我指的是以下资源创建一个Application,用于使用已经使用Amazon ML训练的模型进行实时预测。

targeted-marketing-java

我已将该值替换为模型的名称,并且我已在AWS ML Console中创建了一个端点。但是当我执行main函数时,我得到以下异常:

Exception in thread "main" com.amazonaws.AmazonClientException: Unable to load AWS credentials from any provider in the chain
    at com.amazonaws.auth.AWSCredentialsProviderChain.getCredentials(AWSCredentialsProviderChain.java:117)
    at com.amazonaws.services.machinelearning.AmazonMachineLearningClient.invoke(AmazonMachineLearningClient.java:1983)
    at com.amazonaws.services.machinelearning.AmazonMachineLearningClient.getMLModel(AmazonMachineLearningClient.java:1585)
    at ML.RealtimePredict.lookupEndpoint(RealtimePredict.java:58)
    at ML.RealtimePredict.predict(RealtimePredict.java:45)
    at ML.RealtimePredict.main(RealtimePredict.java:21)

这是我正在使用的程序(Java):

    package ML;

import java.util.HashMap;
import java.util.Map;

import com.amazonaws.services.machinelearning.AmazonMachineLearningClient;
import com.amazonaws.services.machinelearning.model.*;

public class RealtimePredict {
    public static void main(String[] args) {
        String mlModelId = "ModelID--I provided this";

        RealtimePredict rtp = new RealtimePredict(mlModelId);

        // Example meta-data and data
        String metaData = "X1,X2";

        String data = "20,25";
        Map<String, String> record = rtp.parseArgs(
                metaData.trim().split(","), data.trim().split(","));
        PredictResult response = rtp.predict(record);
        System.out.println(response);
    }

    private AmazonMachineLearningClient client;
    private String mlModelId;
    private String predictEndpoint;

    public RealtimePredict(String mlModelId) {
        client = new AmazonMachineLearningClient();
        this.mlModelId = mlModelId;
    }

    private Map<String, String> parseArgs(String[] metaData, String[] data) {
        Map<String, String> record = new HashMap<>();

        for (int i = 0; i < (
                metaData.length < data.length ? metaData.length : data.length); i++) {
            record.put(metaData[i].trim(), data[i].trim());
        }
        return record;
    }

    private PredictResult predict(Map<String, String> record) {
        lookupEndpoint();
        PredictRequest request = new PredictRequest()
                .withMLModelId(mlModelId)
                .withPredictEndpoint(predictEndpoint)
                .withRecord(record);
        return client.predict(request);
    }

    /**
     * finds the realtime endpoint for this ML Model
     */
    private void lookupEndpoint() {
        GetMLModelRequest request = new GetMLModelRequest().withMLModelId(mlModelId);
        GetMLModelResult model = client.getMLModel(request);
        predictEndpoint = model.getEndpointInfo().getEndpointUrl();
    }
}

有人可以帮我解决这个问题。我估计我需要提供访问密钥和安全密钥。但是我无法帮助我如何做到这一点。感谢。

1 个答案:

答案 0 :(得分:0)

Java代码必须具有能够调用AWS服务的凭据。它正在几个位置搜索这些凭据,直到它放弃并打印出上述异常。

请阅读此文档,了解设置这些凭据的不同位置:http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html

最佳做法是在本地开发中使用默认凭据配置文件,并在云中运行实例/ container / Lambda时使用IAM角色。