AWS: Java Post to Kinesis Queue

This entry is part 4 of 5 in the series AWS & Java
(Last Updated On: )

Posting to an AWS Kinesis Queue is rather simple and straight forward. As always you should refer to AWS Documentation.

Put Multiple Records On Queue

Import the following

  1. import com.amazonaws.auth.AWSStaticCredentialsProvider;
  2. import com.amazonaws.auth.BasicAWSCredentials;
  3. import com.amazonaws.services.kinesis.AmazonKinesis;
  4. import com.amazonaws.services.kinesis.AmazonKinesisClientBuilder;
  5. import com.amazonaws.services.kinesis.model.PutRecordsRequest;
  6. import com.amazonaws.services.kinesis.model.PutRecordsRequestEntry;
  7. import com.amazonaws.services.kinesis.model.Record;

Put Records

  1. AmazonKinesisClientBuilder clientBuilder = AmazonKinesisClientBuilder.standard().withRegion(myRegion).withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(myAccessKeyId, mySecretKey)));
  2. AmazonKinesis kinesisClient = clientBuilder.build();
  3. PutRecordsRequest putRecordsRequest = new PutRecordsRequest();
  4. putRecordsRequest.setStreamName(myQueue);
  5. List putRecordsRequestEntryList = new ArrayList<>();
  6.  
  7.  
  8. //You can put multiple entries at once if you wanted to
  9. PutRecordsRequestEntry putRecordsRequestEntry = new PutRecordsRequestEntry();
  10. putRecordsRequestEntry.setData(ByteBuffer.wrap(myData));
  11. putRecordsRequestEntry.setPartitionKey(myKey);
  12. putRecordsRequestEntryList.add(putRecordsRequestEntry);
  13.  
  14.  
  15. putRecordsRequest.setRecords(putRecordsRequestEntryList);
  16. PutRecordsResult putResult = kinesisClient.putRecords(putRecordsRequest);

Put Single Record On Queue

Import the following

  1. import com.amazonaws.auth.AWSStaticCredentialsProvider;
  2. import com.amazonaws.auth.BasicAWSCredentials;
  3. import com.amazonaws.services.kinesis.AmazonKinesis;
  4. import com.amazonaws.services.kinesis.AmazonKinesisClientBuilder;
  5. import com.amazonaws.services.kinesis.model.PutRecordRequest;
  6. import com.amazonaws.services.kinesis.model.Record;

Put Record

  1. AmazonKinesisClientBuilder clientBuilder = AmazonKinesisClientBuilder.standard().withRegion(myRegion).withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(myAccessKeyId, mySecretKey)));
  2. AmazonKinesis kinesisClient = clientBuilder.build();
  3. PutRecordRequest putRecordRequest = new PutRecordRequest();
  4. putRecordRequest.setStreamName(myQueue);
  5.  
  6. putRecordRequest.setData(ByteBuffer.wrap(data.getBytes("UTF-8")));
  7. putRecordRequest.setPartitionKey(myKey);
  8.  
  9. PutRecordResult putResult = kinesisClient.putRecord(putRecordRequest);

You now have put a record(s) onto the queue congratulations!

Series Navigation<< AWS: Java S3 UploadAWS: Send Simple Email Service >>