是否可以将具有相同架构的通用记录转换为特定记录? [英] Is it possible to convert a generic record to specific record with the same schema?

查看:92
本文介绍了是否可以将具有相同架构的通用记录转换为特定记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模式A的GenericRecord对象,它也是一个生成的Avro Java类.我有可能以某种方式将此对象转换为实际的A类型吗?

I have a GenericRecord object of schema A, which is also a generated Avro Java class. Is it possible for me to cast this object into actual A type somehow?

推荐答案

您可以使用deepCopy

SpecificData.get().deepCopy(genericRecord.schema, genericRecord)

相反的操作-将SpecificRecord转换为GenericRecord也可以.请注意,此方法不支持架构演变:只有一个架构传递给deepCopy,并且用于读取和写入.这意味着将根据字段的位置复制字段,因此SpecificRecord架构必须与GenericRecord架构完全匹配.

The reverse operation -- converting a SpecificRecord to a GenericRecord also works. Note that this approach does not support schema evolution: there is only one schema passed to the deepCopy and it is used for both reading and writing. This means the fields are copied based on their position, so the SpecificRecord schema must match the GenericRecord schema exactly.

请参见 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆