0

есть метод для создания схемы Avro:

public void schemaCreator(String recordName, EntityModel entityModel){
        SchemaBuilder.FieldAssembler<Schema> fields;
        SchemaBuilder.RecordBuilder<Schema> record = SchemaBuilder.record(recordName);
        fields = record.namespace("Test").fields();
        for (AttributeModel attributeModel : entityModel.getAttributeModels()) {


            fields = fields.name(attributeModel.getName()).type().nullable().stringType()
                    .noDefault();

        }
    }

В результате имеем схему:

{
 "type":"record",
 "name":"InfoData",
 "namespace":"Test",
 "fields":[
   {
    "name":"Setting",
    "type":["string","null"]
   },
   {
    "name":"Value",
    "type":["string","null"]}]
   }

Как добавить несколько типов для поля? Например: "type":["string","long","boolean","null"]}]

1 ответ 1

0

Вот вариант решения, таким образом можно формировать список типов в цикле:

public void schemaCreator(String recordName, EntityModel entityModel){
        SchemaBuilder.FieldAssembler<Schema> fields;
        SchemaBuilder.RecordBuilder<Schema> record = SchemaBuilder.record(recordName);
        fields = record.namespace("Test").fields();
        final Schema intType = Schema.create(Schema.Type.INT);
        final Schema booleanType = Schema.create(Schema.Type.BOOLEAN);
        final Schema nullType = Schema.create(Schema.Type.NULL);

        for (AttributeModel attributeModel : entityModel.getAttributeModels()) {
            Schema unionSchema = Schema.createUnion(Arrays.asList(nullType, intType, booleanType));
            fields.name(attributeModel.getName()).type(unionSchema).noDefault();
        }
    }

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.