Search code examples
javaamazon-web-servicesamazon-kinesis-firehose

Getting Error: " the constructor AmazonKinesisFirehoseClient(AWSCredentials) is deprecated"


I am trying to put some data to my AWS Firehose Stream, but am having some difficulties. I am also getting an error that my stream name could not be found, even though it definitely exists under that name. I also tried another active stream, and I got the same problem. I'm not sure if this error has anything to do with the error that I'm getting with the constructor being deprecated.

Firehose project-stream not found under account xxxxxxxxxxx. (Service: AmazonKinesisFirehose; Status Code: 400; Error Code: ResourceNotFoundException; Request ID: xxxxxxxxxxxxxxxxxxxxxx

Getting the following error when I try to create "firehoseClient," after my first try-catch:

the constructor AmazonKinesisFirehoseClient(AWSCredentials) is deprecated

I tried to look through AWS' API, but could not find a constructor that worked.

package com.amazonaws.samples;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.Collection;

import com.amazonaws.*;
import com.amazonaws.AmazonClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.kinesisfirehose.model.PutRecordRequest;
import com.amazonaws.services.kinesisfirehose.AmazonKinesisFirehoseClient;
import com.amazonaws.services.kinesisfirehose.model.Record;
import com.amazonaws.services.kinesisfirehose.AmazonKinesisFirehoseClient;
import com.amazonaws.services.kinesisfirehose.*;

public class FirehoseExample {

    public static void main(String[] args) {
        AWSCredentials credentials = null;

        try {
            credentials = new ProfileCredentialsProvider().getCredentials();
        }

        catch (Exception e) {
            throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                    + "Please make sure that your credentials file is at the correct "
                    + "location (/Users/elybenari/.aws/credentials), and is in valid format.", e);
        }

        AmazonKinesisFirehoseClient firehoseClient = new AmazonKinesisFirehoseClient(credentials);
        PutRecordRequest request = new PutRecordRequest();

        request.setDeliveryStreamName("project-stream");

        Record record = new Record();

        for (int i = 0; i < 10*60; i++){
            try {
                URL url = new URL("https://www.google.com/finance/info?q=NASDAQ:AMZN");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    response.append(line);  
                }
                reader.close();

                System.out.println(response.toString().replace("\n", "").replaceAll(" ", ""));
                System.out.println("****\n");

                ByteBuffer buff = ByteBuffer.wrap(response.toString().replace("\n", "").replaceAll(" ", "").getBytes());
                record.setData(buff);

                request.setRecord(record);
                firehoseClient.putRecord(request);
                Thread.sleep(2000);


            }
            catch(Exception e){
                e.printStackTrace();
            }
        }   
    }
}

Solution

  • Use the static builder() method instead of the deprecated constructors.

    The javadoc documentation suggests this approach instead of using the deprecated constructor:

    AmazonKinesisFirehoseClient client = AmazonKinesisFirehoseClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).build();
    // You can now use the client as you normally would...
    client.putRecord(xyz);
    

    See: http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/kinesisfirehose/AmazonKinesisFirehoseClient.html#AmazonKinesisFirehoseClient-com.amazonaws.auth.AWSCredentials-