Data type of Thrift

Source: Internet
Author: User

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)
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.