Thrift 文件的格式及可用的数据类型
编写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 文件的格式及可用的数据类型的更多相关文章
- 编写服务说明.thrift文件
1.数据类型 基本类型: bool:布尔值,true 或 false,对应 Java 的 boolean byte:8 位有符号整数,对应 Java 的 byte i16:16 位有符号整数,对应 J ...
- wave文件(*.wav)格式、PCM数据格式
1. 音频简介 经常见到这样的描述: 44100HZ 16bit stereo 或者 22050HZ 8bit mono 等等. 44100HZ 16bit stereo: 每秒钟有 44100 次采 ...
- wave文件(*.wav)格式、PCM数据格式, goldwave 可以播放pcm raw audio
1. 音频简介 经常见到这样的描述: 44100HZ 16bit stereo 或者 22050HZ 8bit mono 等等. 44100HZ 16bit stereo: 每秒钟有 44100 次采 ...
- k8s之yaml文件书写格式
k8s之yaml文件书写格式 1 # yaml格式的pod定义文件完整内容: 2 apiVersion: v1 #必选,版本号,例如v1 3 kind: Pod #必选,Pod 4 metadata: ...
- diff和patch的使用、patch文件的格式解说
为了弄懂 patch中的 p0 p1 和.orig文件是啥,找到了这篇文章! 来源:http://www.cnblogs.com/super119/archive/2010/12/18/19 ...
- vim 查看文件二进制格式
用vim打开文件,vim -b file,选项-b是二进制模式打开 然后输入 :%!xxd,就可看到二进制编码 其实在linux下,直接输入xxd file 也是可以看到的文件二进制格式的
- c# 根据文件流查看文件真实格式
今天在做图片注册的功能的时候,测试提出一个问题:将随便一个非图片文件将后缀名改为jpg或其他,上传时应检验图片合法性.然后同事给提供了根据文件流前两个字节判断文件真实格式的思路,代码如下: publi ...
- MXF素材文件交换格式深入研究
MXF素材文件交换格式深入研究 2012-09-03 | 访问次数:262 | 新闻来源:电科网 [摘要]DCI规定数字电影需采用MXF封装音视频等节目素材内容.为了深 ...
- 在.NetCore中使用Myrmec检测文件真实格式
Myrmec 是什么? Myrmec 是一个用于检测文件格式的库,Myrmec不同于其它库或者手写检测代码,Myrmec不依赖文件扩展名(在实际使用中,你的用户很可能使用虚假的扩展名欺骗你的应用程序) ...
随机推荐
- java线程系列文章之一(线程的安全性)
本文来自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/7421217,转载请注明. 当我们查看JDK API的时候,总会发现一些类 ...
- Codeforces Round #295 (Div. 2)C - DNA Alignment 数学题
C. DNA Alignment time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- .NET面试宝典-高级2
http://blog.csdn.net/shanyongxu/article/category/6023593 对于 Web 性能优化,您有哪些了解和经验吗? 1.前端优化 (1)减少 HTTP 请 ...
- js:深入prototype(上:内存分析)
/** * 下面演示了通过原型的创建方式,使用基于原型的创建能够将属性和方法 * 设置为Person专有的,不能通过window来调用. * 原型是javascript中的一个特殊对象,当一个函 ...
- apache如何支持asp.net
Apache是目前广泛使用的一种网络服务器程序,不仅在UNIX/LINUX平台上被大量使用,而且在Windows平台上也有许多站点放弃了IIS而转向Apache..NET是微软推出的功能强大的开发技术 ...
- PHP中的 变量 与 常量 详解
几乎所有的编程语言都会涉及到变量和常量这两个概念,PHP也不例外.本节将介绍PHP语言中的变量和常量的应用方法. 一.什么是变量和常量 在程序执行的过程中,变量存储的值可以随时改变,而常量存储的值是不 ...
- 使用框架帮助Activity规范化
摘要 本文原创,转载请注明地址:http://kymjs.com/code/2015/05/10/01 写给那些在用.想用.还没有用过KJFrame的朋友. KJFrameForAndroid总共分为 ...
- Java7语法新特性
Java7语法新特性: 1. switch中增加对String类型的支持. public String generate(String name, String gender) { String ti ...
- [翻译] SWTableViewCell
SWTableViewCell An easy-to-use UITableViewCell subclass that implements a swippable content view whi ...
- JS 中div内容的显示和隐藏
1. document.getElementById("dialog-auclot-status").style.display="none";//页面加载时隐 ...