0. Thrift的特性

1. 安装Thrift编译器

[Todo]

http://thrift.apache.org/docs/install/

http://thrift.apache.org/docs/install/os_x

2. Thrift类型 (Thrift Type)

[Ref]: http://thrift.apache.org/docs/types

基本类型

  • bool: A boolean value (true or false)
  • byte: An 8-bit signed integer
  • i16: A 16-bit signed integer
  • i32: A 32-bit signed integer
  • i64: A 64-bit signed integer
  • double: A 64-bit floating point number
  • string: A text string encoded using UTF-8 encoding

特殊类型

binary: a sequence of unencoded bytes

结构体 (Structs)

Thrift structs define a common object -- they are essentially equivalent to classes in OOP languages,

but without inheritance. A struct has a set of strongly typed fields, each with a unique name identifier.

Fields may have various annotations (numeric field IDs, optional default values, etc.)

容器 (Containers)

Thrift containers are strongly typed containers that map to commonly used and commonly available

container types in most programming languages.

三种类型的容器:

list: An ordered list of elements.

set: An unordered set of unique elements. (PHP does not support sets, so it is treated similar to a List)

map: A map of strictly unique keys to values.

容器元素可以是任意合法的Thrift Type.

[注]: For maximal compatibility, the key type for map should be a basic type rather than a struct or container type.

异常 (Exceptions)

Exceptions are functionally equivalent to structs, except that they inherit from the native exception

base class as appropriate in each target programming language, in order to seamlessly integrate with the

native exception handling in any given language.

服务 (Services)

Services are defined using Thrift types. Definition of a service is semantically equivalent to defining an

interface (or a pure virtual abstract class) in object oriented programming. The Thrift compiler generates

fully functional client and server stubs that implement the interface.

A service consists of a set of named functions, each with a list of parameters and a return type.

Note that void is a valid type for a function return, in addition to all other defined Thrift types.

Additionally, an oneway modifier keyword may be added to a void function, which will generate

code that does not wait for a response. Note that a pure void function will return a response to

the client which guarantees that the operation has completed on the server side. With oneway

method calls the client will only be guaranteed that the request succeeded at the transport layer.

Oneway method calls of the same client may be executed in parallel/out of order by the server.

3. IDL

[Ref]: http://thrift.apache.org/docs/idl

A Thrift IDL file is processed by the Thrift code generator to produce code for the various target

languages to support the defined structs and services in the IDL file.

3.1 Document

每个Thrift文档包含0或者多个header,header后紧跟0或者多个definitions.

[]  Document        ::=  Header* Definition*

3.2 Header

A header is either a Thrift include, a C++ include, or a namespace declaration.

[]  Header          ::=  Include | CppInclude | Namespace

3.2.1 Thrift Include

An include makes all the symbols from another file visible (with a prefix) and adds corresponding

include statements into the code generated for this Thrift document.

[]  Include         ::=  'include' Literal

3.2.2 C++ Include

A C++ include adds a custom C++ include to the output of the C++ code generator for this Thrift document.

[]  CppInclude      ::=  'cpp_include' Literal

3.2.3 Namespace

A namespace declares which namespaces/package/module/etc. the type definitions in this file will be declared in

for the target languages. The namespace scope indicates which language the namespace applies to;

a scope of '*' indicates that the namespace applies to all target languages.

[]  Namespace       ::=  ( 'namespace' ( NamespaceScope Identifier ) |
( 'smalltalk.category' STIdentifier ) |
( 'smalltalk.prefix' Identifier ) ) |
( 'php_namespace' Literal ) |
( 'xsd_namespace' Literal ) [] NamespaceScope ::= '*' | 'cpp' | 'java' | 'py' | 'perl' | 'rb' | 'cocoa' | 'csharp'

3.3 Definition

[]  Definition      ::=  Const | Typedef | Enum | Senum | Struct | Union | Exception | Service

3.3.1 Const

[]  Const           ::=  'const' FieldType Identifier '=' ConstValue ListSeparator?

3.3.2 Typedef

[]  Typedef         ::=  'typedef' DefinitionType Identifier

3.3.3 Enum

An enum creates an enumerated type, with named values. If no constant value is supplied, the value is

either 0 for the first element, or one greater than the preceding value for any subsequent element.

Any constant value that is supplied must be non-negative.

[] Enum            ::=  'enum' Identifier '{' (Identifier ('=' IntConstant)? ListSeparator?)* '}'

3.3.4 Senum

Senum (and Slist) are now deprecated and should both be replaced with String.

3.3.5 Struct

[] Struct          ::=  'struct' Identifier 'xsd_all'? '{' Field* '}'
[注]: The xsd_all keyword has some purpose internal to Facebook but serves no purpose in Thrift itself.
Use of this feature is strongly discouraged 
 

3.3.6 Union

Unions are similar to structs, except that they provide a means to transport exactly one field of a
possible set of fields, just like union {} in C++. Consequently, union members cannot be required fields.
[] Union          ::=  'union' Identifier 'xsd_all'? '{' Field* '}'

[注]: The xsd_all keyword has some purpose internal to Facebook but serves no purpose in Thrift itself.

Use of this feature is strongly discouraged

3.3.7 Exception

Exceptions are similar to structs except that they are intended to integrate with the native exception

handling mechanisms in the target languages. The name of each field must be unique within the exception.

[] Exception       ::=  'exception' Identifier '{' Field* '}'

3.3.8 Service

A service provides the interface for a set of functionality provided by a Thrift server. The interface is simply

a list of functions. A service can extend another service, which simply means that it provides the functions of

the extended service in addition to its own.

[] Service         ::=  'service' Identifier ( 'extends' Identifier )? '{' Function* '}'

3.3.9 Field

[] Field           ::=  FieldID? FieldReq? FieldType Identifier ('= ConstValue)? XsdFieldOptions ListSeparator?

Field ID

[] FieldID         ::=  IntConstant ':'

Field Requiredness

[] FieldReq        ::=  'required' | 'optional'

XSD Options

[注]: These have some internal purpose at Facebook but serve no current purpose in Thrift.

Use of these options is strongly discouraged.

[] XsdFieldOptions ::=  'xsd_optional'? 'xsd_nillable'? XsdAttrs?

[] XsdAttrs        ::=  'xsd_attrs' '{' Field* '}'

3.3.10 Functions

[] Function        ::=  'oneway'? FunctionType Identifier '(' Field* ')' Throws? ListSeparator?

[] FunctionType    ::=  FieldType | 'void'

[] Throws          ::=  'throws' '(' Field* ')'

3.3.11 Types

[] FieldType       ::=  Identifier | BaseType | ContainerType

[] DefinitionType  ::=  BaseType | ContainerType

[] BaseType        ::=  'bool' | 'byte' | 'i16' | 'i32' | 'i64' | 'double' | 'string' | 'binary' | 'slist'

[] ContainerType   ::=  MapType | SetType | ListType

[] MapType         ::=  'map' CppType? '<' FieldType ',' FieldType '>'

[] SetType         ::=  'set' CppType? '<' FieldType '>'

[] ListType        ::=  'list' '<' FieldType '>' CppType?

[] CppType         ::=  'cpp_type' Literal

3.3.12 Constant Values

[] ConstValue      ::=  IntConstant | DoubleConstant | Literal | Identifier | ConstList | ConstMap

[] IntConstant     ::=  ('+' | '-')? Digit+

[] DoubleConstant  ::=  ('+' | '-')? Digit* ('.' Digit+)? ( ('E' | 'e') IntConstant )?

[] ConstList       ::=  '[' (ConstValue ListSeparator?)* ']'

[] ConstMap        ::=  '{' (ConstValue ':' ConstValue ListSeparator?)* '}'

3.3.13 Basic Definitions

Literal

[] Literal         ::=  ('"' [^"]* '"') | ("'" [^']* "'")

Identifier

[] Identifier      ::=  ( Letter | '_' ) ( Letter | Digit | '.' | '_' )*

[] STIdentifier    ::=  ( Letter | '_' ) ( Letter | Digit | '.' | '_' | '-' )*

List Separator

[] ListSeparator   ::=  ',' | ';'

Letters and Digits

[] Letter          ::=  ['A'-'Z'] | ['a'-'z']

[] Digit           ::=  [''-'']

3.4 例子

[Todo]

http://thrift.apache.org/docs/idl

http://wiki.apache.org/thrift/Tutorial


Where to go

Thrift.1

Thrift.0的更多相关文章

  1. 在Mac OS X 10.9上安装 Thrift 0.9.1

    Thrift 0.9.1 官方文档中对于Mac OS X上的安装描述适合 10.8,但不适用于10.9. Homebrew  macport 默认都不能在 10.9上安装Thrift 0.9.1成功 ...

  2. ubuntu thrift 0.9.3编译安装

    Table of Contents 1. 下载thrift源代码 2. 编译并安装 3. 运行测试程序 4. 安装 1 下载thrift源代码 git clone https://git-wip-us ...

  3. OS X 10.9 Mavericks 安装 thrift 0.9.1

    通过Homebrew安装的时候,编译会报错.查了一下资料,原来是10.9系统默认使用的libc++的库,而且移除了C++ 11标准前tr库,所以编译存在问题.且笔者使用的时候,brew安装只支持到0. ...

  4. Ubuntu 12.04下安装thrift 0.9

    Thrift这里就不介绍了,只说一句--Facebook很牛逼. 我这里安装Thrift主要是为Accumulo数据库作准备,所以java语言为必选项. 具体安装参考官方Apache Thrift R ...

  5. ubuntu 14.04 安装 Apache Thrift 0.10

    1.到官网下载源码压缩文件 https://thrift.apache.org/download 2.安装依赖软件,可以参考 https://thrift.apache.org/docs/instal ...

  6. thrift接口描述语言 (基于thrift 0.13.0版本)

    thrift接口描述语言(IDL)用来定义thrift类型. 一个Thrift IDL文件用来生成各种语言使用的结构体和服务. IDL中包含如下部分: 1. Document Document中包含0 ...

  7. thrift编译java的问题-(安装thrift0.8.0成功-编译mapkeeper.java成功)

    上一次帖子说了thrift编译java出现错误,由于只用到cpp版的,就将此略过.但是老版本的ycsb不是很好用,于是决定以locall的方式编译mapkeeper供最新版ycsb使用.目前根据 ht ...

  8. 和 Thrift 的一场美丽邂逅

    一. 与 Thrift 的初识 也许大多数人接触 Thrift 是从序列化开始的.每次搜索 “java序列化” + “方式”.“对比” 或 “性能” 等关键字时,搜索引擎总是会返回一大堆有关各种序列化 ...

  9. Thrift简单实践

    0.什么是RPC RPC(Remote Procedure Call - 远程过程调用),是通过网络从远程计算机上请求服务,而不需要了解底层网路技术的细节.简单点说,就是像调用本地服务(方法)一样调用 ...

随机推荐

  1. swift杂记

    1.0   数据类型强转  范围小 --->范围大  不会丢失精度  : 范围大 ---> 范围小 ,可能丢失精度 如 :Int(4.2) = 4 :CGFloat(2) = 2.0 2. ...

  2. 2018面向对象程序设计(Java)第15周学习指导及要求

    2018面向对象程序设计(Java)第15周学习指导及要求 (2018.12.6-2018.12.9)   学习目标 (1) 掌握Java应用程序打包操作: (2) 了解应用程序存储配置信息的两种方法 ...

  3. 解决SMARTFORMS文本编辑器不能打开

    在DEV打开SMARTFORMS文本编辑器时,出现如下错误 由于宏安全设置,无法找到宏或宏被禁用. 解决方法如下: 在DEV环境新建程序后输入如下代码执行即可. *&------------- ...

  4. mongo副本集设置主库权重,永远为主

    mongo副本集设置主库权重,即使主库宕机了再重启也还是主库. cfg = rs.conf()     ------->(查看序列)cfg.members[0].priority = 1 (设置 ...

  5. python-ceilometerclient命令行(2)

    命令行解析工具argparse argparse是python标准库中的模块,利用argparse,可以完成对命令行的参数定义.解析以及后续的处理.一个简单的例子: # coding:utf-8 im ...

  6. PR回写 所有物料规划PR时对净需求+最小采购批量+安全库存+舍入值的先后考虑逻辑

    所有物料规划PR时对净需求+最小采购批量+安全库存+舍入值的先后考虑逻辑20171207-1228.docx PR回写案例一: '; --SAFE_QTY:安全库存 ' ; -- MIN_LOT_SI ...

  7. ELK 日志学习

    一.Elasticsearch 安装(所有的版本均使用5.5.0 ,且版需要相同 logstash \ kibana \ filebeat ) 官网下载地址:https://www.elastic.c ...

  8. 强制改变css样式优先级

    .list{ left:20px!important; } css !important作用是提高指定CSS样式规则的应用优先权. !important是CSS1就定义的语法,作用是提高指定样式规则的 ...

  9. hibernate 中,出现了错误 "node to traverse cannot be null!" 如何改正

    这个错误基本上是因为hql语句写错了而造成的, 返回找hql输出一下发现, hql语句中间少了几个空格, 写成了String hql = "from"+className+&quo ...

  10. 数位dp poj1850

    题目链接:https://vjudge.net/problem/POJ-1850 这题我用的是数位dp,刚刚看了一下别人用排列组合,我脑子不行,想不出来. 在这题里面我把a看成1,其他的依次递增,如果 ...