0

I am referring to the following resource to create an Application for doing real time predictions using model I already trained using Amazon ML.

targeted-marketing-java

I had replaced the value with the Name of the model and I have already created an endpoint in my AWS ML Console. But when I execute the main function, I am getting the following exception:

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)

This is the program I am using (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();
    }
}

Can someone please help me fix this. I reckon that I need to provide the access key and security key. But I couldn't find much help on how I can do that. Thanks.

New Coder
  • 499
  • 4
  • 22

1 Answers1

0

The Java code must have credentials to be able to call AWS services. It is searching for these credentials in a few locations until it gives up and print put the above exception.

Please read this documentation about the different locations to set up these credentials: http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html

The best practice is to use the default credential profiles file in the local development, and IAM roles for the instance/container/Lambda when running it in the cloud.

Guy
  • 12,388
  • 3
  • 45
  • 67