数据序列化格式简介

[TOC]

数据序列化

用于模块通讯时,将对象序列化为通信流,高效的传输到另一个模块,并提供反序列化还原数据

Json

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写,同时也易于机器解析和生成。
它基于JavaScript(Standard ECMA-262 3rd Edition - December 1999)的一个子集。

现在每个语言肯定含有json机构的序列化和反序列化工具,用于数据的编写和传输

json使用方便,序列化过程的数据也非常便于开发者阅读,缺点是当数据量很大的时候,性能不太好

XML

XML 指可扩展标记语言(EXtensible Markup Language)。 是一种标记语言,很类似 HTML

  • XML 的设计宗旨是传输数据,而非显示数据
  • XML 标签没有被预定义
  • 需要自行定义标签
  • XML 被设计为具有自我描述性

http://www.w3.org/XML/

XML 序列化和反序列化是目前效率最低的,但可承载数据类型和数据量是最大的

thrift

http://thrift.apache.org/

解决facebook系统中各系统间大数据量的传 输通信以及系统之间语言环境不同需要跨平台的特性。
所以thrift可以支持多种程序语言,例如: C++, C#, Cocoa, Erlang, Haskell, Java, Ocami, Perl, PHP, Python, Ruby, Smalltalk.
在多种不同的语言之间通信thrift可以作为二进制的高性能的通讯中间件,支持数据(对象)序列化和多种类型的RPC服务。

  • 优点,大量语言都支持thrift,传输支持文本图片视频,性能很好,反序列化和序列化都非常高效,数据量大也不会有太大影响,thrift还提供RPC功能
  • 缺点,对用于程序对程序静态的数据交换,需要先确定好他的数据结构,他是完全静态化的当数据结构发生变化时,必须重新编辑IDL文件,代码生成,再编译载入的流程。

protobuf

http://code.google.com/p/protobuf

protobuf全称Protocol Buffers,是google推出的一种高效,快捷的数据交换格式,和XML,Thrift一样,都是一种数据交换协议。

-它是一种二进制的数据格式,具有更高的传输,打包和解包效率,可以传输一切Thrift可以传输的东西

  • 缺点是没有RPC功能
  • 目前支持的语言不如thrift多

avro

http://avro.apache.org/
Avro是Hadoop中的一个子项目,也是Apache中一个独立的项目,Avro是一个基于二进制数据传输高性能的中间件。在Hadoop的其他项目中例如HBase(Ref)和Hive(Ref)的Client端与服务端的数据传输也采用了这个工具。
Avro是一个数据序列化的系统。
Avro 可以将数据结构或对象转化成便于存储或传输的格式。
Avro设计之初就用来支持数据密集型应用,适合于远程或本地大规模数据的存储和交换

简单的数据类型

类型 说明
null no value
boolean a binary value
int 32-bit signed integer
long 64-bit signed integer
float single precision (32-bit) IEEE 754 floating-point number
double double precision (64-bit) IEEE 754 floating-point number
bytes sequence of 8-bit unsigned bytes
string unicode character sequence

复杂数据类型

类型 属性 说明
Records type name record
name a JSON string providing the name of the record (required).
namespace a JSON string that qualifies the name(optional).
doc a JSON string providing documentation to the user of this schema (optional).
aliases a JSON array of strings, providing alternate names for this record (optional).
fields type name a JSON array, listing fields (required).
name a JSON string.
type a schema/a string of defined record.
default a default value for field when lack.
order ordering of this field.
Enums type name enum
name a JSON string providing the name of the enum (required).
namespace a JSON string that qualifies the name.
doc a JSON string providing documentation to the user of this schema (optional).
aliases a JSON array of strings, providing alternate names for this enum (optional)
symbols a JSON array, listing symbols, as JSON strings (required). All symbols in an enum must be unique.
Arrays type name array
items the schema of the array’s items.
Maps type name map
values the schema of the map’s values
Fixed type name fixed
name a string naming this fixed (required).
namespace a string that qualifies the name.
aliases a JSON array of strings, providing alternate names for this enum (optional).
size an integer, specifying the number of bytes per value (required).
Unions a JSON arrays

你可能感兴趣的:(数据序列化格式简介)