json - 在 Node js 中使用 Avro 序列化数据

标签 json node.js serialization avro

我想序列化来自 JSON 对象的数据,并以 kafka 为结尾通过网络发送。现在我在一个文件中有一个 avro 模式,它确定了为日志系统发送到 kafka 所需的字段:

{"namespace": "com.company.wr.messages",
   "type": "record",
   "name": "Log",
   "fields": [
       {"name": "timestamp", "type": "long"},
       {"name": "source", "type": "string"},
       {"name": "version", "type": "string"},
       {"name": "ipAddress", "type": "string"},
       {"name": "name", "type": "string"},
       {"name": "level", "type": "string"},
       {"name": "errorCode", "type": "string"},
       {"name": "message", "type": "string"}
       ]
}

我正在使用 Node 包“avro-schema”,我尝试了其他的,但都没有正常工作,我只需要从 Node js 以 avro 方式序列化。

最佳答案

avsc :

var avro = require('avsc');

// Parse the schema.
var logType = avro.parse({
  "namespace": "com.company.wr.messages",
  "type": "record",
  "name": "Log",
  "fields": [
    {"name": "timestamp", "type": "long"},
    {"name": "source", "type": "string"},
    {"name": "version", "type": "string"},
    {"name": "ipAddress", "type": "string"},
    {"name": "name", "type": "string"},
    {"name": "level", "type": "string"},
    {"name": "errorCode", "type": "string"},
    {"name": "message", "type": "string"}
  ]
});

// A sample log record.
var obj = {
  timestamp: 2313213,
  source: 'src',
  version: '1.0',
  ipAddress: '0.0.0.0',
  name: 'foo',
  level: 'INFO',
  errorCode: '',
  message: ''
};

// And its corresponding Avro encoding.
var buf = logType.toBuffer(obj);

您可以找到有关可用的各种编码方法的更多信息here .

关于json - 在 Node js 中使用 Avro 序列化数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22935558/

相关文章:

php - 如何从YouTube Feed中提取RTSP链接

javascript - jQuery each() 的 JSON 结构

javascript - socketstream 和第 3 方存储库

c# - C# 中的 Google Protocol Buffer

c++ - 具有 Boost::serialization 的 XML 层次结构

java - Spring MVC 测试中带有过滤器的 JsonPath 表达式

javascript - NodeJS 与 Express : Header undefined

javascript - 为什么 String.raw 不能以反斜杠结尾?

php - Symfony:如何使 JMS 序列化程序与严格类型一起工作?

javascript - 在合并对象的键中创建值数组