avsc 详细介绍
avsc 是 Apache Avro 的纯 JavaScript 实现。
解码吞吐率的示意图(越高越好):
库比较:
node-avsc, this package. node-json, built-in JSON serializer. node-pson, an alternative to JSON. node-avro-io, most popular previously existing Avro implementation.在一个 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. */ });文章转载自 开源中国社区[https://www.oschina.net]
相关资源:ApacheAvro纯JavaScript实现avsc.zip