如何在Avro中定义LogicalType。(java)

enr*_*ico 6 java avro

我需要能够在AVRO模式中标记一些字段,以便在序列化时对它们进行加密。

logicalType允许标记字段,并且与自定义转换一起应允许AVRO透明地对其进行加密。


我遇到一些问题,无法找到有关如何在AVRO中定义和使用新的logicalType(avro_1.8.2#Logical + Types)的文档
然后,我决定在这里分享我找到的答案,以简化其他人的生活,并在我做错事情时获得一些反馈。

enr*_*ico 11

首先,我将逻辑类型定义为:

public class EncryptedLogicalType extends LogicalType {
    //The key to use as a reference to the type
    public static final String ENCRYPTED_LOGICAL_TYPE_NAME = "encrypted";

    EncryptedLogicalType() {
        super(ENCRYPTED_LOGICAL_TYPE_NAME);
    }

    @Override
    public void validate(Schema schema) {
        super.validate(schema);
        if (schema.getType() != Schema.Type.BYTES) {
            throw new IllegalArgumentException(
                    "Logical type 'encrypted' must be backed by bytes");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后进行新的转换:

public class EncryptedConversion extends Conversion<ByteBuffer> {
    // Construct a unique instance for all the conversion. This have to be changed in case the conversion
    //   needs some runtime information (e.g.: an encryption key / a tenant_ID). If so, the get() method should 
    //   return the appropriate conversion per key.
    private static final EncryptedConversion INSTANCE = new EncryptedConversion();
    public static final EncryptedConversion get(){ return INSTANCE; }
    private EncryptedConversion(){ super(); }

    //This conversion operates on ByteBuffer and returns ByteBuffer
    @Override
    public Class<ByteBuffer> getConvertedType() { return ByteBuffer.class; }

    @Override
    public String getLogicalTypeName() { return EncryptedLogicalType.ENCRYPTED_LOGICAL_TYPE_NAME; }

    // fromBytes and toBytes have to be overridden as this conversion works on bytes. Other may need to be 
    //  overridden. The types supported need to be updated also in EncryptedLogicalType#validate(Schema schema)
    @Override
    public ByteBuffer fromBytes(ByteBuffer value, Schema schema, LogicalType type) {
        encryptedValue = __encryptionLogic__(value); 
        return encryptedValue;
    }

    @Override
    public ByteBuffer toBytes(ByteBuffer value, Schema schema, LogicalType type) {
        decryptedValue = __decryptionLogic__(value); 
        return decryptedValue;
    }
}
Run Code Online (Sandbox Code Playgroud)

.avsc模式文件将类似于:

{
    "name": “MyMessageWithEncryptedField”,
    "type": "record",
    "fields": [
        {"name": "payload","type" : {"type" : "bytes","logicalType" : "encrypted"}},
        ...
Run Code Online (Sandbox Code Playgroud)

最后,在MyMessageWithEncryptedField.java从模式文件生成的类中,我添加了方法以返回转换:

@Override
public Conversion<?> getConversion(int fieldIndex) {
    // This allow us to have a more flexible conversion retrieval, so we don't have to code it per field.
    Schema fieldSchema = SCHEMA$.getFields().get(fieldIndex).schema();
    if ((fieldSchema.getLogicalType() != null)
            && (fieldSchema.getLogicalType().getName() == EncryptedLogicalType.ENCRYPTED_LOGICAL_TYPE_NAME)){
     // here we could pass to the get() method a runtime information, e.g.: a tenantId that can be found in the data structure.
        return EncryptedConversion.get();
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

为了使其运行,我仍然必须在运行时注册类型:

LogicalTypes.register(EncryptedLogicalType.ENCRYPTED_LOGICAL_TYPE_NAME, new LogicalTypes.LogicalTypeFactory() {
    private final LogicalType encryptedLogicalType = new EncryptedLogicalType();
    @Override
    public LogicalType fromSchema(Schema schema) {
        return encryptedLogicalType;
    }
});
Run Code Online (Sandbox Code Playgroud)

一些注意事项:

  • 如果您的logicalType需要从架构定义中传入其他一些属性,则可以从avro.lang.java.avro.src.main.java.org.apache.avro.LogicalTypes.Decimal中的示例修改LogicalType类。
  • 最后一段代码(寄存器)当前在我的逻辑开始之前运行,但我计划将其移至架构生成的类(MyMessageWithEncryptedField.java)中的静态块中