All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aliyun.datahub.client.impl.batch.avro.AvroUtils Maven / Gradle / Ivy

The newest version!
package com.aliyun.datahub.client.impl.batch.avro;

import com.aliyun.datahub.client.impl.batch.BatchConstants;
import com.aliyun.datahub.client.model.Field;
import com.aliyun.datahub.client.model.RecordSchema;
import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;

public class AvroUtils {

    public static Schema genAvroSchema(RecordSchema recordSchema) {
        SchemaBuilder.FieldAssembler assembler = SchemaBuilder.record(BatchConstants.AVRO_RECORD_NAME).fields();

        if (recordSchema != null) {
            genTupleAvroSchema(assembler, recordSchema);
        } else {
            genBlobAvroSchema(assembler);
        }

        assembler.name(BatchConstants.ATTRIBUTE_COLUMN_NAME).type().unionOf().nullType().and().map().values().stringType().endUnion().nullDefault();
        return assembler.endRecord();
    }

    private static void genTupleAvroSchema(SchemaBuilder.FieldAssembler assembler, RecordSchema recordSchema) {
        for (Field field : recordSchema.getFields()) {
            switch (field.getType()) {
                case BOOLEAN:
                    if (field.isAllowNull()) {
                        assembler.optionalBoolean(field.getName());
                    } else {
                        assembler.requiredBoolean(field.getName());
                    }
                    break;
                case TINYINT:
                case SMALLINT:
                case INTEGER:
                    if (field.isAllowNull()) {
                        assembler.optionalInt(field.getName());
                    } else {
                        assembler.requiredInt(field.getName());
                    }
                    break;
                case BIGINT:
                case TIMESTAMP:
                    if (field.isAllowNull()) {
                        assembler.optionalLong(field.getName());
                    } else {
                        assembler.requiredLong(field.getName());
                    }
                    break;
                case FLOAT:
                    if (field.isAllowNull()) {
                        assembler.optionalFloat(field.getName());
                    } else {
                        assembler.requiredFloat(field.getName());
                    }
                    break;
                case DOUBLE:
                    if (field.isAllowNull()) {
                        assembler.optionalDouble(field.getName());
                    } else {
                        assembler.requiredDouble(field.getName());
                    }
                    break;
                case STRING:
                case DECIMAL:
                case JSON:
                    if (field.isAllowNull()) {
                        assembler.optionalString(field.getName());
                    } else {
                        assembler.requiredString(field.getName());
                    }
                    break;
            }
        }
    }

    private static void genBlobAvroSchema(SchemaBuilder.FieldAssembler assembler) {
        assembler.requiredBytes(BatchConstants.BLOB_COLUMN_NAME);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy