This article explains the serialization mechanism Thrift, thrift as a data exchange format to see how it works?
1. Construction scenarios:
1). First, let's define a simple structure at the thrift.
1
2
3
4
5
namespace java com.yangyang.thrift.api
struct Pair {
1: required string key
2: required string value
}
required modifiers sure you can guess its meaning, but if you have no such doubts, "1", "2" What is the meaning of these numerical identifier exactly? What kind of role it actually plays in the serialization mechanism? compile and thrift -gen java
2) Write the test code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
private static String datafile = "1.dat";
// *) the object into the document
public static void writeData() throws IOException, TException {
Pair pair = new Pair();
pair.setKey("key1").setValue("value1");
FileOutputStream fos = new FileOutputStream(new File(datafile));
Versioning in Thrift is implemented via field identifiers. The field header for every member of a struct in Thrift is encoded with a unique field identifier. The combination of this field identifier and its type specifier is used to uniquely identify the field. The Thrift definition language supports automatic assignment of field identifiers, but it is good programming practice to always explicitly specify field identifiers.
thrift and common data exchange format comparison | Interchange Format | Type | advantages | disadvantages | | - | - | - | - | | the Xml | text | read | bloated, does not support the binary data types | | Json | text | Easy read | discard the types of information, such as "score": 100, to score type is int / double parsing ambiguous, does not support the binary data types | | the java serizable | binary | simple | bloated, limited only in java field | | Thrift | | binary | efficient | should not be read backwards compatible with certain conventions limit | | Google Protobuf | binary | efficient | should not be read backwards compatible with certain contractual constraints |
4. Practice backward compatibility
Thrift official document, also referred to the new field properties, using id incrementally to identify and optional modifications are made to add.