简体   繁体   English

如何让 Avro SchemaBuilder 在两个“记录”之间构建 map 的模式?

[英]How can I get Avro SchemaBuilder to build a schema of a map between two 'records'?

Trying to serialize Java Objects represented as a Map.尝试序列化 Java 表示为 Map 的对象。 This requires me to build an Avro Schema first.这需要我先构建一个 Avro Schema。 I am using Avro SchemaBuilder for schema generation.我正在使用 Avro SchemaBuilder生成模式。 Have already succeeded in schema generation for Map's objects.已经成功地为 Map 的对象生成模式。 However can't deal with schema building for overall Map.但是无法处理整体 Map 的架构构建。 Final schema should look like the following (created this one manually):最终架构应如下所示(手动创建此架构):

{
    "type": "record",
    "name": "MapObject",
    "namespace": "test",
    "fields": [
        {
            "name": "CacheMap",
            "type": {
                "type": "map",
                "values": [
                    {
                        "type": "record",
                        "name": "User",
                        "namespace": "test",
                        "fields": [                         
                            {
                                "name": "id",
                                "type": "long"
                            },
                            {
                                "name": "status",
                                "type": "string"
                            }
                        ]
                    },
                    {
                        "type": "record",
                        "name": "UserKey",
                        "namespace": "test",
                        "fields": [
                            {
                                "name": "key",
                                "type": "long"
                            },
                            {
                                "name": "keyPrint",
                                "type": "string"
                            }
                        ]
                    }
                ]
            }
        }
    ]
}

How can I pass to different records for key and value of a map in SchemaBuilder?如何在 SchemaBuilder 中将 map 的键和值传递给不同的记录? SchemaBuilder.map().values() does not allow to pass more than one Schema as a parameter for .value() . SchemaBuilder.map().values()不允许传递多个 Schema 作为.value()的参数。

Apache Avro Unions can be of help here. Apache Avro Unions可以在这里提供帮助。

SchemaBuilder.builder()
            .map()
 .values(SchemaBuilder.unionOf().type(firstSchema).and().type(secondSchema).endUnion());

where the firstSchema and the secondSchema are separate schemas already defined for required User and UserKey 'records' (considering the schema from question)其中firstSchemasecondSchema是已为所需的 User 和 UserKey 'records' 定义的单独模式(考虑问题中的模式)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用avro(SchemaBuilder)的递归模式 - Recursive schema with avro (SchemaBuilder) 如何使用apache avro SchemaBuilder更新现有的avro模式? - How do you update an existing avro schema using apache avro SchemaBuilder? 不论模式如何,在地图中读取avro文件记录都会减少 - Read avro file records in map reduce irrespective of schema Avro 和 Kafka 通过使用 SchemaBuilder - Avro and Kafka by making use of SchemaBuilder 我如何使用 Apache Flink 在这个 avro 模式中应用过滤器 - How can i apply a filter in this avro schema using Apache Flink 如何在Avro模式中避免Map of Map - How to avoid Map of Map of Map of Map in Avro schema 如何为地图创建Avro模式 <Integer,Map<Integer,Float> &gt;? - How to create Avro schema for Map<Integer,Map<Integer,Float>>? avro maven 插件无法为 map 架构生成代码 - avro maven plugin can not generate code for map schema 如何将简单的 Java POJO 转换为 avro schema.avsc 文件,然后转换为自动生成的 avro 记录,最终将其推送到 Kafka 主题中? - How to covert Simple Java POJO to avro schema .avsc file and then to autogenerated avro records to finally push it into Kafka topic? 我可以拥有一个 Avro 架构,其中的枚举包含值作为枚举成员吗? - Can I have an Avro Schema with an enum that includes values as enum members?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM