文件名称:ApacheAvro纯JavaScript实现avsc.zip
文件大小:653KB
文件格式:ZIP
更新时间:2022-08-06 22:23:24
开源项目
avsc 是 Apache Avro 的纯 JavaScript 实现。特性:完整的 Avro 架构支持,包括递归架构, sort order, 以及 schema evolution.快速!速度相当于 JSON 的两倍,同时更少的编码(varies per schema).无依赖, avsc 甚至可以在浏览器运行表现:解码吞吐率的示意图(越高越好):库比较: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. */ }); 标签:avsc