10

With C++ Apache Thrift library, is it possible to use only Serialization/Deserialization and not use RPC services?

As I understand from this page, it is possible to do with Java library. However, I could not find the similar classes for C++ library.

Community
  • 1
  • 1
Lazylabs
  • 1,414
  • 16
  • 23

5 Answers5

8

Yes it is possible. Thrift lacks documentation about this subject. Well, about anything really.

Here i found this:

http://mail-archives.apache.org/mod_mbox/thrift-user/201010.mbox/%3C5EF8F634-79A2-45C4-9A04-6D96D3B7A84F@manbert.com%3E

i personally use boost::serialization if there is no need to transfer data over network. Much clear syntax and supports JSON, XML and binary output/input.

mikbal
  • 1,168
  • 1
  • 16
  • 27
  • Thanks. Thrift documentation is pretty much non-existent. – Lazylabs Sep 10 '12 at 05:43
  • In the meantime (since 2012), there is plenty of docs available. Nevertheless, there's some thruth to it w/regard to "serialization only". Alttough deriveable from the full solution with RPC, we can do better => [THRIFT-4128](https://issues.apache.org/jira/browse/THRIFT-4128) – JensG Mar 22 '17 at 10:36
4

In c++ you can use the TFileTransport:

boost::shared_ptr<TFileTransport> transport(new TFileTransport(filename));
boost::shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport));
yourObj.write(protocol.get()); // to write

or

yourObj.read(protocol.get()); // to read
3

If you simply want to serialize into bytes (without having to write to a file), you can use TMemoryBuffer.

boost::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
boost::shared_ptr<TBinaryProtocol> binaryProtcol(new TBinaryProtocol(buffer));

obj->write(binaryProtcol.get());
obj->read((binaryProtcol.get()));
jeffreyveon
  • 13,400
  • 18
  • 79
  • 129
  • Does the `obj` have to be an instance of TBase? Also, once the `obj` has been serialized to bytes using the write() call, how does one access those resulting bytes? Thanks. – jithinpt Mar 14 '17 at 22:46
1

Here's a different mailing list post with some attached code showing how to use thrift to serialize to a file in C++.

http://mail-archives.apache.org/mod_mbox/thrift-user/201203.mbox/%3C90483D01-ED25-4707-9DB2-5BB3627301FC@manbert.com%3E

Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
1

I realize that this question was asked a while ago. I encountered a similar use case recently (with an additional requirement - sending the serialized bytes to a Kafka cluster). Here's my answer with more complete code snippets that illustrate how the serialization and sending tasks can be done.

Community
  • 1
  • 1
jithinpt
  • 1,204
  • 2
  • 16
  • 33
  • One of them is a duplicate as it is today. My recommendation would be to consider rephrasing of the other questiion in a way that makes it a non-duplicate, i.e. focusing on the Kafka part of the problem. – JensG Mar 22 '17 at 10:26
  • Thanks for pointing that out. I've reworded the other question to focus on Kafka. Let me know if more changes are required. – jithinpt Mar 22 '17 at 17:46