使用Spring Data Aerospike如何避免存储@user_key与@_class bin
你可以通过修改Aerospike配置类,关闭这两个默认字段的存储逻辑,具体操作如下:
1 字段说明
@_class:Spring Data Aerospike默认用于多态类型映射的别名字段,用于多实体类型场景下的反序列化识别@user_key:框架默认将@Id标注的主键额外写入bin的字段,实际上主键已经保存在Aerospike记录的元数据key中,属于重复存储
2 修改配置类关闭字段写入
在你现有的AerospikeConfiguration类中新增aerospikeConverter方法重写,配置如下:
@Configuration @EnableConfigurationProperties(AerospikeConfigurationProperties.class) @EnableAerospikeRepositories(basePackages = "com.moneris.repositories") public class AerospikeConfiguration extends AbstractAerospikeDataConfiguration { @Autowired private AerospikeConfigurationProperties aerospikeConfigurationProperties; @Override protected Collection<Host> getHosts() { return Collections.singleton(new Host(aerospikeConfigurationProperties.getHost(), aerospikeConfigurationProperties.getPort())); } @Override protected String nameSpace() { return aerospikeConfigurationProperties.getNamespace(); } // 新增配置关闭目标字段写入 @Override public AerospikeConverter aerospikeConverter(AerospikeMappingContext context, AerospikeCustomConversions conversions, @Nullable KeyToBinMapper keyToBinMapper) { // 传入null作为typeKey参数,关闭@_class字段写入 MappingAerospikeConverter converter = new MappingAerospikeConverter(context, conversions, null); // 主键映射器设置为null,关闭@user_key字段写入 converter.setKeyToBinMapper(null); return converter; } }
注意事项
如果你的项目后续需要使用多态实体存储、查询能力,不要关闭@_class字段,否则会导致类型映射失败无法正常反序列化数据。当前你的示例中只有单实体类型,关闭这两个字段不会影响现有读写逻辑。
内容的提问来源于stack exchange,提问作者Shaho Shahbazpanahi
相关产品推荐
相关产品推荐

