使用aerospike模板java将数据插入aerospike
我正在尝试将数据插入aerospike。为了对Aerospike客户端执行同样的操作,我写道:使用aerospike模板java将数据插入aerospike,java,spring-data,aerospike,Java,Spring Data,Aerospike,我正在尝试将数据插入aerospike。为了对Aerospike客户端执行同样的操作,我写道: Key key = new Key("test", "myset", "mykey"); Bin bin = new Bin("shahjahan", "k"); aerospikeClient.put(new WritePolicy(), key, bin); 现在,我想用AeroSpike模板做同样的事情。但模板中的插入方法期望对象作为参数,而不是键和容器 @Ov
Key key = new Key("test", "myset", "mykey");
Bin bin = new Bin("shahjahan", "k");
aerospikeClient.put(new WritePolicy(), key, bin);
现在,我想用AeroSpike模板做同样的事情。但模板中的插入方法期望对象作为参数,而不是键和容器
@Override
public <T> T insert(T objectToInsert, WritePolicy policy) {
Assert.notNull(objectToInsert, "Object to insert must not be null!");
try {
AerospikeData data = AerospikeData.forWrite(this.namespace);
converter.write(objectToInsert, data);
Key key = data.getKey();
Bin[] bins = data.getBinsAsArray();
client.put(policy == null ? this.insertPolicy : policy, key, bins);
}
catch (AerospikeException o_O) {
DataAccessException translatedException = exceptionTranslator
.translateExceptionIfPossible(o_O);
throw translatedException == null ? o_O : translatedException;
}
return null;
}
@覆盖
公共T插入(T objectToInsert,WritePolicy策略){
Assert.notNull(objectToInsert,“要插入的对象不能为null!”);
试一试{
AerospikeData=AerospikeData.forWrite(this.namespace);
write(objectToInsert,data);
Key=data.getKey();
Bin[]Bin=data.getbinsaarray();
client.put(policy==null?this.insertPolicy:policy,key,bin);
}
捕获(Aerospike例外o_o){
DataAccessException translatedException=exceptionTranslator
.translateException如果可能(o_o);
抛出translatedException==null?o_o:translatedException;
}
返回null;
}
我想知道如何传递键和值以插入数据。您的对象需要有一个
@Id
注释来指定记录的键。所有其他字段将存储为箱子。下面是一个例子:
public class Product {
@Id
private Integer id;
private String productId;
private String description;
private String imageUrl;
private double price;
...
}
然后只需对对象调用save()
:
productRepository.save(product);