编写thrift文件是,须要知道thrift文件支持的数据类型有哪些。假设定义Service等。以下是官方文档的说明:

#
# Thrift Tutorial
# Mark Slee (mcslee@facebook.com)
#
# This file aims to teach you how to use Thrift, in a .thrift file. Neato. The
# first thing to notice is that .thrift files support standard shell comments.
# This lets you make your thrift file executable and include your Thrift build
# step on the top line. And you can place comments like this anywhere you like.
#
# Before running this file, you will need to have installed the thrift compiler
# into /usr/local/bin. /**
* The first thing to know about are types. The available types in Thrift are:
*
* bool Boolean, one byte
* byte Signed byte
* i16 Signed 16-bit integer
* i32 Signed 32-bit integer
* i64 Signed 64-bit integer
* double 64-bit floating point value
* string String
* map<t1,t2> Map from one type to another
* list<t1> Ordered list of one type
* set<t1> Set of unique elements of one type
*
* Did you also notice that Thrift supports C style comments? */ // Just in case you were wondering... yes. We support simple C comments too. /**
* Thrift files can reference other Thrift files to include common struct
* and service definitions. These are found using the current path, or by
* searching relative to any paths specified with the -I compiler flag.
*
* Included objects are accessed using the name of the .thrift file as a
* prefix. i.e. shared.SharedObject
*/
include "shared.thrift" /**
* Thrift files can namespace, package, or prefix their output in various
* target languages.
*/
namespace cpp tutorial
namespace java tutorial
php_namespace tutorial
namespace perl tutorial
namespace smalltalk.category Thrift.Tutorial /**
* Thrift lets you do typedefs to get pretty names for your types. Standard
* C style here.
*/
typedef i32 MyInteger /**
* Thrift also lets you define constants for use across languages. Complex
* types and structs are specified using JSON notation.
*/
const i32 INT32CONSTANT = 9853
const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'} /**
* You can define enums, which are just 32 bit integers. Values are optional
* and start at 1 if not supplied, C style again.
* ^ ThriftIDL page says "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" so I'm guessing that's a bug.
* PS: http://enel.ucalgary.ca/People/Norman/enel315_winter1997/enum_types/ states that if values are not supplied, they start at 0 and not 1.
*/
enum Operation {
ADD = 1,
SUBTRACT = 2,
MULTIPLY = 3,
DIVIDE = 4
} /**
* Structs are the basic complex data structures. They are comprised of fields
* which each have an integer identifier, a type, a symbolic name, and an
* optional default value.
*
* Fields can be declared "optional", which ensures they will not be included
* in the serialized output if they aren't set. Note that this requires some
* manual management in some languages.
*/
struct Work {
1: i32 num1 = 0,
2: i32 num2,
3: Operation op,
4: optional string comment,
} /**
* Structs can also be exceptions, if they are nasty.
*/
exception InvalidOperation {
1: i32 what,
2: string why
} /**
* Ahh, now onto the cool part, defining a service. Services just need a name
* and can optionally inherit from another service using the extends keyword.
*/
service Calculator extends shared.SharedService { /**
* A method definition looks like C code. It has a return type, arguments,
* and optionally a list of exceptions that it may throw. Note that argument
* lists and exception lists are specified using the exact same syntax as
* field lists in struct or exception definitions. NOTE: Overloading of
* methods is not supported; each method requires a unique name.
*/ void ping(), i32 add(1:i32 num1, 2:i32 num2), i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch), /**
* This method has an oneway modifier. That means the client only makes
* a request and does not listen for any response at all. Oneway methods
* must be void.
*
* The server may execute async invocations of the same client in parallel/
* out of order.
*/
oneway void zip(),
} /**
* It's possible to declare more than one service per Thrift file.
*/
service CalculatorExtreme extends shared.SharedService {
void pingExtreme(),
} /**
* That just about covers the basics. Take a look in the test/ folder for more
* detailed examples. After you run this file, your generated code shows up
* in folders with names gen-<language>. The generated code isn't too scary
* to look at. It even has pretty indentation.
*/

从文档中,我们能够知道的数据类型有:

 *  bool        Boolean, one byte
* byte Signed byte
* i16 Signed 16-bit integer
* i32 Signed 32-bit integer
* i64 Signed 64-bit integer
* double 64-bit floating point value
* string String
* map<t1,t2> Map from one type to another
* list<t1> Ordered list of one type
* set<t1> Set of unique elements of one type

定义支持语言:

//cpp
namespace cpp tutorial
//java
namespace java tutorial{包名}

最后定义服务:

service Calculator extends shared.SharedService {

  /**
* A method definition looks like C code. It has a return type, arguments,
* and optionally a list of exceptions that it may throw. Note that argument
* lists and exception lists are specified using the exact same syntax as
* field lists in struct or exception definitions. NOTE: Overloading of
* methods is not supported; each method requires a unique name.
*/ void ping(), i32 add(1:i32 num1, 2:i32 num2), i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch), /**
* This method has an oneway modifier. That means the client only makes
* a request and does not listen for any response at all. Oneway methods
* must be void.
*
* The server may execute async invocations of the same client in parallel/
* out of order.
*/
oneway void zip(),
}

以下列举一个完整的java thrift服务的定义:

namespace java com.thrift
service TranslationThriftService{
i32 addENRealtimeData(1:string jsonObject) }

//编译命令 thrift -r -gen java *.thrift

thrift -r -gen java *.thrift

Thrift 文件的格式及可用的数据类型的更多相关文章

  1. 编写服务说明.thrift文件

    1.数据类型 基本类型: bool:布尔值,true 或 false,对应 Java 的 boolean byte:8 位有符号整数,对应 Java 的 byte i16:16 位有符号整数,对应 J ...

  2. wave文件(*.wav)格式、PCM数据格式

    1. 音频简介 经常见到这样的描述: 44100HZ 16bit stereo 或者 22050HZ 8bit mono 等等. 44100HZ 16bit stereo: 每秒钟有 44100 次采 ...

  3. wave文件(*.wav)格式、PCM数据格式, goldwave 可以播放pcm raw audio

    1. 音频简介 经常见到这样的描述: 44100HZ 16bit stereo 或者 22050HZ 8bit mono 等等. 44100HZ 16bit stereo: 每秒钟有 44100 次采 ...

  4. k8s之yaml文件书写格式

    k8s之yaml文件书写格式 1 # yaml格式的pod定义文件完整内容: 2 apiVersion: v1 #必选,版本号,例如v1 3 kind: Pod #必选,Pod 4 metadata: ...

  5. diff和patch的使用、patch文件的格式解说

    为了弄懂 patch中的 p0   p1    和.orig文件是啥,找到了这篇文章! 来源:http://www.cnblogs.com/super119/archive/2010/12/18/19 ...

  6. vim 查看文件二进制格式

    用vim打开文件,vim -b file,选项-b是二进制模式打开   然后输入 :%!xxd,就可看到二进制编码     其实在linux下,直接输入xxd file 也是可以看到的文件二进制格式的

  7. c# 根据文件流查看文件真实格式

    今天在做图片注册的功能的时候,测试提出一个问题:将随便一个非图片文件将后缀名改为jpg或其他,上传时应检验图片合法性.然后同事给提供了根据文件流前两个字节判断文件真实格式的思路,代码如下: publi ...

  8. MXF素材文件交换格式深入研究

    MXF素材文件交换格式深入研究   2012-09-03 | 访问次数:262 | 新闻来源:电科网               [摘要]DCI规定数字电影需采用MXF封装音视频等节目素材内容.为了深 ...

  9. 在.NetCore中使用Myrmec检测文件真实格式

    Myrmec 是什么? Myrmec 是一个用于检测文件格式的库,Myrmec不同于其它库或者手写检测代码,Myrmec不依赖文件扩展名(在实际使用中,你的用户很可能使用虚假的扩展名欺骗你的应用程序) ...

随机推荐

  1. Python— isinstance用法说明

    在学习自动化测试的脚本中发现了这个函数,所以在网上查了一下资料进行如下整理: 通过帮助查看如下: 作用:来判断一个对象是否是一个已知的类型: 其第一个参数(object)为对象,第二个参数为类型名(i ...

  2. Python知识(5)--绘图

    Python学习变得很方便,不用任何安装一个浏览器也能够使用学习,比如Jupyter就是很好的一个网络工具,提供了编辑编译展示等强大的功能,网址如下: https://try.jupyter.org/ ...

  3. 云计算大会有感—MapReduce和UDF

    (转载请注明出处:http://blog.csdn.net/buptgshengod) 1.參会有感       首先还是非常感谢CSDN能给我票,让我有机会參加这次中国云计算峰会.感觉不写点什么对不 ...

  4. mybatis如何根据mapper接口生成其实现类

    SpringBoot集成mybatis mybatis的statement的解析与加载 mybatis如何根据mapper接口生成其实现类 mybatis的mapper返回map结果集 mybatis ...

  5. 实效云计算用户组(ECUG) 与 阿里云

    http://www.ecug.org/ http://www.aliyun.com/   阿里云

  6. leetcode第一刷_Restore IP Addresses

    字符串的问题真是难.一般递归比較好写代码,一般地归还会超时,并且測试用例特别多.. 这道题刚拿到手时直接慌了,这情况也太多了.后来冷静下来想想,事实上还是比較单纯的. 一个ip地址,肯定是四个整数加三 ...

  7. python性能优化建议

    参考: https://segmentfault.com/a/1190000000666603 http://blog.csdn.net/zhoudaxia/article/details/23853 ...

  8. 服务器返回的“未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0提供程序””错误解决

    未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0提供程序”

  9. 【docker】docker部署spring boot项目在服务器上

    IDE:idea 工具:docker spring boot:2.0.1 ======================================== 简单记录一下流程,以供参考: 第一步:首先得 ...

  10. anroid源码下载和编译

    本文是在Ubuntu10.10系统上进行实践的. 1 因为我们需要Android的模拟器,所以需要安装Android的SDK,并创建AVD, 可以一次命名为AVD15,AVD22,AVD23,... ...