【问题标题】:How to create valid Avro format file nodejs如何创建有效的 Avro 格式文件 nodejs
【发布时间】:2016-06-13 20:21:16
【问题描述】:

我正在尝试创建一个有效的 avro 文件以上传到 google-bigquery。

var avro = require('node-avro-io').DataFile.AvroFile();
var schema = {
            "name": "data",
            "type": "record",
            "fields": [
                {"name":"key","type": "string"},
                {"name":"value","type": "string"},
                {"name":"flag","type": "boolean"},
                {"name":"subrecord","type":"record","fields":[
                    {"name":"key","type":"string"},
                    {"name":"value","type":["string","int","null"]}
                ]}
            ]
};
var writer = avro.open("myAvroFile.avro", schema, { flags: 'w' , codec: 'deflate'});
writer
    .append({ key:"john", value:"hive", flag: true, subrecord: { key: "preference", value: 2}})
    .append({ key:"eric", value:"lola", flag: true, subrecord: { key: "postcode", value: null}})
    .end({ key:"fred", value:"wonka", flag: false, subrecord: { key: "city", value: "michigan"}});

这里是myAvroFile.avro:

Obj�avro.codec�deflate�avro.schema�{"name":"data","type":"record","fields":[{"name":"key","type":"string"},{"name":"value","type":"string"},{"name":"flag","type":"boolean"},{"name":"subrecord","type":"record","fields":[{"name":"key","type":"string"},{"name":"value","type":["string","int","null"]}]}]} �3????�ä0�x���A� @0O� )�T�%H4��:�*Uy�>P0��%�05k��n�d�T�������\����I�3????�ä0�x�

但是当我尝试将它上传到大查询时它失败了:

The Apache Avro library failed to parse file

当我尝试从以下链接上传解压缩的 avro fli 时,我成功了。 https://cloud.google.com/bigquery/docs/yob1900.avro.zip

myAvroFile.avro 无效的任何原因? 如何使用node-avro-io 库创建有效的 avro 文件?

【问题讨论】:

  • 不确定我能否处理嵌入的 Avro 文件。如果您可以通过 google.com 的 huazhang 将其通过电子邮件发送给我,我可以看看。
  • 是的,这就是问题所在。当我删除嵌套字段时,它运行良好。

标签: node.js google-bigquery avro


【解决方案1】:

看起来评论的限制很小。我会发布作为答案。

这是我使用 avro-tools 得到的错误消息:

线程“main”org.apache.avro.SchemaParseException 中的异常:“record”不是定义的名称。 “子记录”字段的类型必须是定义的名称或 {"type": ...} 表达式。

我发现 avro-tools 可以方便地处理 avro 文件。这个link 解释了如何使用它。

“子记录”字段的类型字段需要是模式。根据 Avro specification:

type:定义模式的 JSON 对象,或命名为 记录定义(必需)。

所以你应该像这样改变它:

{"name":"subrecord",
 "type": {"name":"subrecord_type",
          "type":"record",
          "fields":[{"name":"key","type":"string"},
                    {"name":"value","type":["string","int","null"]}
                   ]
         }
}

【讨论】:

    猜你喜欢
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    相关资源
    最近更新 更多