Adam Matan
Adam Matan

Reputation: 136351

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?

Upvotes: 3

Views: 3874

Answers (2)

rm5248
rm5248

Reputation: 2645

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.

Upvotes: 2

Mike Thomsen
Mike Thomsen

Reputation: 37506

You could probably do a lot worse than Hibernate. The beauty of that approach would be that you could serialize them to decent records that could be queried while not losing the ability to turn them back into protobuf messages.

Upvotes: 0

Related Questions