avsc 是 Apache Avro 的纯 JavaScript 实现。
**
完整的 Avro 架构支持,包括递归架构, sort order, 以及 schema evolution.
快速!速度相当于 JSON 的两倍,同时更少的编码(varies per schema).
无依赖, avsc 甚至可以在浏览器运行
avsc
解码吞吐率的示意图(越高越好):
库比较:
node-avsc, this package.
node-avsc
node-json, built-in JSON serializer.
node-json
node-pson, an alternative to JSON.
node-pson
node-avro-io, most popular previously existing Avro implementation.
node-avro-io
在一个 node.js 模块,或使用 browserify:
var avsc = require('avsc');
编码和解码对象:
// We can declare a schema inline:var type = avsc.parse({
name: ‘Pet’, type: ‘record’, fields: [ {name: ‘kind’, type: {name: ‘Kind’, type: ‘enum’, symbols: [‘CAT’, ‘DOG’]}}, {name: ‘name’, type: ‘string’} ] });var pet = {kind: ‘CAT’, name: ‘Albert’};var buf = type.toBuffer(pet); // Serialized object.var obj = type.fromBuffer(buf); // {kind: ‘CAT’, name: ‘Albert’}
生成一个 schema 的随机实例:
// We can also parse a JSON-stringified schema:
var type = avsc.parse(‘{“type”: “fixed”, “name”: “Id”, “size”: 4}’); var id = type.random(); // E.g. Buffer([48, 152, 2, 123])
检查对象是否符合给定 schema:
// Or we can specify a path to a schema file (not in the browser):
var type = avsc.parse(‘./Person.avsc’); var person = {name: ‘Bob’, address: {city: ‘Cambridge’, zip: ‘02139’}}; var status = type.isValid(person); // Boolean status.
从一个 Avro 容器文件(不在浏览器)得到解码记录的readable stream:
avsc.createFileDecoder('./records.avro')
.on(‘metadata’, function (type) { / type is the writer’s type. / }) .on(‘data’, function (record) { / Do something with the record. / });
type