To write thrift definition files, be sure to familiarize yourself with the common data types of thrift:
1. Basic type (corresponding Java type in parentheses):
BOOL (Boolean): Boolean type (TRUE or FALSE)
Byte (byte): 8-bit signed integer
I16 (short): 16-bit signed integer
I32 (int): 32-bit signed integer
I64 (Long): 64-bit signed integer
Double (double): 64-bit floating-point number
String: Strings encoded with UTF-8
2. Special type (corresponding Java type in parentheses):
Binary (Bytebuffer): A byte stream without coding
3.STRUCTS (structure):
struct defines a very common OOP object, but there is no inheritance attribute.
struct UserProfile {
1:I32 uid,
2:string name,
3:string blurb
}
If the variable has a default value, it can be written directly in the definition file:
struct UserProfile {
1:I32 uid = 1,
2:string name = "User1",
3:string blurb
}
4. Containers, in addition to the basic data types mentioned above, thrift also supports the following container types:
List (java.util.ArrayList):
Set (Java.util.HashSet):
Map (JAVA.UTIL.HASHMAP):
The thrift container is closely related to the type, which corresponds to the type of container provided by the current popular programming language, expressed in a Java generic style. The thrift provides 3 types of containers:
LIST<T1>: An ordered table of elements of a series of T1 types that can be repeated
SET<T1>: An unordered table of elements consisting of a series of T1 types, with the element unique
Map<t1,t2>:key/value pair (key type is T1 and key is unique, value type is t2).
The element type in the container can be any legitimate thrift type (including structs and exceptions) except for service accidents.
Usage is as follows:
struct Node {
1:I32 ID,
2:string name,
3:list<i32> Subnodelist,
4:map<i32,string> Subnodemap,
5:set<i32> Subnodeset
}
Other object that contains the definition:
struct Subnode {
1:I32 uid,
2:string name,
3:I32 PID
}
struct Node {
1:I32 uid,
2:string name,
3:list<subnode> subnodes
}
5.Services service, which is the interface of external presentation:
Service Userstorage {
void Store (1:userprofile user),
UserProfile Retrieve (1:i32 uid)
}