Apache Avro 純 JavaScript 實現:avsc
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. */ });
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!