我正在使用融合的kafka平台。为了生成一个主题的消息,我给出了一个包含16个字段的Avro模式。现在,传入的记录将只包含来自这16个字段的数据,但不是全部数据。对于每个字段使用DEFAULT值没有帮助,如discussion中所指出的那样。 这个问题的解决方案是什么?
示例代码:
var KafkaRest = require('kafka-rest');
var AvroSchemais = new KafkaRest.AvroSchema({
"name": "Mydata",
"type": "record",
"fields": [
{ "name": "id", "type": "string" },
{"name" : "data",
"type" : {
"type" : "array",
"items" :{
"name":"manyfields",
"type":"record",
"fields" : [
{"name" : "ip",
"type" : "string",
"default" : "NONE"},
{"name" : "iptime",
"type" : "string",
"default" : "NONE"},
{"name" : "mcc",
"type" : "string",
"default" : "NONE"},
{"name" : "mnc",
"type" : "string",
"default" : "NONE"},
{"name" : "cid",
"type" : "string",
"default" : "NONE"},
{"name" : "lac",
"type" : "string",
"default" : "NONE"}
]}}}]});
topic.produce(AvroSchema, {'id':'abcd','data': [{"ip":"12.12.12.12","lac":"1234"}]},function(err, res){
if (err){console.log(err);}
else{console.log(res);}
});
错误:
message: 'Conversion of JSON to Avro failed: Failed to convert JSON to Avro: Expected field name not found: iptime'
任何帮助表示赞赏!!
答案 0 :(得分:3)
从阅读Avro specification开始,默认情况下应该让你做你想做的事情:
默认值:此字段的默认值,用于读取缺少此字段的实例
您使用的反序列化器可能没有为JSON编码实现此功能。在此期间,您可以使用avsc
通过自动填充缺少的字段来解决此问题:
var KafkaRest = require('kafka-rest'),
avro = avsc;
var attrs = {
"name": "Mydata",
"type": "record",
"fields": [ /* ... */ ]
};
var AvroSchema = new KafkaRest.AvroSchema(attrs);
var type = avro.parse(attrs);
var withoutDefaults = {'id': 'abcd','data': [{"ip":"12.12.12.12", "lac":"1234"}]};
var withDefaults = type.clone(withoutDefaults); // All defaults are present here.
topic.produce(AvroSchema, withDefaults, function (err, res){
if (err) { console.log(err); }
else { console.log(res); }
});