Search code examples
amazon-web-servicesspring-cloudbytebufferamazon-kinesis

Convert object to ByteBuffer


My situation is that developing on spring.boot.version = 1.4.2, can't upgrade our boot version(our service is so huge).

I need to use Kafka for our service.
So I implemented this feature using spring-cloud-stream-binder-kafka.
spring-cloud-stream-binder-kafka:1.1.2.RELEASE is supporting spring boot version 1.4.6, so I can implemented this feature.

Until now, it's not bad.
But we're using AWS on our services, there is no kafka in AWS as you know.
So I tried to use spring-cloud-stream-binder-kinesis:1.0.0.RELEASE.
But unfortunately, spring-cloud-stream-binder-kinesis:1.0.0.RELEASE version is supporting over bootVersion 2.0.0.

So I have to implement this feature using Kinesis Producer Library.
(I refer https://github.com/awslabs/amazon-kinesis-producer/blob/master/java/amazon-kinesis-producer-sample/src/com/amazonaws/services/kinesis/producer/sample/SampleProducer.java)

I have to publish Java object to kinesis, so I should pass Java Object to data argument of KinesisProducer.addUserRecord.
So, how can I convert Java Object to ByteBuffer?


Solution

  • You need to first convert it to a byte[], then call ByteBuffer.wrap() on that array.

    You could use Java serialization to do this, but I strongly recommend using some form of JSON serialization. That will make the records easily used by other consumers, which is one of the reasons to use something like Kinesis in the first place.

    Also, AWS does provide a managed Kafka service. I haven't used it, so can't compare to a self-managed Kafka cluster, and don't know if it's available in all regions. But if you already have the tools and experience to use Kafka, it might be a better choice for you.