如何为复杂的 json 文档定义 avro 模式?

编程入门 行业动态 更新时间:2024-10-26 19:24:52
本文介绍了如何为复杂的 json 文档定义 avro 模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 JSON 文档,我想将其转换为 Avro,并且需要为此目的指定一个架构.这是我想为其定义 avro 模式的 JSON 文档:

I have a JSON document that I would like to convert to Avro and need a schema to be specified for that purpose. Here is the JSON document for which I would like to define the avro schema:

{ "uid": 29153333, "somefield": "somevalue", "options": [ { "item1_lvl2": "a", "item2_lvl2": [ { "item1_lvl3": "x1", "item2_lvl3": "y1" }, { "item1_lvl3": "x2", "item2_lvl3": "y2" } ] } ] }

我可以为非复杂类型定义架构,但不能为复杂的选项"字段定义架构:

I'm able to define the schema for the non-complex types but not for the complex "options" field:

{ "namespace" : "my.ns", "type" : "record", "fields" : [ {"name": "uid", "type": "int"}, {"name": "somefield", "type": "string"} {"name": "options", "type": .....} ] }

感谢您的帮助!

推荐答案

需要使用Avro 复杂类型,特别是 数组 和 记录.然后将它们嵌套在一起:

You need to use Avro complex types, specifically arrays and records. And then nest these together:

{ "namespace" : "my.ns", "name": "myrecord", "type" : "record", "fields" : [ {"name": "uid", "type": "int"}, {"name": "somefield", "type": "string"}, {"name": "options", "type": { "type": "array", "items": { "type": "record", "name": "lvl2_record", "fields": [ {"name": "item1_lvl2", "type": "string"}, {"name": "item2_lvl2", "type": { "type": "array", "items": { "type": "record", "name": "lvl3_record", "fields": [ {"name": "item1_lvl3", "type": "string"}, {"name": "item2_lvl3", "type": "string"} ] } }} ] } }} ] }

此外,为了提高可读性,您可以 拆分架构 到多个文件中.

Also, to improve readiblity, you can split the schema into multiple files.

更多推荐

如何为复杂的 json 文档定义 avro 模式?

本文发布于:2023-11-23 09:07:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620879.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为   定义   模式   文档   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!