Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java:Efficiently store Protobuf objects in MySQL

I have a few hundred thousands of ProtoBuf objects in a container object in Java.

The container grew quite large, and serializing into a MySQL database seems to be the best idea.

What's the most reasonable way to store Protobuf Objects in MySQL using Java?

  • What's the right (de)serialization technique?
  • Which field type should I use in the MySQL Schema for the serialized objects?
like image 273
Adam Matan Avatar asked Jan 01 '26 05:01

Adam Matan


1 Answers

Use the protocol buffers built-in serialization technique. Serialize the protobufs, and then store the serialized data in the database. To retrieve the values, simply get the data back from the database and use one of the various parseFrom methods to get the information back.

I'm not an expert on MySQL(or even SQL in general), but you'd probably use a BLOB datatype to store this data. You could have two columns, one which holds the class name and the other which holds the data. It would be possible at that point to use Java reflection to load the class needed, and call parseFrom on that object.

like image 148
rm5248 Avatar answered Jan 02 '26 18:01

rm5248



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!