2

Suppose we have 10mb of data in a table, with select query I am storing all those data in resultset object. Now I have to iterate over the resultset and convert it to specific json format.

I have to store only 1mb of data in that json message so that i can publish 1mb json message into kafka topic. SO how to check the size of json message in bytes?

jsonObject.toString().getBytes().length; is this correct?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Seriously
  • 155
  • 5
  • You can't really be sure from the Java side how much storage space a single character will take, because it's only determined on the storage side. – Mike Kim Jan 02 '23 at 06:35
  • Similar topic https://stackoverflow.com/questions/40970411/calculate-json-array-size-in-kb-or-mb-in-android – Pavel Jan 02 '23 at 06:36
  • 1
    @MikeKim `getBytes` gets the data in a string in a particular encoding, so it is independent of how Java Strings represent characters – tgdavies Jan 02 '23 at 06:48
  • Removing Kafka tag since question is about JVM internals. Removing library tags since they're not relevant for finding size of a String/byte array – OneCricketeer Jan 02 '23 at 17:21
  • If size is a truly a concern, use a binary format like Protobuf and enable Kafka message compression. Lastly, Kafka can easily support larger events, if configured – OneCricketeer Jan 02 '23 at 17:23

0 Answers0