Protocol buffers stored as Oracle BLOB: python fails to get

I am trying to read a protocolbuffer message previously saved as a BLOB in Oracle DB. I use python + google protocol buffers to read python to read data:

oracle 11g
python 2.6.7
google protocol buffers 2.4.1

Parsing the message is fine: it does not return any exceptions.
However, the size of the message to read is always 0 (displaying a string, as the text indicates that it is not empty)

cursor.execute("select myblob from mytable") mydata = Data_pb2.MyData() for dataDB in cursor: mydata.ParseFromString(dataDB[0]) print "size:" + str(mydata.ByteSize()) 

Any idea? Greetings.

+4
source share
1 answer

It is not indicated which module is used to access Oracle, so I assume it is cx_Oracle.

When the LOB is read from Oracle, the OCI provides a LOB locator that is wrapped in a cx_Oracle object. Thus, in the example, dataDB [0] is not a string, but a cx_Oracle.LOB object. Depending on how ParseFromString is encoded, it may or may not be properly converted to a string.

Therefore, I would use dataDB [0] .read () as a parameter for ParseFromString.

 cursor.execute("select myblob from mytable") for dataDB in cursor: lob = dataDB[0].read() print "size:", len(lob) mydata = Data_pb2.MyData() mydata.ParseFromString(lob) 

In addition, the ByteSize () method is usually used during serialization to calculate the size of a serialized message. I'm not sure that it can be used immediately after parsing the message. In the protocol buffer API, ByteSize () is explicitly associated with serialization. In fact, it makes no sense to call it during parsing, since the size of the buffer is already known before parsing.

0
source

Source: https://habr.com/ru/post/1400776/


All Articles