Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protocol Buffer - merge binary data files with the same .proto file to the one file

I have a lot of data files (almost 150) in binary structure created according the .proto scheme of Protocol Buffer. Is there any efficient solution how to merge all the files to just one big binary data file without losing any information?

like image 245
Lkor Avatar asked Dec 09 '25 05:12

Lkor


1 Answers

If your scheme allows it, you could combine existing data.

The Scheme

message People {
  repeated Person person = 1;
}
message Person {
  required int32 id = 1;
  required string name = 2;
  optional string email = 3;
}

Existing Data

Two existing binary files conataining a Person, each.

  • person1.bin
  • person2.bin

Python Code

import p_pb2

people = p_pb2.People()
people.person.add().ParseFromString(open("person1.bin", "rb").read())
people.person.add().ParseFromString(open("person2.bin", "rb").read())
with open("people.bin", "wb") as o:
    o.write(people.SerializeToString())

Combined Data

Now the file people.bin contains a People instance including both Person instances.


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!